【web开发学习笔记】Structs2 Result学习笔记(一)简单介绍
2014-07-05 18:54
549 查看
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运用服务器跳转 服务器跳转 显示action
redirect - 重定向
跳转到jsp
chain
服务器跳转
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" />结论:配置很复杂,使用不需改。
相关文章推荐
- 【web开发学习笔记】Structs2 Result学习笔记(三)带參数的结果集
- 【web开发学习笔记】Structs2 Result学习笔记(二)动态结果集
- 【web开发学习笔记】Structs2 Result学习笔记(三)带参数的结果集
- 【web开发学习笔记】Structs2 Result学习笔记(二)动态结果集
- Ubuntu操作系统学习笔记之Vim简单介绍
- MongoDB学习笔记-- 简单介绍(二)
- pthread学习笔记(二)--pthread常用API及简单介绍
- 【web开发学习笔记】Structs2 Action学习笔记(一)
- iOS学习笔记14(1)—Core Data简单介绍和实现
- 【web开发学习笔记】Structs2 Action学习笔记(两)
- Lucene5 学习笔记(2) —— 简单介绍 Lucene 搜索功能和索引的修改、删除
- 【web开发学习笔记】Structs2 Action学习笔记(四)include的使用
- Nutch 1.3 学习笔记 10-1 - Ntuch 插件机制简单介绍
- AngularJS基础学习笔记之简单介绍
- Flume-1.6.0学习笔记(一)简单介绍
- Android自动化学习笔记之MonkeyRunner:官方介绍和简单实例
- 【web开发学习笔记】web.xml配置简单分析一
- Git学习笔记1--Git原理简单介绍
- [每日学习笔记][2013.03.24]MYSQL的语法和简单介绍(二)
- Apache Nutch 1.3 学习笔记十(Ntuch 插件机制简单介绍)