當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Struts2+Spring传参
生活随笔
收集整理的這篇文章主要介紹了
Struts2+Spring传参
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
總結(jié)下Struts2的傳參
方式一: 直接在xxxAction中聲明變量,并設(shè)置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?
方式二:將數(shù)據(jù)封裝到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?
方式三:實(shí)現(xiàn)ModenDriven<T>接口實(shí)現(xiàn)傳參
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.javaUser.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.javaindex.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?
轉(zhuǎn)載于:https://www.cnblogs.com/alcc/p/3625886.html
總結(jié)
以上是生活随笔為你收集整理的Struts2+Spring传参的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ctags对部分目录生成tags
- 下一篇: 利用chunk重设大小攻击堆