struts2教程(3)--请求参数处理
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教程6:在Action类中获得HttpServletResponse对象的四种方法
在struts1.x Action类的execute方法中,有四个参数,其中两个就是response和request.而在Struts2中,并没有任何参数,因此,就不能简单地从execute方法获得H ...
- struts2教程(5)--请求参数校验
一.请求参数校验介绍 校验的分类 :客户端数据校验 和服务器端数据校验 客户端数据校验 ,通过JavaScript完成校验 (改善用户体验,使用户减少出错 ) 服务器数据校验 ,使用框架内置校验功能( ...
- struts2之请求参数接收
struts2之请求参数接收 1. 采用基本类型接受请求参数(get/post) 在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名的属性. 请求路径:http ...
- Spring Boot 2.x基础教程:JSR-303实现请求参数校验
点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 作者 | 翟永超 来源 | didispace.com/spring-boot-learni ...
- Struts2请求参数合法性校验机制
在Action中通过代码执行数据校验 请求参数的输入校验途径一般分两种:客户端校验 :通过JavaScript 完成 (jquery validation插件),目的:过滤正常用户的误操作. 服务器校 ...
- 十一、Struts2封装请求参数的方式
十一.Struts2封装请求参数的方式 方式一.Action 本身作为model对象,通过成员setter封装(一个名字为params的拦截器干的) 注意:表单中的名称要和动作类中的名称一致(这是必须 ...
- struts2获取请求参数的三种方式及传递给JSP参数的方式
接上一篇文章 package test;import com.opensymphony.xwork2.ActionSupport; import javax.servlet.http.*; impor ...
- Struts2的第二天(Action接收请求参数)
Struts2的第二天 Struts2的第二天的内容 1. Struts2框架中的Servlet的API的使用 2. Struts2中Action接收请求参数 3. Struts2中自定义拦截器 案例 ...
- Struts2之Action接收请求参数和拦截器
技术分析之在Struts2框架中使用Servlet的API 1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据, ...
最新文章
- 计算机专业学位英语试题,(荐)2015年最新电大学位英语考试精篇复习资料 小抄版.doc...
- ubuntu16.04 nginx安装
- Java不可变类作为参数传递遇到的坑
- ds1302典型应用原理图_不同类型的光纤激光器,在工业中有哪些典型应用
- bic,orr——设置某些位为0或者1
- Intellj IDEA 注册码 2018
- 小红书回应泄露未成年人隐私及审核漏放
- linux 配置root环境变量,root用户Linux 环境变量的配置解决(-bash: jps: command not found)有关问题...
- ubuntu 1610本地安装wordpress
- 【Codeforces 851D Arpa and a list of numbers】
- PAT (Basic Level) Practice1001 害死人不偿命的(3n+1)猜想
- H3C Hybrid实验
- Visual Leak Detector 2.2.3 Visual C++内存检测工具
- java隐式参数的作用_隐式参数_scala教程_田守枝Java技术博客
- C# Datatable的Select方法
- 在北京如何租房比较好?
- 计算机蓝屏原因原理,蓝屏炸弹
- C语言 Fibonacci数列(递推)
- 实用供暖通风空调设计手册 第三版_从设计到施工,设计师必知的工艺材料知识都在这里!...
- 如何测身高未来身高计算机,第2课 预测身高——编程解决问题的基本步骤