标签:java com dem post ide 方式 end page ext
Struts2的数据封装:Struts2是一个web层框架,框架是软件的半成品。提供了数据封装的基本功能。
这种方式很少用,除非数据少,需要在Action类里自己往对象里封装
前端jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 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=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>Struts2的数据封装</h1> 11 <h3>方式一:属性驱动:提供Set方法的方式</h3> 12 <form action="${pageContext.request.contextPath }/userAction1.action" method="post"> 13 用户名:<input type="text" name="username"><br/> 14 密码:<input type="password" name="password"><br/> 15 年龄:<input type="text" name="age"><br/> 16 生日:<input type="text" name="birthday"><br/> 17 工资:<input type="text" name="salary"><br/> 18 <input type="submit" value="提交"> 19 </form> 20 </body> 21 </html>
实体类User:
package com.itheima.struts2.domain; /** * 实体对象 */ import java.util.Date; public class User { private String username; private String password; private int age; private Date birthday; private Double salary; public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setAge(int age) { this.age = age; } public void setBirthday(Date birthday) { this.birthday = birthday; } public void setSalary(Double salary) { this.salary = salary; } @Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", age=" + age + ", birthday=" + birthday
+ ", salary=" + salary + "]";
}
}
Action类:
1 package com.itheima.struts2.demo2; 2 3 import java.util.Date; 4 5 import com.itheima.struts2.domain.User; 6 import com.opensymphony.xwork2.ActionSupport; 7 /** 8 * 数据封装的方式一:属性封装 9 * 10 */ 11 public class UserAction1 extends ActionSupport { 12 private String username; 13 private String password; 14 private Integer age; 15 private Date birthday; 16 private Double salary; 17 18 19 public void setUsername(String username) { 20 this.username = username; 21 } 22 23 24 public void setPassword(String password) { 25 this.password = password; 26 } 27 28 29 public void setAge(Integer age) { 30 this.age = age; 31 } 32 33 34 public void setBirthday(Date birthday) { 35 this.birthday = birthday; 36 } 37 38 39 public void setSalary(Double salary) { 40 this.salary = salary; 41 } 42 43 44 public String execute() throws Exception{ 45 //接收数据 46 System.out.println(username); 47 System.out.println(password); 48 System.out.println(age); 49 System.out.println(birthday); 50 System.out.println(salary); 51 //封装数据 52 User user = new User(); 53 user.setAge(age); 54 user.setUsername(username); 55 user.setBirthday(birthday); 56 user.setSalary(salary); 57 58 return NONE; 59 } 60 }
前端JSP:name属性前加上实体类的对象前缀,如:user.password
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 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=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>Struts2的数据封装</h1> 11 <h3>方式二:属性驱动:提供页面表达式方式</h3> 12 <form action="${pageContext.request.contextPath }/userAction2.action" method="post"> 13 用户名:<input type="text" name="user.username"><br/> 14 密码:<input type="password" name="user.password"><br/> 15 年龄:<input type="text" name="user.age"><br/> 16 生日:<input type="text" name="user.birthday"><br/> 17 工资:<input type="text" name="user.salary"><br/> 18 <input type="submit" value="提交"> 19 </form> 20 </body> 21 </html>
Action类:相较而言,这种直接封装对象,而不是操作属性的方式,比较简约
package com.itheima.struts2.demo2; import com.itheima.struts2.domain.User; import com.opensymphony.xwork2.ActionSupport; /** * 数据封装方式二:属性驱动 * @author 李腾 * */ public class UserAction2 extends ActionSupport { //提供一个User对象,在前端页面的name属性上加上user前缀 private User user; //提供user的get和set方法,一定要提供get方法 //因为拦截器提供对象的封装,需要创建User对象,前提是前端属性名字的前缀和类一样 public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute() throws Exception{ System.out.println(user); return NONE; } }
Action类实现ModelDriven接口,重写getModel方法,返回实体对象
缺点:只能想一个对象(比如user)中封装对象。而页面表达式可以向多个对象封装(只需改变name属性的前缀如student.username user.username )
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 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=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>Struts2的数据封装</h1> 11 <h3>方式三:模型驱动方式</h3> 12 <form action="${pageContext.request.contextPath }/userAction3.action" method="post"> 13 用户名:<input type="text" name="username"><br/> 14 密码:<input type="password" name="password"><br/> 15 年龄:<input type="text" name="age"><br/> 16 生日:<input type="text" name="birthday"><br/> 17 工资:<input type="text" name="salary"><br/> 18 <input type="submit" value="提交"> 19 </form> 20 </body> 21 </html>
Action类:
package com.itheima.struts2.demo2; import com.itheima.struts2.domain.User; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; /** * 数据封装方式三:模型驱动 最常用的方式,缺点:只能同时向一个对象中封装数据 * 二:向多个数据封装中同时封装数据 * @author 李腾 * */ public class UserAction3 extends ActionSupport implements ModelDriven<User>{ //前提:手动实例化User private User user = new User(); //模型驱动需要使用的方法 @Override public User getModel() { return user; } public String execute() throws Exception{ System.out.println(user); return NONE; } }
标签:java com dem post ide 方式 end page ext
原文地址:https://www.cnblogs.com/ltfxy/p/9824481.html