多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # struts MVC ![](https://box.kancloud.cn/9bc09ac426211385c767055c06d50fa3_968x658.png) # Action生命周期 1. 每次请求到来时,都会创建一个新的Action实例 2. Action是线程安全的.可以使用成员变量接收参数 # 属性获得参数 表单页面 ~~~ <form action="${pageContext.request.contextPath}/hello/HelloAction"> 用户名:<input type="text" name="name" /><br> 年龄:<input type="text" name="age" /><br> 生日:<input type="text" name="birthday" /><br> <input type="submit" value="提交" /> </form> ~~~ action类 ~~~ // 准备与参数键名称相同的属性 private String name; // 自动类型转换 只能转换8大基本数据类型以及对应包装类 private Integer age; // 支持特定类型字符串转换为Date ,例如 yyyy-MM-dd private Date birthday; public String hello() { System.out.println("name参数值:" + name + ",age参数值:" + age + ",生日:" + birthday); return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } ~~~ # 对象驱动 编写一个对象,用来存放参数 User类 ~~~ package param; import java.util.Date; public class User { private String name; private Integer age; private Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", birthday=" + birthday + "]"; } } ~~~ form表单页面 ~~~ <form action="${pageContext.request.contextPath}/hello/HelloAction"> 用户名:<input type="text" name="user.name" /><br> 年龄:<input type="text" name="user.age" /><br> 生日:<input type="text" name="user.birthday" /><br> <input type="submit" value="提交" /> </form> ~~~ Action类 ~~~ // 准备user对象 private User user; public String hello() { System.out.println(user); return "success"; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } ~~~ # 模型驱动 User类还是上面的 form表单 ~~~ <form action="${pageContext.request.contextPath}/hello/HelloAction"> 用户名:<input type="text" name="name" /><br> 年龄:<input type="text" name="age" /><br> 生日:<input type="text" name="birthday" /><br> <input type="submit" value="提交" /> </form> ~~~ Action类 ~~~ package domain; import com.opensymphony.xwork2.ModelDriven; import param.User; public class HelloAction implements ModelDriven<User> { // 准备user对象 private User user = new User();; public String hello() { System.out.println(user); return "success"; } @Override public User getModel() { return user; } } ~~~ # 集合封装 ## list form表单 ~~~ <form action="${pageContext.request.contextPath}/hello/HelloAction"> list:<input type="text" name="list" /><br> list:<input type="text" name="list[3]" /><br> <input type="submit" value="提交" /> </form> ~~~ Action类 ~~~ package domain; import java.util.List; public class HelloAction { // list private List<String> list; public String hello() { System.out.println("list:" + list); return "success"; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } } ~~~ ## map form表单 ~~~ <form action="${pageContext.request.contextPath}/Demo11Action" method="post" > map:<input type="text" name="map['haha']" /><br> <input type="submit" value="提交" /> </form> ~~~ Action类 ~~~ import java.util.Map; public class HelloAction { // Map private Map<String, String> map; public String hello() { System.out.println("map:" + map); return "success"; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } } ~~~