【Struts2】(3)参数传递
2015-11-30 15:02
447 查看
一. 转发类型
在Struts源码struts2-core包中,有一个struts-default.xml文件,里面写了几种转发类型:<package name="struts-default" abstract="true"> <result-types> <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" /> </result-types>
1. dispatcher
如果我们什么都不写,就默认是使用dispatcher转发的,最常见的一种用法。2. chain
使用chain表示将结果由服务器进行转发给Action,配置如下:<action name="simple" class="com.thr.action.SimpleAction"> <result name="hello">/hello.jsp</result> <result name="add_input" type="chain">index</result> <result name="add_success">/success.jsp</result> <result name="update_input">/update_input.jsp</result> <result name="update_success">/success.jsp</result> <result name="delete_success">/success.jsp</result> <result name="other_action" type="chain">index</result> </action> <action name="index"> <result>/index.jsp</result> </action>然后在Action中添加一个方法返回"other_action"即可。
如果使用chain进行转发,Action中的数据不会丢失。
3. redirect
客户端转发给视图。使用这个转发的时候,会丢失在Action中保存的数据。例如:
<result name="add_input" type="dispatcher">/add_input.jsp</result> <result name="add_success" type="redirect">/success.jsp</result>如果这样子配置,我们就无法读取到保存的username和password了。
4. redirectAction
客户端转发给Action。使用这个转发的时候,也会丢失在Action中保存的数据。例如:
<action name="simple" class="com.thr.action.SimpleAction"> <result name="hello">/hello.jsp</result> <result name="add_input" type="dispatcher">/add_input.jsp</result> <result name="add_success" type="redirect">/success.jsp</result> <result name="update_input">/update_input.jsp</result> <result name="update_success" type="redirectAction">index</result> <result name="delete_success">/success.jsp</result> <result name="other_action" type="chain">index</result> </action> <action name="index"> <result>/index.jsp</result> </action>update_input.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>更新界面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <br> <form action="/web/test/simple!update.action" method="post"> UserName:<input name="username" /><br /> Password:<input name="password" type="password" /><br /> <input type="submit" value="提交" /> </form> </body> </html>在我们提交更新的时候,保存的数据不会显示到index.jsp当中。但是如果我们更改上面为:
<result name="update_success" type="chain">index</result>数据就可以传过去。
5. plainText
将视图jsp源码转发给客户端。用于测试我们可以在success.jsp中输出传入过来的值:
<br /> UserName${username} <br /> Password${password }
二. 不同包之间的转发
如果我们定义了2个以上的package,在它们之间是如何转发的。我们需要修改配置文件如下:
<package name="basicstruts2" extends="struts-default" namespace="/test"> <action name="simple" class="com.thr.action.SimpleAction"> <result name="hello">/hello.jsp</result> <result name="add_input" type="dispatcher">/add_input.jsp</result> <result name="add_success" type="redirect">/success.jsp</result> <result name="update_input">/update_input.jsp</result> <result name="update_success" type="chain"> <param name="namespace">/test2</param> <param name="actionName">index2</param> </result> <result name="delete_success">/success.jsp</result> <result name="other_action" type="chain">index </result> </action> <action name="index"> <result>/index.jsp</result> </action> </package> <package name="mystruts" extends="struts-default" namespace="/test2"> <action name="index2"> <result>/index2.jsp</result> </action> </package>我们在update_success时,使用了chain转发到命名空间为/test2并且actionName为index2的Action,这样就实现了不同包之间的转发。
三. 参数传递
我们首先创建也表单:测试表单 <br /> <form action="/web/test/form.action" method="post"> UserName:<input name="username"><br /> Password:<input name="password" type="password"><br /> Age:<input name="age"><br /> Long:<input name="l" /><br /> Boolean:<input name="b" /><br /> Double:<input name="d" /><br /> <input type="submit" name="method:test" value="提交"> </form>再创建一个Action类,接受数据:
package com.thr.action; public class UserAction { private String username; private String password; private int age; private long l; private boolean b; private double d; public String test() { return "success"; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public long getL() { return l; } public void setL(long l) { this.l = l; } public boolean isB() { return b; } public void setB(boolean b) { this.b = b; } public double getD() { return d; } public void setD(double d) { this.d = d; } }
修改strust文件:
<package name="basicstruts2" extends="struts-default" namespace="/test"> <action name="user" class="com.thr.action.UserAction"> <result>/success.jsp</result> </action> </package>最后用一个成功页面输出接收到的值:
提交成功! <br /> UserName:${username } <br /> Password:${password } <br /> Age:${age } <br /> Long:${l } <br /> Boolean:${b } <br /> Double:${d }输入数据,测试:
但是,这里要注意的是,当我们什么都不写会出现什么情况呢?
可以看出,报错了,这是因为在传递int或者long的时候,没有一个对应空值的处理,于是报错了,但是double和boolean都有对应的空值处理,这里有两个解决方法:
第一个方法是在表单中默认给出对应的默认值,这样在传递的时候就不会有空值出现了。
第二个方法是将之前我们Action中的属性都改为对应的包装类:
private String username; private String password; private Integer age; private Long l; private boolean b; private double d;修改对应的set和get方法,测试什么都不输入:
这下可以看到Age和Long的值都成空了!后台也不会报错了。
注意这里有一个知识,每一次请求都是对应的不同的action。
四. 参数传递的保存
如果我们在提交的时候,对过来的数据进行验证:if ("Jerry".equals(username) && "111".equals(password)) { return "success"; } else { return "form"; }struts配置:
<action name="user" class="com.thr.action.UserAction"> <result>/success.jsp</result> <result name="form">/form.jsp</result> </action>当表单数据没有验证通过的时候,我们需要对刚才填写的表单进行保存,那么我们需要在value属性加一些东西:
<form action="/web/test/user.action" method="post"> UserName:<input name="username" value="${username}"><br /> Password:<input name="password" type="password" value="${assword}"><br /> Age:<input name="age" value="<s:property value="age"/>"><br /> Long:<input name="l" value="<s:property value="l"/>"><br /> Boolean:<input name="b" value="<s:property value="b"/>"><br /> Double:<input name="d" value="<s:property value="d"/>"><br /> <input type="submit" name="method:test" value="提交"> </form>这里有两种方法:
1. 使用el表达式value="${username}"保存数据。
2. 使用strust的taglib保存,使用这个属性必须在jsp加入:
<%@taglib prefix="s" uri="/struts-tags" %>
五. 几种参数传递方式
1. ActionContext传递参数
我们更新一个update方法,在里面写号定义的值,这里使用了ActionContext对象的getContext方法:public String update() { username = "哈哈"; password = "123"; Date modify = new Date(); ActionContext.getContext().put("modify", modify); return "update_success"; }在jsp页面上使用
<br /> Age:<s:property value="#modify"/>来获取我们传入的值:
<br /> UserName:<s:property value="username"/> <br /> Password:<s:property value="password"/> <br /> Age:<s:property value="#modify"/>注意:如果是使用ActionContext中的属性时,需要在属性值前面加#来访问(不加只是容错,强烈不推荐)。
2. ServletActionContext获取Servlet API传递参数
接下来看怎么使用ServletActionContext传递参数:// 通过ServletActionContext传递数据 ServletActionContext.getRequest() .setAttribute("birthday", "2015-11-30"); ServletActionContext.getRequest().getSession() .setAttribute("fav", "篮球"); ServletActionContext.getServletContext().setAttribute("count", 1000);在jsp段获取数据:
访问ServletAPI中的数据<br /> 访问request中的数据<br /> 生日<s:property value="#request.birthday"/><br /> 访问session中的数据<br /> 爱好<s:property value="#session.fav"/><br /> 访问application中的数据<br /> 访问量<s:property value="#application.count"/><br />
3. ActionContext存取Servlet API当中的数据
存数据:// 通过ActionContext存取Session和Application当中的数据 ActionContext.getContext().getSession().put("home", "西安"); ActionContext.getContext().getApplication().put("company", "北京");取数据:
通过ActionContext访问Session和Application对象<br /> 家庭<s:property value="#session.home"/><br /> 公司<s:property value="#application.company"/><br />这里获取数据还有一种写法:
家庭<s:property value="#session['home']"/><br /> 公司<s:property value="#application['company']"/><br />也是同样可以获得数据的。
这里ActionContext和request的生命周期实际上是一样的。
其实只要属性没有重名的,都可以使用attr来获取值:
通过attr属性访问request, session和application当中的属性<br /> 生日<s:property value="#attr.birthday"/><br /> 爱好<s:property value="#attr.fav"/><br /> 访问量<s:property value="#attr.count"/><br /> 家庭<s:property value="#attr['home']"/><br />
相关文章推荐
- Java中 instanceof 关键字
- JavaEE_Mybatis_SpringMVC_Mybaits_lesson6__MyBatis传递pojo对象实现综合查询
- IO概述
- json、javaBean、xml互转的几种工具介绍
- eclipse以外关闭,打开闪退
- 我为什么没有分享Java学习方法
- 如何在SpringMVC中获取request对象
- SSH框架之jar包讲解
- Spring整合mq
- Eclipse 链接 微软安卓模拟器(windowsAndroid)
- Drools5集成Spring3
- java向上取整函数Math.ceil()
- myeclipse 8.5利用phonegap开发android程序
- SpringMvc快速入门
- 在Spring MVC中,事务可以加在Controller层
- eclipse设置svn代理
- Spring事务配置的五种方式
- 关于eclipse svn插件的 svn:ignore无法使用的问题
- java位运算
- 学习:java设计模式—Observer模式