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 ...
随机推荐
- P1906联合权值
描述 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 WiWi, 每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 v 点的最短距离. ...
- ProceedingJoinPoint获取当前方法
aspectJ切面通过ProceedingJoinPoint想要获取当前执行的方法: 错误方法: Signature s = pjp.getSignature(); MethodSignatu ...
- 编译系统中BNF: Backus-Naur Form
巴科斯范式(BNF: Backus-Naur Form 的缩写)是由 John Backus 和 Peter Naur 首次引入一种形式化符号来描述给定语言的语法. 简称为:BNF符号. 现在,几乎每 ...
- CSS中强大的EM
(转)作者:dearjohn ,发布于2012-7-31 http://www.uml.org.cn/html/201207311.asp 使用CSS也好久了,但一直都是在使用“px”来设置Web元素 ...
- 原来在linux上切换jdk的版本是这么简单
上次在linux上切换jdk版本的时候,还配置了半天的环境变量,今天又查了一下,原来是这么的简单 1. 查看相应的jdk是否在 ubuntu的jdk菜单里,查看: (输全哦) update-alter ...
- Ubuntu16.04使用阿里云镜像安装Mongodb
一.概述 近日要在新的Ubuntu16.04系统上安装MongoDB,某度结果后直接从Mongo官网直接获得3.2版本的下载链接,结果在下载时发觉速度慢的可怜.迫于无奈,只能找国内的镜像下载.切换国内 ...
- SQLSERVER2008新增的审核/审计功能
SQLSERVER2008新增的审核/审计功能 很多时候我们都需要对数据库或者数据库服务器实例进行审核/审计 例如对失败的登录次数进行审计,某个数据库上的DDL语句进行审计,某个数据库表里面的dele ...
- .NET转Java学习规则
做一个个人Blog(博客) 过程分3个大阶段: 使用servlet/jsp和jdbc,mysql完成第一阶段Blog 项目修改为Spring(IOC+MVC)+SpringJdbc+MyBatis模式 ...
- 【转】MySQL性能优化的21个最佳实践
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...
- guava之Joiner 和 Splitter
最近在给客户准备一个Guava的分享,所以会陆续的更新关于Guava更多的细节分享.本文将记录Guava中得字符串处理Joiner(连接)和Splitter(分割)处理. Joiner 首先我们来看看 ...