首先添加lib:
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-config-browser-plugin</artifactId> <version>2.3.20</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.20</version> </dependency>
訪問 http://localhost:8080/conv/config-browser/a.action 可以查看目前映射的所有路徑
插件會按以下順序執行:
1.尋找以下包:struts
, struts2
, action
,actions
任何深度找到的以上包都自動作為根目錄
2.然后在包內尋找以下類:實現了
com.opensymphony.xwork2.Action
或者繼承了com.opensymphony.xwork2.ActionSupport,或類名以Action結尾
3.映射為url:
com.example.actions.MainAction -> /maincom.example.actions.PRoducts.Display -> /products/displaycom.example.struts.company.details.ShowCompanyDetailsAction -> /company/details/show-company-details(全部為小寫)
比如,
會映射成,
可以跳過某些包:struts.convention.exclude.packages
=org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.* (被跳過的包是無法再加入的,即使手動添加)
自動搜索的包名:struts.convention.package.locators
=action,actions,struts,struts2
自動搜索的包名開頭:struts.convention.package.locators.basePackage=
自動搜索的類名結尾:struts.convention.action.suffix=Action
手動指定具體包:struts.convention.action.packages=
4.默認,所有的Result都到這里去找:WEB-INF/content
如, ,則可以通過 http://localhost:8080/conv/my-jsp.action 訪問到
可通過常量控制:struts.convention.result.path
=/WEB-INF/content/
根據返回類型和返回字符串,要起不同的名字:
5.如果找不到這個頁面文件,就認為這是個action,其他action可以在方法上添加@Action(“xx”),指明具體路徑,這也就是Result Type為chain
如 中,
public class MyJsp extends ActionSupport { public String execute() {return "here"; }}
public class HimJsp extends ActionSupport { @Action("my-jsp-here") //通過指定為具體路徑,實現chain,前提是沒有那個頁面文件 public String execute() {return "yes"; }}
最后會變成,
可以設置自動重載:
<
constant
name="struts.devMode" value="true"/>
<
constant
name="struts.convention.classes.reload" value="true" />
struts.convention.action.alwaysMapExecute =false 禁止自動調用execute()
struts.convention.action.disableScanning=true 禁用掃描
struts.convention.action.mapAllMatches =true 沒有@Action也自動映射
struts.convention.action.name.lowercase=false 不要變成小寫
struts.convention.action.name.separator=_ 名稱分隔符
struts.convention.action.eagerLoading=true 不使用Spring的時候可以提高性能
新聞熱點
疑難解答