博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2+Spring传参
阅读量:5276 次
发布时间:2019-06-14

本文共 4753 字,大约阅读时间需要 15 分钟。

总结下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  4  5  6 
7 hello 8 9 10
11 name:
12 pwd :
13
14
15 16
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
{ 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 }
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  4  5  6 
7 hello 8 9 10
11 name:
pwd :
14 15 16
index.jsp

 

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

你可能感兴趣的文章
redis与spring结合错误情况
查看>>
第六章 字节码执行方式--解释执行和JIT
查看>>
实现绘制图形的ToolBar
查看>>
C# 串口接收数据中serialPort.close()死锁
查看>>
字符串方法title()、istitle()
查看>>
yield语句
查看>>
Html.Partial和Html. RenderPartial用法
查看>>
查看linux系统中占用cpu最高的语句
查看>>
[洛谷P1738]洛谷的文件夹
查看>>
ubuntu server设置时区和更新时间
查看>>
《弟子规》下的沉思
查看>>
设置dataGridView单元格颜色、字体、ToolTip、字体颜色
查看>>
【京东咚咚架构演进】-- 好文收藏
查看>>
【HTML】网页中如何让DIV在网页滚动到特定位置时出现
查看>>
文件序列化
查看>>
C#应用视频教程3.1 USB工业相机测试
查看>>
实验一 绘制金刚石图案
查看>>
白话SpringCloud | 第五章:服务容错保护(Hystrix)
查看>>
fabricjs 高级篇(自定义类型)
查看>>
jQuery之end()和pushStack()
查看>>