隨著struts2的不斷升級,Struts開始使用convention-plugin代替codebehind-plugin來實現struts的零配置。所謂的零配置並不是任何配置都不需要,而是採用約定大於配置的方式。
在web開發過程中,根據convention-plugin的預設約定,我們不再需要在Struts.xml中配置任何資訊。
首先,讓我們先來看一下基於convention-plugin實現的Struts"零配置"的HelloWorld。
struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="default" namespace="/" extends="struts-default" /></struts>
我的HelloWorld
package com.example.actions;import com.opensymphony.xwork2.ActionSupport;public class HelloWorld extends ActionSupport { private static final long serialVersionUID = -6662538623355755690L; private String message; public String getMessage() { return message; } public String execute() { if (System.currentTimeMillis() % 2 == 0) { message = "It's 0"; return "zero"; } message = "It's 1"; return SUCCESS; }}
對於一個簡單的HelloWorld來講,struts.xml裡面的配置簡直精簡到了極致。不需要再寫一堆一堆的<action>配置了。而我們需要做的只是加入一個struts2-convention-plugin-x.x.x.x.jar。
之所以在HelloWorld中不需要任何配置,是因為struts2-convention-plugin進行了以下一系列的約定。 Action location by package naming conventions com.example. action.Main Action com.example. actions.products.Display (implements com.opensymphony.xwork2.Action) com.example. struts.company.details.ShowCompanyDetails Action com.example. struts2.company.details.ShowCompanyDetailsAction
Convention plugin預設將包路徑包含action,actions,struts,struts2的所有包都作為Action類的路徑來搜尋。你可以通過設定struts.convention.package.locators屬性來修改這個配置。 Convention plugin預設的配置如下:
<constant name="struts.convention.package.locators" value="action,actions,struts,struts2"/>
Convention plugin在上述的包及其子包中尋找尾碼為Action、或者實現了com.opensymphony.xwork2.Action的類。你可以通過設定
struts.convention.action.suffix屬性來修改這個配置。Convention plugin預設的配置如下:
<constant name="struts.convention.action.suffix" value="Action"/>
Result (JSP, FreeMarker, etc) location by naming conventions
當在瀏覽器中輸入http://localhost:8080/hello-world訪問時,頁面會跳轉到WEB-INF/content/hello-world.jsp。
預設情況下, Convention plugin假設所有的results存放在WEB-INF/content/下。可以通過設定struts.convention.result.path進行修改。Convention plugin預設的配置如下:
<constant name="struts.convention.result.path" value="/WEB-INF/content/"/>
Convention plugin即使在找不到對應的action的情況下,也可以通過Action的URL(這裡是/hello-world)找到對應的結果(/hello-world.jsp)。
Class name to URL naming convention com.example.actions.MainAction -> /main
com.example.actions.products.Display -> /products/display
com.example.struts.company.details.ShowCompanyDetailsAction -> /company/details/show-company-details
以com.example.struts.company.details.ShowCompanyDetailsAction對應的Action URL為例,Convention plugin會將com.example.struts.company.details.ShowCompanyDetailsAction 的struts後的子包轉換成命名空間“/company/details/”,然後ShowCompanyDetailsAction中的“Action”尾碼去掉,並將駝峰式的命名大寫變小寫,並在之間加上“-”(當然“-”也是Convention plugin預設的)最終形成/company/details/show-company-details。這就是類名和Action URL之間的“約定”。
Package name to namespace convention com.example.actions.MainAction -> /
com.example.actions.products.Display -> /products
com.example.struts.company.details.ShowCompanyDetailsAction -> /company/details
Convention plugin預設將actions、action、struts、struts2作為root Package,那麼com.example.actions.MainAction的命名空間即為“/”,而com.example.actions.products.Display的命名空間為“/products”。
正是有了這些“約定”才使得我們的HelloWorld能夠實現零配置。
實際的開發過程中如果只這些約定,也許不能完全滿足實際項目的需要。幸運的是Convention Plugin的配置是相當靈活的。
日後,在繼續學習基於註解的靈活配置。