struts2结果(Result)
一、结果(result)类型
result的type属性默认为dispatcher,其他常见的属性有redirect\chain\redirectAction
<action name="a1">
<result type="dispatcher">
/a1.jsp
</result>
</action>
<action name="a2">
<result type="redirect">
/a2.jsp
</result>
</action>
<action name="a3">
<result type="chain">
a1
</result>
</action>
<action name="a4">
<result type="chain">
<param name="actionName">a1</param>
<param name="namespace">/</param>
</result>
</action>
<action name="a5">
<result type="redirectAction">
a1
</result>
</action>
dispatcher就是forward到一个jsp页面
redirect就是跳转到一个jsp页面
chain就是forward到一个action,跳转到另外一个package用第四种方式
redirectAction就是跳转到一个action
二、全局结果集
在配置struts2.xml时有时候会遇到一个问题即多个action的result是相同的这时候就没有必要为每个action配一个result,只要配置一个global result即可:
<package name="index" namespace="/" extends="struts-default">
<global-results>
<result name="msg">/msg.jsp</result>
</global-results>
</package>
<package name="user" namespace="/user" extends="index"> </package>
这样当index包和user包下的action返回msg的时候就会forward到/msg.jsp。(extends="index"从index包继承)
三、动态结果集
在struts.xml中配置:
<action name="login" class="cn.orlion.user.UserAction" method="login">
<result>
${r}
</result>
</action>
UserAction:
package cn.orlion.user;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private int type;
private String r;
public String login(){
if (1 == type) r = "/a1.jsp";
else if (2 == type) r = "/a2.jsp";
return "success";
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getR() {
return r;
}
public void setR(String r) {
this.r = r;
}
}
这样当访问http://localhost:8080/Struts2Demo/user/login.action?type=1时就会看到a1.jsp
访问...?type=2时就会看到a2.jsp了
struts.xml中${r} 实际是从Value Stack中取出r的值
四、结果集传参数
当访问.../login.jsp?uid=test时通过< s:property value="uid" />是取不到的因为uid是个参数不会放到Value Stack中而是放到Stack Context中,可以通过<s:property value="#parameters.uid" />娶到。
当访问一个action(在这个action里赋值给了uid)时,这个action的结果:login.jsp(即forward到 /login.jsp)可以用<s:property value="uid" />取到值,这是因为forward可以从Value Stack中取到。
struts2结果(Result)的更多相关文章
- Struts2 配置文件result的name属性和type属性
Struts2 配置文件result的name属性和type属性:Name属性SUCCESS:Action正确的执行完成,返回相应的视图,success是 name属性的默认值: NONE:表示Act ...
- Struts2 中result type属性说明
Struts2 中result type属性说明 首先看一下在struts-default.xml中对于result-type的定义: <result-types><result-t ...
- Struts2之Result详解
上一篇我们把Struts2中的Action接收参数的内容为大家介绍了,本篇我们就一起来简单学习一下Action的4种Result type类型,分为:dispatcher(服务端页面跳转):redir ...
- struts2 action result type类型
struts2 action result type类型 1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony. ...
- struts2的@Result annotation 如何添加params,并且在页面取值
http://www.bubuko.com/infodetail-2492575.html .............................................. 标签:lai ...
- Struts2配置Result(Struts2_result)
一.概要 二.常用四种类型的配置 Struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!D ...
- 【struts2】Result和ResultType
简单的说,Result是Action执行完后返回的一个字符串,它指示了Action执行完成后,下一个页面在哪里.Result仅仅是个字符串,仅仅是用来指示下一个页面的,那么如何才能够到达下一个页面呢? ...
- struts2的result的type属性
一共有两个属性name和type name这里就不介绍了 type 返回结果的类型,值可以从default-struts.properties中看到看到 常用的值:dispatcher (默认) ...
- Struts2中 Result类型配置详解
一个result代表了一个可能的输出.当Action类的方法执行完成时,它返回一个字符串类型的结果码,框架根据这个结果码选择对应的result,向用户输出.在com.opensymphony.xwor ...
- Struts2 自定义Result
注意:我只要是解决自定义返回Json 和异常处理问题 新建一个类 AjaxResult 继承 StrutsResultSupport 看看代码吧 public class AjaxResult e ...
随机推荐
- 各种UIButton
前几日尝试了各种UIButton UIButton *btn0=[UIButton buttonWithType:UIButtonTypeContactAdd]; UIButton *btn1=[U ...
- H264与RTP
http://blog.163.com/laorenyuhai126@126/blog/static/1935077920111218152989/
- WIN8 隐私声明
隐私权声明 本应用连接网络仅为控制硬件设备,不会收集你的个人信息,也不共享你个个人信息. 应用名称 CrossMedia可视化控制系统(服务器版) 关于本应用 本应仅为控制设备应用,不关注任何配置相关 ...
- CSS浮动(float,clear)通俗讲解
首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流.如下图: 可以看出,即使div1的宽度很小,页面中一行可以容下div1和div2,div2也不会排在div1后边,因为d ...
- wampserver 2.5 首页链接问题,wampserver Your Projects
在wampserver 2.5之后,在首页的Your projects里面,链接的地址为http://直接加你的系统名称,会导致无法访问, 正常的应该为http://localhost+你的系统名称, ...
- 《与mysql零距离接触》视屏学习笔记
1.数据表的增删改查操作(crud): 对于表: 增:create table XXXX 删:drop table XXXX 改:alter table XXXX rename to XXXX 查 ...
- maven配置发布仓库
首先,在工程的pom.xml中添加仓库信息 <distributionManagement> <repository> <id>releases</id> ...
- 原生Ajax封装随笔
XMLHttpRequest 对象用于和服务器交换数据.我们使用 XMLHttpRequest 对象的 open() 和 send() 方法: open(method,url,async) metho ...
- 浅论Android网络请求库——android-async-http
在iOS开发中有大名鼎鼎的ASIHttpRequest库,用来处理网络请求操作,今天要介绍的是一个在Android上同样强大的网络请求库android-async-http,目前非常火的应用Insta ...
- Websocket全讲解。跨平台的通讯协议 !!基于websocket的高并发即时通讯服务器开发。
本博文,保证不用装B的话语和太多专业的语言,保证简单易懂,只要懂JAVAEE开发的人都可以看懂. 本博文发表目的是,目前网上针对Websocket的资料太散乱,导致初学者的知识体系零零散散,学习困难加 ...