总结下Struts2的传参
方式一: xxxAction index.jsp xxxAction.java User.java UserAction.java User.java index.jsp
直接在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 }
方式二:将数据封装到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 4 5 6 715 16hello 8 9 10
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 }
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 }
方式三:实现ModenDriven<T>接口实现传参
UserAction.java1 public class UserAction extends ActionSupport implements ModelDriven{ 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 it18 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 @Override35 public User getModel() {36 return user;37 }38 }
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 }
index.jsp 这里提交表单时候用的是name,pwd。
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 4 5 6 7hello 8 9 10