struts2 注解配置
1、web.xmlJava代碼
- <!-- struts2 配置 -->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
- </filter-class>
- <init-param>
- <param-name>actionPackages</param-name>
- <param-value>com.yz.webapp.action</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
struts.properties
Java代碼
- #修改時重新加載
- struts.configuration.xml.reload = true
- #打印更多錯誤信息
- struts.devMode = true
- #注解掃描的包結尾名
- struts.convention.package.locators = action
- #映射擴展名
- struts.action.extension = html
- #結果資源所在路徑
- #struts.convention.result.path = /WEB-INF/pages
2、action 類名上加注解
Java代碼
- @Namespace("/ssi")
- @ParentPackage("json-default")
- @Action(value = "admin", results = {
- @Result(name = "success", location = "/WEB-INF/pages/admin.jsp"),
- @Result(name = "json", type = "json", params = { "excludeProperties","adminMgr" }) })
- public class AdminAction extends BaseAction{}
多個Action
Java代碼
- @Namespace("/msa")
- @Result(name = "json", type = "json", params = { "excludeProperties",
- ".*Manager,.*\\.authorities,.*\\.roles,.*\\.fileCon" })
- @Actions(value = {
- @Action(value = "foreignship", results = { @Result(name = "success", location = "foreignship/foreignship.jsp") }),
- @Action(value = "foreignshipsee", results = { @Result(name = "success", location = "foreignship/foreignshipsee.jsp") }) })
- public class TMsaForeignShipArchivesAction extends BaseAction{}
在類方法上加注解
Java代碼
- //@Action(value = "add", results = { @Result(name = "success", location = "/index.jsp") })
- @Action(value = "save")
- public String save() {
- try{
- adminMgr.insert("insertYz_admin", admin);
- }catch(Exception e){
- msg = e.toString();
- success = false;
- }
- return this.SUCCESS;
- }
posted on 2013-06-22 10:23 void 閱讀(422) 評論(0) 編輯 收藏 所屬分類: Struts2