Structs2 Result学习笔记(一)简介
问题一
<struts> <constant name="struts.devMode" value="true" /> <package name="resultTypes" namespace="/r" extends="struts-default"> <action name="r1"> <result type="dispatcher">/r1.jsp</result> </action> <action name="r2"> <result type="redirect">/r2.jsp</result> </action> <action name="r3"> <result type="chain">r1</result> </action> <action name="r4"> <result type="redirectAction">r2</result> </action> </package> </struts>dispatcher - forward运用server跳转 server跳转 显示action
redirect - 重定向 跳转到jsp
chain server跳转
redirectAction问题二
chain跳转 -> 从一个action,跳转至另外一个包里面的action的方法:<package name="public" extends="struts-default"> <!-- Chain creatAccount to login, using the default parameter --> <action name="createAccount" class="..."> <result type="chain">login</result> </action> <action name="login" class="..."> <!-- Chain to another namespace --> <result type="chain"> <param name="actionName">dashboard</param> <param name="namespace">/secure</param> </result> </action> </package> <package name="secure" extends="struts-default" namespace="/secure"> <action name="dashboard" class="..."> <result>dashboard.jsp</result> </action> </package>通过样例能够看出,是通过为chain配置actionName參数和namespace參数。
详细实例參见struts-2.1.6/docs/docs/chain-result.html里面的文档说明。
问题三。全局Result
//前端htm <body> Result类型 <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> </body>
//struct.xml <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.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.struts2.user.action.AdminAction"> <result>/admin.jsp</result> </action> </package> </struts>
//类包 //AdminAction package com.struts2.user.action; import com.opensymphony.xwork2.ActionSupport; public class AdminAction extends ActionSupport { @Override public String execute() throws Exception { return "mainpage"; } } //UserAction package com.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"; } }
分析1
<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.struts2.user.action.UserAction"> <result>/user_success.jsp</result> <result name="error">/user_error.jsp</result> </action> </package>这段配置文件里,result 为success时。调用user_success.jsp,result为error时,调用
/user_error.jsp。其余全部的情况都使用result为mainpage的配置。能够共用这个结果集。
分析二
<package name="admin" namespace="/admin" extends="user"> <action name="admin" class="com.struts2.user.action.AdminAction"> <result>/admin.jsp</result> </action> </package>extends的作用是从另外一个包里面继承配置信息,工作中使用对项目进行分模块处理,每一个模块都有公共的配置信息,这样能够将公共配置信息定义为<global-results>。然后被各个模块继承,简化处理。和c++/java中的继承关系好像啊!
问题四:struct-default解析
源文件在ReferencedLibraries/WebRoot/WEB-INF/lib/struts2-core-2.1.6.jar/struct-default.xml<package name="struts-default" abstract="true">默认定义了拦截器
<default-interceptor-ref name="defaultStack"/> <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />结论:配置非常复杂,使用不需改。