1 struts2 获得参数 1-属性驱动获得参数

1 Demo8Action

  1. package www.test.c_param;
  2.  
  3. import java.util.Date;
  4.  
  5. import com.opensymphony.xwork2.ActionContext;
  6. import com.opensymphony.xwork2.ActionSupport;
  7.  
  8. //struts2获得参数的方式1-属性驱动获得参数
  9. public class Demo8Action extends ActionSupport {
  10.  
  11. //每次请求Action时都会创建新的Action实例对象
  12. public Demo8Action() {
  13. super();
  14. System.out.println("demo8Action被创建了!");
  15. }
  16.  
  17. public String execute() throws Exception {
  18. ActionContext actionContext = ActionContext.getContext();
  19. actionContext.put("username", username);
  20. actionContext.put("age", age);
  21. actionContext.put("birthday", birthday);
  22. return SUCCESS;
  23. }
  24.  
  25. //准备与参数键名称相同的属性
  26. private String username;
  27.  
  28. //自动类型转换 只能转换8大基本数据类型以及对应包装类
  29. private Integer age;
  30.  
  31. //支持特定类型字符串转换为Date ,例如 yyyy-MM-dd
  32. private Date birthday;
  33.  
  34. public Integer getAge() {
  35. return age;
  36. }
  37. public void setAge(Integer age) {
  38. this.age = age;
  39. }
  40. public Date getBirthday() {
  41. return birthday;
  42. }
  43. public void setBirthday(Date birthday) {
  44. this.birthday = birthday;
  45. }
  46. public String getUsername() {
  47. return username;
  48. }
  49. public void setUsername(String username) {
  50. this.username = username;
  51. }
  52.  
  53. }

2 struts.xml

  1. <action name="Demo8Action" class="www.test.c_param.Demo8Action" method="execute">
  2. <result name="success" type="dispatcher">/regist.jsp</result>
  3. </action>

3 form1.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="${pageContext.request.contextPath }/Demo8Action" method="post">
  11. 用户名:<input type="text" name="username"><br/>
  12. 年龄:<input type="text" name="age"><br/>
  13. 生日:<input type="text" name="birthday"><br/>
  14. <input type="submit" value="注册">
  15. </form>
  16. </body>
  17. </html>

4 regist.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. 用户名:${requestScope.username }<br/>
  11. 年龄:${requestScope.age }<br/>
  12. 出生日期:${requestScope.birthday }<br/>
  13. </body>
  14. </html>

2 struts2 获得参数 2-对象驱动

1 User类

  1. package www.test.domain;
  2.  
  3. import java.util.Date;
  4.  
  5. public class User {
  6.  
  7. private String username;
  8. private Integer age;
  9. private Date birthday;
  10. public String getUsername() {
  11. return username;
  12. }
  13. public void setUsername(String username) {
  14. this.username = username;
  15. }
  16. public Integer getAge() {
  17. return age;
  18. }
  19. public void setAge(Integer age) {
  20. this.age = age;
  21. }
  22. public Date getBirthday() {
  23. return birthday;
  24. }
  25. public void setBirthday(Date birthday) {
  26. this.birthday = birthday;
  27. }
  28. @Override
  29. public String toString() {
  30. return "User [username=" + username + ", age=" + age + ", birthday=" + birthday + "]";
  31. }
  32.  
  33. }

2 Demo9Action

  1. package www.test.c_param;
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;
  4.  
  5. import www.test.domain.User;
  6.  
  7. //struts2获得参数的方式1-属性驱动获得参数
  8. public class Demo9Action extends ActionSupport {
  9.  
  10. //准备user对象
  11. private User user;
  12. public String execute() throws Exception {
  13. System.out.println(user);
  14. return SUCCESS;
  15. }
  16.  
  17. public User getUser() {
  18. return user;
  19. }
  20. public void setUser(User user) {
  21. this.user = user;
  22. }
  23.  
  24. }

3 struts.xml

  1. <action name="Demo9Action" class="www.test.c_param.Demo9Action" method="execute">
  2. <result name="success" type="dispatcher">/form2.jsp</result>
  3. </action>

4 form2.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="${pageContext.request.contextPath }/Demo9Action" method="post">
  11. 用户名:<input type="text" name="user.username"><br/>
  12. 年龄:<input type="text" name="user.age"><br/>
  13. 生日:<input type="text" name="user.birthday"><br/>
  14. <input type="submit" value="注册">
  15. </form>
  16. </body>
  17. </html>

3 struts2 获得参数 3-模型驱动

1 Demo10Action

  1. package www.test.c_param;
  2.  
  3. import com.opensymphony.xwork2.ActionContext;
  4. import com.opensymphony.xwork2.ActionSupport;
  5. import com.opensymphony.xwork2.ModelDriven;
  6.  
  7. import www.test.domain.User;
  8.  
  9. //struts2 获得参数 3-模型驱动
  10. public class Demo10Action extends ActionSupport implements ModelDriven<User> {
  11.  
  12. // User对象
  13. private User user = new User();
  14.  
  15. public String execute() throws Exception {
  16. ActionContext context = ActionContext.getContext();
  17. context.put("username", user.getUsername());
  18. context.put("age", user.getAge());
  19. context.put("username", user.getBirthday());
  20. return SUCCESS;
  21. }
  22.  
  23. @Override
  24. public User getModel() {
  25.  
  26. return user;
  27. }
  28.  
  29. }

2 struts.xml

  1. <action name="Demo10Action" class="www.test.c_param.Demo10Action" method="execute">
  2. <result name="success" type="dispatcher">/regist.jsp</result>
  3. </action>

3 form3.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>struts2获得参数方式3-模型驱动</title>
  8. </head>
  9. <body>
  10. <form action="${pageContext.request.contextPath }/Demo8Action" method="post">
  11. 用户名:<input type="text" name="username"><br/>
  12. 年龄:<input type="text" name="age"><br/>
  13. 生日:<input type="text" name="birthday"><br/>
  14. <input type="submit" value="注册">
  15. </form>
  16. </body>
  17. </html>

4 struts2 获得参数方式-集合类型封装

1 Demo11Action

  1. package www.test.c_param;
  2.  
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import com.opensymphony.xwork2.ActionSupport;
  7.  
  8. //struts2 封装集合类型参数
  9. public class Demo11Action extends ActionSupport {
  10. //list
  11. private List list;
  12. //map
  13. private Map<String,String> map;
  14.  
  15. public String execute() throws Exception {
  16. //list.clear();
  17. System.out.println(list);
  18. System.out.println(map);
  19. return SUCCESS;
  20. }
  21.  
  22. public List getList() {
  23. return list;
  24. }
  25. public void setList(List list) {
  26. this.list = list;
  27. }
  28. public Map<String, String> getMap() {
  29. return map;
  30. }
  31. public void setMap(Map<String, String> map) {
  32. this.map = map;
  33. }
  34. }

2 struts.xml

  1. <action name="Demo11Action" class="www.test.c_param.Demo11Action" method="execute">
  2. <result name="success" type="dispatcher">/form4.jsp</result>
  3. </action>

3 form4.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>struts2 获得参数方式-集合类型封装</title>
  8. </head>
  9. <body>
  10. <form action="${pageContext.request.contextPath }/Demo11Action" method="post">
  11. list:<input type="text" name="list"><br/>
  12. list:<input type="text" name="list[5]"><br/>
  13. map:<input type="text" name="map['name']"><br/>
  14. <input type="submit" value="提交">
  15. </form>
  16. </body>
  17. </html>

04-struts2获得参数的更多相关文章

  1. Struts 2.3.24源码解析+Struts2拦截参数,处理请求,返回到前台过程详析

    Struts2官网:http://struts.apache.org/ 目前最新版本:Struts 2.3.24 Struts1已经完全被淘汰了,而Struts2是借鉴了webwork的设计理念而设计 ...

  2. Struts2接受参数的几种类型和接受复杂类型参数(list<String>和list<Object>)

    Struts2接受参数的几种类型 大概有这几种类型: 1.使用Action的属性接受参数 在Action中加入成员变量,配置Getter和Setter方法,Getter而和Setter方法的名字和表单 ...

  3. Struts2请求参数校验

    校验的分类 客户端数据校验 和 服务器端数据校验 客户端数据校验 ,通过JavaScript 完成校验 (改善用户体验,使用户减少出错 ) 服务器数据校验 ,通过Java代码 完成校验 struts2 ...

  4. struts2接收参数——域模型、DTO

    在开始介绍域模型之前我们要明白一点,为什么通过域模型我们可以把参数这么方便的在后台接收. 那是因为 通过参数拦截器(params interceptor)自动的把前台传过来的参数给域对象(domain ...

  5. Struts2 请求参数接收

    在Struts2中提供了更为简单的参数请求与接收方法,可以直接在Action中定义属性:Struts2通过反射机制将参数反射到属性的set方法上实现参数的传递: GET方式传送参数 <strut ...

  6. Struts2请求参数合法性校验机制

    在Action中通过代码执行数据校验 请求参数的输入校验途径一般分两种:客户端校验 :通过JavaScript 完成 (jquery validation插件),目的:过滤正常用户的误操作. 服务器校 ...

  7. SpringMVC札集(04)——SpringMVC传递参数

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  8. Struts2获取参数的几种方式

    Struts2由于是一个贴心的框架,所以获取参数这种体力活,就无需再通过原生的request来getParameter了,有如下几种方式进行获取 1.Action中属性驱动,必须提供与form表单na ...

  9. struts2 复杂参数封装

    1.1.1    Struts2中封装复杂类型的数据: 封装到List集合: 页面: 商品名称:<input type="text" name="products[ ...

  10. Python进阶04 函数的参数对应

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经接触过函数(function)的参数(arguments)传递.当时我们根 ...

随机推荐

  1. C#LIQN基础知识

  2. SQL Server 常用函数总结

    SQL去空格函数 1.ltrim(‘内容’)--去掉字符左边的空格 代码如下 declare @str varchar(100) set @str=' ADFADF' select @str sele ...

  3. angular 父组件调用子组件

    import { Component, OnInit, ViewChild } from '@angular/core'; @Component({ selector: 'app-child', te ...

  4. WinForm中使用自定义Tooltip控件

    private ToolTip tooltipCtr; 构造函数中: 隐藏默认的Tooltip:this.ShowCellToolTips = false; this.tooltipCtr = new ...

  5. 「HNOI2008」越狱

    题目链接 戳我 \(Solution\) 正难则反,这道题直接做有点困难,但我们可以反过来思考我们可以用总方案数减去不可以越狱的方案数 首先来算总方案数: 对于每个房间的人都有\(M\)种宗教可以选, ...

  6. php代码审计4审计代码执行漏洞

    代码执行漏洞代码执行漏洞是指应用程序本身过滤不严,用户可以通过请求将代码注入到应用中执行,当应用在调用一些能将字符串转化成代码的函数(如php中的eval)时,没有考虑到用户是否能控制这个字符串,造成 ...

  7. 【bzoj4176】Lucas的数论 莫比乌斯反演+杜教筛

    Description 去年的Lucas非常喜欢数论题,但是一年以后的Lucas却不那么喜欢了. 在整理以前的试题时,发现了这样一道题目"求Sigma(f(i)),其中1<=i< ...

  8. loj #2051. 「HNOI2016」序列

    #2051. 「HNOI2016」序列 题目描述 给定长度为 n nn 的序列:a1,a2,⋯,an a_1, a_2, \cdots , a_na​1​​,a​2​​,⋯,a​n​​,记为 a[1: ...

  9. BZOJ2668:[CQOI2012]交换棋子(费用流)

    题目描述 有一个n行m列的黑白棋盘,你每次可以交换两个相邻格子(相邻是指有公共边或公共顶点)中的棋子,最终达到目标状态.要求第i行第j列的格子只能参与mi,j次交换. 输入输出格式 输入格式: 第一行 ...

  10. 洛谷P3355 骑士共存问题(最小割)

    传送门 de了两个小时的bug愣是没发现错在哪里……没办法只好重打了一遍竟然1A……我有点想从这里跳下去了…… 和方格取数问题差不多,把格子按行数和列数之和的奇偶性分为黑的和白的,可以发现某种颜色一定 ...