首页 技术 正文
技术 2022年11月13日
0 收藏 383 点赞 3,880 浏览 1854 个字

一、结果(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中取到。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,033
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,862