zhaozhi3758 发表于 2013-2-6 08:41:38

struts2 注解配置

1、web.xml
<!-- 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
#修改时重新加载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 类名上加注解
@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
@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{}

在类方法上加注解
//@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;}returnthis.SUCCESS;}
页: [1]
查看完整版本: struts2 注解配置