如果有很多action,有共同的result指向, 而且属于不同的包,那么可以继承上面的包, 然后写一个<global-results>
1. jsp显示文件:
<ol> <li><a href="user/user?type=1">返回success</a></li> <li><a href="user/user?type=2">返回error</a></li> <li><a href="user/user?type=3">返回global result</a></li> <li><a href="admin/admin">admin,继承user包</a></li> </ol>
2. struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/user" extends="struts-default"> <global-results> <result name="mainpage">/main.jsp</result> </global-results> <action name="index"> <result>/index.jsp</result> </action> <action name="user" class="com.bjsxt.struts2.user.action.UserAction"> <result>/user_success.jsp</result> <result name="error">/user_error.jsp</result> </action> </package> <package name="admin" namespace="/admin" extends="user"> <action name="admin" class="com.bjsxt.struts2.user.action.AdminAction"> <result>/admin.jsp</result> </action> </package> </struts>
userAction.JAVA:
package com.bjsxt.struts2.user.action; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private int type; public int getType() { return type; } public void setType(int type) { this.type = type; } @Override public String execute() throws Exception { if(type == 1) return "success"; else if (type == 2) return "error"; else return "mainpage"; } }
adminAction.java:
package com.bjsxt.struts2.user.action; import com.opensymphony.xwork2.ActionSupport; public class AdminAction extends ActionSupport { @Override public String execute() throws Exception { return "mainpage"; } }