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 ...
随机推荐
- 【转】浅谈html5网页内嵌视频
转自 http://www.pchou.info/web/2014/01/30/52ea01e13a7f1.html
- 百度音乐api
百度音乐全接口 会利用使用接口找歌简单又快捷 http://tingapi.ting.baidu.com/v1/restserver/ting 获取方式:GET 参数:format=json或xml& ...
- Refresh recovery area usage data after manually deleting files under recovery area
Original source: http://www.dba-oracle.com/t_v$_flash_recovery_area.htm If you manually delete files ...
- 【转】【Linux】 临界区,互斥量,信号量,事件的区别
原文地址:http://blog.itpub.net/10697500/viewspace-612045/ Linux中 四种进程或线程同步互斥的控制方法1.临界区:通过对多线程的串行化来访问公共资源 ...
- CSS中继承,特殊性,层叠与重要性
继承 CSS的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代.比如下面代码: <html><head> ...
- 用canvas制作酷炫射击游戏--part1
好久没写博客了,因为过年后一直在学游戏制作方面的知识.学得差不多后又花了3个月时间做了个作品出来,现在正拿着这个作品找工作. 作品地址:https://betasu.github.io/Crimonl ...
- c++的转换
1.静态转换 static_cast 用于明确定义的变换 ,包括 编译器允许的非强制转换和不太安全但定义清楚的变换.ps:(非强制变换,窄化变换,隐式转换,类层次静态定位,void*强制转换) 2.常 ...
- HtmlAgilityPack中通过sibling才能得到对应的InnerText和form,option等tag的子节点
[背景] 之前使用HtmlAgilityPack期间,遇到了2个bug: 1. InnerText没有包含对应字符串(但是用NextSibling.InnerText却可以得到) 对于html: ? ...
- Nodejs学习路线图
前言 用Nodejs已经1年有余,陆陆续续写了48篇关于Nodejs的博客文章,用过的包有上百个.和所有人一样,我也从Web开发开始,然后到包管 理,再到应用系统的开发,最后开源自己的Nodejs项目 ...
- 【转载】关于.NET里的内存泄漏
所谓内存泄露就是指一个不再被程序使用的对象或变量一直被占据在内存中..Net 中有垃圾回收机制,它可以保证一对象不再被引用的时候,即对象编程了孤儿的时候,对象将自动被垃圾回收器从内存中清除掉.虽然.N ...