总结下Struts2的传参

方式一:

直接在xxxAction中声明变量,并设置get、set方法。前台直接传过来

 1 public class UserAction {
 2     /**
 3      * Logger for this class
 4      */
 5     private static final Logger logger = Logger.getLogger(UserAction.class);
 6
 7     UserService userService = null;
 8
 9     public UserService getUserService() {
10         return userService;
11     }
12
13     public void setUserService(UserService userService) {
14         this.userService = userService;
15     }
16
17     public String getName() {
18         return name;
19     }
20
21     public void setName(String name) {
22         this.name = name;
23     }
24
25     public int getPwd() {
26         return pwd;
27     }
28
29     public void setPwd(int pwd) {
30         this.pwd = pwd;
31     }
32
33     private String name = null;
34     private int pwd = 0;
35
36     public void login() {
37         // boolean flag = userService.login(user);
38         logger.info(name);
39         logger.info(pwd);
40         /*
41          * if (flag == true) { } else { }
42          */
43     }
44
45 }

xxxAction

方式二:将数据封装到JavaBean中,在Action中声明get、set方法,接收该JavaBean。注意index.jsp中的表单提交方式:user.name,user.pwd

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 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=ISO-8859-1">
 7 <title>hello</title>
 8 </head>
 9 <body>
10     <form action="/mybatis-spring/login.action" method="post">
11         name: <input type="text" name="user.name"><br>
12         pwd : <input type="password" name="user.pwd">
13               <input type="submit" value="Submit">
14     </form>
15 </body>
16 </html>

index.jsp

 1 public class UserAction {
 2     /**
 3      * Logger for this class
 4      */
 5     private static final Logger logger = Logger.getLogger(UserAction.class);
 6
 7     UserService userService = null;
 8
 9     public UserService getUserService() {
10         return userService;
11     }
12
13     public void setUserService(UserService userService) {
14         this.userService = userService;
15     }
16     private User user = null;
17
18
19     public User getUser() {
20         return user;
21     }
22
23     public void setUser(User user) {
24         this.user = user;
25     }
26
27     public void login() {
28         // boolean flag = userService.login(user);
29         logger.info(user.getPwd());
30         logger.info(user.getName());
31         /*
32          * if (flag == true) { } else { }
33          */
34     }
35
36 }

xxxAction.java

 1 public class User {
 2     private Integer id;
 3     private String name;
 4     private Integer pwd;
 5
 6     public Integer getId() {
 7         return id;
 8     }
 9
10     public void setId(Integer id) {
11         this.id = id;
12     }
13
14     public String getName() {
15         return name;
16     }
17
18     public void setName(String name) {
19         this.name = name == null ? null : name.trim();
20     }
21
22     public Integer getPwd() {
23         return pwd;
24     }
25
26     public void setPwd(Integer pwd) {
27         this.pwd = pwd;
28     }
29 }

User.java

方式三:实现ModenDriven<T>接口实现传参

UserAction.java

 1 public class UserAction extends ActionSupport implements ModelDriven<User> {
 2     /**
 3      * Logger for this class
 4      */
 5     private static final Logger logger = Logger.getLogger(UserAction.class);
 6
 7     UserService userService = null;
 8
 9     public UserService getUserService() {
10         return userService;
11     }
12
13     public void setUserService(UserService userService) {
14         this.userService = userService;
15     }
16
17     // have to initialize it
18     private User user = new User();
19
20     public User getUser() {
21         return user;
22     }
23
24     public void setUser(User user) {
25         this.user = user;
26     }
27
28     public String login() {
29         logger.info(user.getPwd());
30         logger.info(user.getName());
31         return "success";
32     }
33
34     @Override
35     public User getModel() {
36         return user;
37     }
38 }

UserAction.java

User.java

 1 public class User {
 2
 3     private Integer id;
 4     private String name;
 5     private Integer pwd;
 6
 7     public Integer getId() {
 8         return id;
 9     }
10
11     public void setId(Integer id) {
12         this.id = id;
13     }
14
15     public String getName() {
16         return name;
17     }
18
19     public void setName(String name) {
20         this.name = name == null ? null : name.trim();
21     }
22
23     public Integer getPwd() {
24         return pwd;
25     }
26
27     public void setPwd(Integer pwd) {
28         this.pwd = pwd;
29     }
30
31     public User() {
32
33     }
34
35 }

User.java

index.jsp 这里提交表单时候用的是name,pwd。

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 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=ISO-8859-1">
 7 <title>hello</title>
 8 </head>
 9 <body>
10     <form action="/mybatis-spring/login.action" method="post">
11         name: <input type="text" name="name"><br> pwd : <input
12             type="password" name="pwd"> <input type="submit"
13             value="Submit">
14     </form>
15 </body>
16 </html>

index.jsp

转载于:https://www.cnblogs.com/alcc/p/3625886.html

Struts2+Spring传参相关推荐

  1. struts2 url传参中文乱码

    1.设置struts.xml <constant name="struts.i18n.encoding" value="UTF-8" /> 或是设置 ...

  2. Struts2三种传参方式(从jsp页面传到Action)

    多的不说,直接上代码;struts.xml代码:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYP ...

  3. Spring Boot 传参方式

    2019独角兽企业重金招聘Python工程师标准>>> 最近在搞Spring Boot的项目,把传参方式总结一下.网上也参考一些文章,总结的很不错,这里借鉴一下. 注解 @Reque ...

  4. ajax redirectattributes 使用,Spring中RedirectAttributes对象重定向传参

    Spring3中的FlashAttribute 为 了防止用户刷新重复提交,save操作之后一般会redirect到另一个页面,同时带点操作成功的提示信息.因为是Redirect,Request里 的 ...

  5. spring mvc controller间跳转 重定向 传参

    spring mvc controller间跳转 重定向 传参 1. 需求背景     需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形 ...

  6. Strut2页面传参跳转 --Struts2

    1.本案例借助struts2框架,完成页面传参.跳转功能 2.代码实现 index.jsp: <form action="helloStruts2.action" metho ...

  7. Spring mvc Controller间跳转/重定向/传参

    Spring mvc Controller常用写法 1.ModelAndView @RequestMapping(value = "/getxxxList.html") publi ...

  8. layui-mini+spring boot实现table搜索操作传参

    项目场景: layui-mini + spring boot 使用table.reload实现搜索操作的传参 问题描述: layui向后端传的json格式数据 因table.reload在传参时默认携 ...

  9. Spring/Boot/Cloud系列知识:SpringMVC 传参详解(下)

    (接上文<Spring/Boot/Cloud系列知识:SpringMVC 传参详解(上)>) 2.3.通过@PathVariable注解基于URL匹配符接收请求传参 为了便于开发人员实现更 ...

最新文章

  1. LeetCode简单题之学生分数的最小差值
  2. 芯片设计抽象层及其设计风格
  3. 别再说你不会!自学java教程百度云
  4. Homework 8 测试计划
  5. POJ - 1330 Nearest Common Ancestors(树上倍增/树链剖分求LCA)
  6. java 接口可以多继承
  7. EPS 转 pdf 在线
  8. Luckysheet(在线表格) v2.1.12
  9. Mybatis出现文档根元素 mapper 必须匹配 DOCTYPE 根 configuration错误解决办法
  10. An impassioned circulation of affection(尺取+预处理)
  11. Python学习-类的继承
  12. 计算机数制和运算的一点总结.
  13. 记录一次爬取淘宝/天猫评论数据的过程
  14. 软件设计师中级考试备考资料
  15. 工行u盾显示316_工行手机银行u盾签名失败或未完成(310)是为什么?
  16. 如果你不释放MogaFX外汇,你将无法获得交易或投资
  17. 这是一篇路由器踩坑的文章
  18. 【扫盲】Pulse消除马赛克(老司机福利)
  19. 什么是微分?导数和微分的区别是什么?微分和积分的联系?
  20. windows命令行工具

热门文章

  1. spark 写tidb_优秀的数据工程师,怎么用Spark在TiDB上做OLAP分析
  2. php接口返回错误码,laravel 错误处理,接口错误返回json代码
  3. 电子科大电气工程导师介绍绍_电子科技大学计算机科学与工程学院研究生导师介绍:葛树志...
  4. python汉诺塔用循环结构实现_Python基于递归算法实现的汉诺塔与Fibonacci数列
  5. python能做哪些单机游戏好玩_【单机游戏】可以快速用Python进行数据分析的几个小技巧_玩得好游戏攻略...
  6. vb6 打印选项对话框_办公必备技能,Word打印问题及解决方案全在这,轻松解决打印难题...
  7. hdu5437(2015长春网络赛A题)
  8. LOJ #2731 [JOI2016春季合宿]Solitaire (DP、组合计数)
  9. 开了gomod不识别gopath_三分钟掌握Go mod常用与高级操作
  10. 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race