Struts2(6)——Action传递参数
来源:互联网 发布:学python能做什么 编辑:程序博客网 时间:2024/06/06 00:52
1. 将参数作为Action的成员变量
在Action中给定一个私有属性,并实现其set、get访问器,然后即可直接接收请求中的参数。
package com.imm.demo;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import java.util.Map;public class Demo8Action extends ActionSupport { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String execute() throws Exception { System.out.println("name:"+name); return SUCCESS; }}
能够这样实现的原因是在于,Action是线程安全的。而Servlet时代则不可行,因为Servlet是在客户端第一次请求某个Servlet时根据web.xml实例化的,在后续无论多少个请求访问该Servlet,都不会再实例化,而是访问同一个Servlet,因此Servlet只能在方法中接收参数。
2.通过某个实体对象
创建实体类,然后创建Action的私有属性为该实体类的实例。如下:
package com.xiaoyin.demo;import com.opensymphony.xwork2.ActionSupport;import com.xiaoyin.entity.User;public class Demo9Action extends ActionSupport { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { System.out.println(user); return SUCCESS; }}
package com.xiaoyin.entity;import java.util.Date;public class User { private String name; private int age; private Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", birthday=" + birthday + '}'; }}
<%-- Created by IntelliJ IDEA. User: ASCEND Date: 2017/9/10 Time: 1:24 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %><html><head> <meta http-equiv="Content-Type" content="text/html" ;charset="utf-8"/> <title>hello</title></head><body><h1>hello</h1><form action="${pageContext.request.contextPath}/Demo9Action.action" method="get"> 名字:<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>
3.模型驱动传参
创建实体类的实例作为Action的成员变量,Action实现ModelDriven
接口,并重写该接口方法,成员对象须被初始化,以免重写的方法返回null值。
package com.xiaoyin.demo;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.xiaoyin.entity.User;public class Demo10Action extends ActionSupport implements ModelDriven<User> { private User user=new User(); @Override public String execute() throws Exception { System.out.println(user); return SUCCESS; } public User getModel() { return user; }}
<%-- Created by IntelliJ IDEA. User: ASCEND Date: 2017/9/10 Time: 1:24 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %><html><head> <meta http-equiv="Content-Type" content="text/html" ;charset="utf-8"/> <title>hello</title></head><body><h1>hello</h1><form action="${pageContext.request.contextPath}/Demo10Action.action" method="get"> 名字:<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>
4.集合类型的参数封装
import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.xiaoyin.entity.User;import java.util.List;public class Demo11Action extends ActionSupport { private List<String> list; public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } @Override public String execute() throws Exception { System.out.println("list:"+list); return SUCCESS; }}
将集合类型作为Action的一个属性,然后jsp页面
<%-- Created by IntelliJ IDEA. User: ASCEND Date: 2017/9/10 Time: 1:24 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %><html><head> <meta http-equiv="Content-Type" content="text/html" ;charset="utf-8"/> <title>hello</title></head><body><h1></h1><form action="${pageContext.request.contextPath}/Demo11Action.action" method="post"> 名字:<input type="text" name="list"><br/> 年龄:<input type="text" name="list[1]"><br/> 地址:<input type="text" name="list[3]"><br/> <input type="submit" value="提交"></form></body></html>
如果提交Map类型的数据,那么jsp页面表单input的name应该是
name=属性名['键']
阅读全文
0 0
- Struts2(6)——Action传递参数
- struts2 action之间传递参数
- struts2.3.4 Action传递参数
- struts2 redirect-action 传递参数问题
- struts2 redirect-action 传递参数问题
- Struts2中Action间参数传递
- struts2中redirect action的参数传递
- Struts2 action与jsp参数传递机制
- struts2的action之间的参数传递
- Struts2中action传递时间参数
- Struts2中Action间参数传递
- Struts2 action之间相互跳转传递参数
- Struts2 action重定向如何传递参数
- Struts2中Action接收传递的参数
- struts2中Action参数传递之DTO
- Struts2 action之间相互跳转传递参数
- struts2中使用action属性传递参数
- Struts2 action的参数传递方式
- 你应该知道的JDK知识
- NLP中的并行分词方法
- 【oracle】简单查询
- 脑袋里是浆糊时的c++程序(我一定是傻了)
- JS代码
- Struts2(6)——Action传递参数
- ADM打不开/data,或打开后无法导出里面的文件
- ACM 括号匹配深度
- 【LeetCode】125、Valid Palindrome
- 移动端踩坑之旅-ios下fixed、软键盘相关问题总结
- ajax跨域传递cookie,验证登录
- 1.虚拟机
- intelliJ idea 导入eclipse/myeclipse web project
- ubuntu16 caffe安装+测试(cpu)