Struts2+Spring传参

总结下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