Struts2学习(六)【参数获取】
来源:互联网 发布:rxjs 多个网络请求 编辑:程序博客网 时间:2024/06/05 15:22
一、参数获取的三种方式
我们想要获取页面的值,可以使用原生的方式获取,如果你愿意的话,当然我相信基本没人这么干。那么我们看看 Struts2 中参数是怎么获取的。有如下三种方式
属性驱动获得参数
对象驱动
模型驱动
>
二、测试共用的文件说明
我们需要完成上面三种获取参数的测试,需要书写测试代码,但是他们有些东西是共用的。下面是共用的部分。
2.1 配置文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"><struts> <package name="params" namespace="/" extends="struts-default"> <!--参数获取方式一 属性驱动获得参数--> <action name="DemoParams1Action" class="com.qwm.struts2_2.c_params.DemoParams1Action"> <result name="success">/message.jsp</result> </action> <!--参数获取方式二 对象驱动获得参数--> <action name="DemoParams2Action" class="com.qwm.struts2_2.c_params.DemoParams2Action"> <result name="success">/message.jsp</result> </action> <!--参数获取方式三 模型驱动获得参数--> <action name="DemoParams3Action" class="com.qwm.struts2_2.c_params.DemoParams3Action"> <result name="success">/message.jsp</result> </action> <!--集合获取参数测试--> <action name="DemoParams4Action" class="com.qwm.struts2_2.c_params.DemoParams4Action"> <result name="success">/message2.jsp</result> </action> </package></struts>
2.2 message.jsp
<%-- Created by IntelliJ IDEA. User: qiwenming Date: 2017/9/19 Time: 14:07 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>提交的信息</title></head><h2>${title}测试</h2>姓名:${name}<br>年龄:${age}<br>生日:${birthday}</body></html>
三、参数获取方式一 属性驱动获得参数
这种方式获取参数,我们需要在 Action中创建我和我们提交的数据名称对应的属性(有get/set方法)。然后我们在 excute方法中就可以使用这些参数了。
3.1 form1.jsp
<%-- Created by IntelliJ IDEA. User: qiwenming Date: 2017/9/19 Time: 13:54 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>参数获取</title></head><body> <form action="${pageContext.request.contextPath}/DemoParams1Action" method="post"> 用户名:<input type="text" name="name" /><br> 年龄:<input type="text" name="age" /><br> 生日:<input type="text" name="birthday" /><br> <input type="submit" value="提交" /> </form></body></html>
3.2 DemoParams1Action
package com.qwm.struts2_2.c_params;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import java.util.Date;/** * @author: wiming * @date: 2017-09-19 13:56:24 星期二 * @decription: * 参数获取方式一 属性驱动获得参数 */public class DemoParams1Action extends ActionSupport { //自动类型转换 只能转换8大基本数据类型以及对应包装类,以及Date //准备与参数键名称相同的属性 private String name; private Integer age; //支持特定类型字符串转换为Date ,例如 yyyy-MM-dd private Date birthday; @Override public String execute() throws Exception { //保存到request域中去 ActionContext.getContext().put("title",this.getClass().getSimpleName()); ActionContext.getContext().put("name",name); ActionContext.getContext().put("age",age); ActionContext.getContext().put("birthday",birthday); return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; }}
3.3 图示
四、参数获取方式二 对象驱动获得参数
这种方式获取参数主要步骤是这样的:
后台创建一个实体类这个类的属性(有get/set方法),与提交的属性名一样。
在Action 中创建一个实体类的属性(比如创建一个 user 这样的属性)
在页面中调用的时候,需要使用 Action中的实体类的属性名加上 实体类内部的属性名提交(例如上面的 提交的时候,我们需要指定为 user.name)
4.1 form.jsp
<%-- Created by IntelliJ IDEA. User: qiwenming Date: 2017/9/19 Time: 13:54 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>参数获取</title></head><body> <form action="${pageContext.request.contextPath}/DemoParams2Action" method="post"> 用户名:<input type="text" name="user.name" /><br> 年龄:<input type="text" name="user.age" /><br> 生日:<input type="text" name="user.birthday" /><br> <input type="submit" value="提交" /> </form></body></html>
4.2 DemoParams2Action
package com.qwm.struts2_2.c_params;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import java.util.Date;/** * @author: wiming * @date: 2017-09-19 14:19:22 星期二 * @decription: * 参数获取方式二 对象驱动获得参数 */public class DemoParams2Action extends ActionSupport { private User user; @Override public String execute() throws Exception { //保存到request域中去 ActionContext.getContext().put("title",this.getClass().getSimpleName()); ActionContext.getContext().put("name",user.getName()); ActionContext.getContext().put("age",user.getAge()); ActionContext.getContext().put("birthday",user.getBirthday()); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; }}
4.3 图示
五、参数获取方式三 模型驱动获得参数
这种方式获取参数主要步骤是这样的:
后台创建一个实体类这个类的属性(有get/set方法),与提交的属性名一样。
在Action 中创建一个实体类的成员变量(建一个 User user = new User() 这样的成员变量)
Action 还需要实现 ModelDriven 接口,传入的泛型为2中创建的类型(如 ModelDriven),在 getModel() 中返回上面2中创建的成员变量
在页面中调用的时候,只需要填写成员变量的属性名称就行了
5.1 form2.jsp
<%-- Created by IntelliJ IDEA. User: qiwenming Date: 2017/9/19 Time: 13:54 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>参数获取</title></head><body> <form action="${pageContext.request.contextPath}/DemoParams3Action" method="post"> 用户名:<input type="text" name="name" /><br> 年龄:<input type="text" name="age" /><br> 生日:<input type="text" name="birthday" /><br> <input type="submit" value="提交" /> </form></body></html>
5.2 DemoParams3Action
package com.qwm.struts2_2.c_params;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;/** * @author: wiming * @date: 2017-09-19 14:19:22 星期二 * @decription: * 参数获取方式三 模型驱动获得参数 */public class DemoParams3Action extends ActionSupport implements ModelDriven<User> { //准备user成员变量,必须实例化 private User user = new User(); @Override public String execute() throws Exception { //保存到request域中去 ActionContext.getContext().put("title",this.getClass().getSimpleName()); ActionContext.getContext().put("name",user.getName()); ActionContext.getContext().put("age",user.getAge()); ActionContext.getContext().put("birthday",user.getBirthday()); return SUCCESS; } @Override public User getModel() { return user; }}
5.3 图示
六、集合参数的获取
获取集合参数,在action中和我们上面使用的是一样的,主要是页面方面。
List集合说明:
使用的时候可以直接使用 action中list结合的属性名如 name=”list” 这种方式,这种方式的话,如果多次使用,那么集合中的数据一个一个的添加上上去。
也可是指定下标使用,如 name=”list[3]”,这种情况,如果前面 3 没有存东西,那么会设置为空。
Map集合说明
Map集合的使用,主要就是得指定键,比如 name=”map[test]”
6.1 form4.jsp
<%-- Created by IntelliJ IDEA. User: qiwenming Date: 2017/9/19 Time: 13:54 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>参数获取</title></head><body> <form action="${pageContext.request.contextPath}/DemoParams4Action" method="post"> list:<input type="text" name="list" /><br> list:<input type="text" name="list[3]" /><br> map:<input type="text" name="map['test']" /><br> <input type="submit" value="提交" /> </form></body></html>
6.2 DemoParams4Action
package com.qwm.struts2_2.c_params;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import java.util.List;import java.util.Map;/** * @author: wiming * @date: 2017-09-19 14:19:22 星期二 * @decription: * 集合获取参数测试 */public class DemoParams4Action extends ActionSupport{ private List<String> list; private Map<String,String> map; @Override public String execute() throws Exception { //保存到request域中去 ActionContext.getContext().put("title",this.getClass().getSimpleName()); ActionContext.getContext().put("list",list); ActionContext.getContext().put("map",map); return SUCCESS; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; }}
6.2 message2.jsp
<%-- Created by IntelliJ IDEA. User: qiwenming Date: 2017/9/19 Time: 14:07 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>提交的信息</title></head><h2>${title}测试</h2>list:${list}<br>map:${map}<br></body></html>
6.6 图示
- Struts2学习(六)【参数获取】
- Struts2学习笔记(六)获取请求处理
- Struts2学习笔记六
- struts2 中获取参数
- Struts2获取参数方法
- Struts2学习笔记之动态方法调用和参数获取
- struts2学习(10)——获取参数
- Struts2学习笔记之动态方法调用和参数获取
- Struts2学习笔记(六)
- Struts2学习(六)—国际化
- struts2获取参数,解决乱码
- Ext + struts2 获取提交参数
- struts2如何获取页面参数
- Struts2中获取请求参数
- Struts2 获取表单请求参数
- struts2获取参数的方法
- Struts2的页面参数获取
- struts2获取url参数问题
- SpringBoot项目实战(7):Filter、Listener
- mysql+mybatis+Spring
- Struts2学习(五)【访问servletAPI方式】
- MyEclipse工作空间设置
- Data URL和图片
- Struts2学习(六)【参数获取】
- eclipse PyDev,配置PyDev环境
- 简明Python教程笔记一 python3.x
- 面向对象之类与对象
- HBuilder中的“有效注释”
- SSH 代理功能
- Struts2学习(七)【OGNL基本语法】
- 使用eclipse用java调用scilab的问题
- SVN设置自动锁定(图)