struts2教程(3)--请求参数处理
来源:互联网 发布:岳游网络千炮捕鱼ol1.1 编辑:程序博客网 时间:2024/05/02 01:49
Action处理请求参数
struts2 和 MVC定义关系
StrutsPrepareAndExecuteFilter : 控制器
JSP : 视图
Action : 可以作为模型,也可以是控制器
struts2 Action 接受请求参数 :属性驱动 和 模型驱动
一、Action处理请求参数三种方式
第一种 :Action 本身作为model对象,通过成员setter封装 (属性驱动 )
页面:
用户名 <input type="text" name="username" /> <br/>
Action :
public class RegistAction1 extends ActionSupport {private String username;public void setUsername(String username) {this.username = username;}}
问题一: Action封装数据,会不会有线程问题 ?
* struts2 Action 是多实例 ,为了在Action封装数据 (struts1 Action是单例的 )
问题二: 在使用第一种数据封装方式,数据封装到Action属性中,不可能将Action对象传递给 业务层
* 需要再定义单独JavaBean,将Action属性封装到JavaBean
第二种 :创建独立model对象,页面通过ognl表达式封装 (属性驱动)
页面:
用户名 <input type="text" name="user.username" /> <br/> ----- 基于OGNL表达式的写法
Action:
public class RegistAction2 extends ActionSupport {private User user;public void setUser(User user) {this.user = user;} public User getUser() {return user;}}
问题: 谁来完成的参数封装
<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
第三种 :使用ModelDriven接口,对请求数据进行封装 (模型驱动 )-----主流
页面:
用户名 <input type="text" name="username" /> <br/>
Action :
public class RegistAction3 extends ActionSupport implements ModelDriven<User> {private User user = new User(); // 必须手动实例化public User getModel() {return user;}}
struts2 有很多围绕模型驱动的特性
<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>为模型驱动提供了更多特性
总结:
对比第二种、第三种 :第三种只能在Action中指定一个model对象,第二种可以在Action中定义多个model对象
<input type="text" name="user.username" />
<input type="text" name="product.info" />
二、封装数据到Collection和Map
1) 封装数据到Collection对象
页面:
产品名称 <input type="text" name="products[0].name" /><br/>
Action :
public class ProductAction extends ActionSupport {private List<Product> products;public List<Product> getProducts() {return products;} public void setProducts(List<Product> products) {this.products = products;}}
2) 封装数据到Map对象
页面:
产品名称 <input type="text" name="map['one'].name" /><br/> ======= one是map的键值
Action :
public class ProductAction2 extends ActionSupport {private Map<String, Product> map; public Map<String, Product> getMap() {return map;} public void setMap(Map<String, Product> map) {this.map = map;}}
- struts2教程(3)--请求参数处理
- struts2请求参数处理
- struts2教程(5)--请求参数校验
- struts2-Action处理请求参数
- struts2教程:11、请求参数接收
- struts2中请求包含参数如何处理
- struts2教程:7、指定Struts2处理的请求后缀
- Struts1.x和Struts2在处理请求参数上区别
- Struts2学习笔记(七)——Action处理请求参数
- struts2 处理请求流程分析(结合源码)3
- struts2 处理请求流程分析(结合源码)3
- Struts2接受请求参数
- struts2 请求参数接收
- Struts2接收请求参数
- Struts2 请求参数接收
- Struts2 封装请求参数
- Struts2---请求参数校验
- struts2-请求参数校验
- mysql远程连接访问设置
- CAD(dxf)、SHP、KML数据加载并套合到卫星图像上(坐标校准)
- 2017-4-18 去哪儿 南京面试(一面 挂)
- div块的overflow属性,自动为溢出内容区的内容增加滚动条
- bootstrap学习总结-css样式设计(二)
- struts2教程(3)--请求参数处理
- ios 给xcode工程关联pod
- 为什么机器学习中, 要假设我们的数据是独立同分布的
- graham求凸包——poj1133Wall
- 1012. 数字分类 (20)
- 在TX1上安装RPLidar及其驱动
- 关于UISearchDisplayController的一个坑
- jdk、jre及jvm的关系
- C++ Primer Chapter 5