一、概要
二、常用四种类型的配置
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="resultTypes" namespace="/resultType" extends="struts-default"> <!--默认重定向--> <action name="result1"> <result type="dispatcher" >/jsp/resultType/resultType_result1.jsp</result> </action> <!---转发--> <action name="result2"> <result type="redirect">/jsp/resultType/resultType_result2.jsp</result> </action> <!--重定向至result1Action--> <action name="result3"> <result type="chain">result1</result> </action> <!--转发到转发result2Action--> <action name="result4"> <result type="redirectAction">result2</result> </action> </package> </struts>
三、全局结果集
<?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"> <!--共享于package的全局文件--> <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> <!--通过exrtend获得全局文件--> <package name="admin" namespace="/admin" extends="user"> <action name="admin" class="com.bjsxt.struts2.user.action.AdminAction"> <result>/admin.jsp</result> </action> </package> </struts>
四、配置动态结果
1、Struts.xml <package name="user" namespace="/user" extends="struts-default"> <action name="hello" class="com.aaron.UserAction"> <result>${result}</result> </action> </package> 2、UserAction.java public class UserAction extends ActionSupport { int type; String result; @Override public String execute() throws Exception { System.out.println(type); if (type == 1) { result = "/jsp/DynamicResult/success.jsp"; } else if (type == 2) { result = "/jsp/DynamicResult/error.jsp"; } return SUCCESS; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } }
五、url尾部附加参数_用于redirect
1、Struts.xml url?type=${type}
源码链接:http://pan.baidu.com/s/1kVExOnd
借鉴:来自马士兵视频|Struts2的思考