<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运用服务器跳转 服务器跳转 显示action
redirect - 重定向 跳转到jsp
chain 服务器跳转
redirectAction<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参数;
//前端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"; } }
<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时,调用
<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中的继承关系好像啊!
<package name="struts-default" abstract="true">默认定义了拦截器
<default-interceptor-ref name="defaultStack"/> <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />结论:配置很复杂,使用不需改。
【web开发学习笔记】Structs2 Result学习笔记(一)简单介绍,布布扣,bubuko.com
【web开发学习笔记】Structs2 Result学习笔记(一)简单介绍
原文地址:http://blog.csdn.net/licong_carp/article/details/37070475