struts.xml中result的学习
2015-12-17 14:39
453 查看
1.result的type类型4种:
dispatcher/redirect/chain/redirectAction ,其中default值为dispatcher<package name="index" namespace="/yanlu" extends="struts-default"> <action name="yan1" > <result type="dispatcher">/yan1.jsp</result> </action> <action name="yan2" > <result type="redirect">/yan2.jsp</result> </action> <action name="yan3" > <result type="chain">yan1</result> </action> <action name="yan4" > <result type="redirectAction">yan2</result> </action> </package>
例子中,yan1,yan3执行后现实页面都为yan1.jsp
yan2,yan4执行后现实页面都为yan2.jsp
dispatcher,redirect跳转的页面为jsp,html等得前台界面
chain,redirectAction跳转为action跳转
2.global-results和extends
index.jsp<ol> <span style="white-space:pre"> </span><li><a href="user/user?type=1">返回success</a></li> <span style="white-space:pre"> </span><li><a href="user/user?type=2">返回error</a></li> <span style="white-space:pre"> </span><li><a href="user/user?type=3">返回global result</a></li> <span style="white-space:pre"> </span><li><a href="admin/admin">admin,继承user包</a></li> </ol>
UserAction.java
public String execute() throws Exception { <span style="white-space:pre"> </span>if(type == 1) {return "success";} <span style="white-space:pre"> </span>else if (type == 2) {return "error";} <span style="white-space:pre"> </span>else {return "mainpage";} <span style="white-space:pre"> </span>}
返回对应result的name的值。result的name属性默认值为success
AdminAction.java
public String execute() throws Exception { return "mainpage"; }
struts.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="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>
例子:个人感觉global-result用处不大,action的excute()返回值为"mainpage",才会调用。不过学习 了,还是记下。
extends默认值:struts-defaults.在当前package的action中找不到对应的result,根据extends去父package中找global-results.
3.动态结果集
UserAction.javapublic String execute() throws Exception { if(type == 1) r="/user_success.jsp"; else if (type == 2) r="/user_error.jsp"; return "success"; }
index.jsp
<ol> <li><a href="user/user?type=1">返回success</a></li> <li><a href="user/user?type=2">返回error</a></li> </ol>
struts.xml
<struts> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/user" extends="struts-default"> <action name="user" class="com.bjsxt.struts2.user.action.UserAction"> <result>${r}</result> </action> </package> </struts>基本用不到,用不到,用不到。。。重要的事情说三遍。
4.参数传递
index.jsp<ol> <li><a href="user/user?type=1">传参数</a></li> </ol>参数type放在action context的paramters中
struts.xml
<struts> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/user" extends="struts-default"> <action name="user" class="com.bjsxt.struts2.user.action.UserAction"> <result type="redirect">/user_success.jsp?t=${type}</result> </action> </package> </struts>
struts.xml取action context中参数用 $
例子: t=${type}
UserAciton.java
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 { return "success"; } }
注意struts.xml中取参数的方法:${}
相关文章推荐
- Java线程安全和非线程安全
- spring在web.xml中的配置示例
- SSM(Spring+SpringMVC+MyBatis)框架快速整合入门教程
- java中常见的异常(转)
- Spring学习总结(2)——Spring的常用注解
- Spring学习总结(2)——Spring的常用注解
- Spring学习总结(2)——Spring的常用注解
- Spring学习总结(2)——Spring的常用注解
- Struts2 内建的验证规则
- java+poi解析excel文件
- Java之多线程内存可见性_3(synchronized和volatile比较)
- 基本排序_冒泡排序_Java实现
- java统计字符串中指定元素出现次数方法
- spring问题排查-调低日志等级
- struts学习笔记(四)
- java资料——线程(转)
- Java将数据库数据导入EXCEL
- Maven Missing artifact jdk.tools:jdk.tools:jar:1.7 解决办法
- springMVC对于controller处理方法返回值的可选类型
- Java 反射