Strus2 页面传输数据到后台(普通属性传值,模型驱动传值,域对象传值)
1、普通属性传值:
在页面中提交信息的名称和action类中设置对应的属性名一致,设置setter方法(建议都设置getter和setter方法)。
示例代码:
页面emp.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>数据从页面到后台(普通属性传值)</title>
</head>
<body>
<form action="emp/emp_insert.action" method="post">
<p>
编号:<input type="text" name="id">
</p>
<p>
姓名:<input type="text" name="name">
</p>
<p>
入职时间:<input type="text" name="hiredate">
</p>
<!--传递Set集合数据 -->
<p>
爱好: <input type="checkbox" name="hobby" value="足球" checked>足球
<input type="checkbox" name="hobby" value="篮球">篮球 <input
type="checkbox" name="hobby" value="乒乓球">乒乓球
</p>
<!--传递Map集合数据 a,b为Map的key-->
<p>
特长1:<input type="text" name="specialty['a']" />
</p>
<p>
特长2:<input type="text" name="specialty['b']" />
</p>
<input type="submit">
</form>
</body>
</html>
后台Action类:
package cn.sz.action;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Emp2Action {
// 数据从页面到后台(普通属性传值),在action类中设置对应的属性(属性名一致),设置setter方法,Map集合需要设置get方法
// 普通属性传值的操作
private Integer id;// 编号
private String name;// 姓名
private Date hiredate;// 入职时间
private Set<String> hobby;// 爱好(Set集合)
private Map<String, String> specialty;// 特长(Map集合)
public Map<String, String> getSpecialty() {
return specialty;
}
public void setSpecialty(Map<String, String> specialty) {
this.specialty = specialty;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public void setHobby(Set<String> hobby) {
this.hobby = hobby;
}
public String insert() {
// 输出接收到的内容
System.out.println(id + "," + name + "," + hiredate);
// 遍历爱好
for (String str : hobby) {
System.out.println(str);
}
// 遍历特长
Set<String> set = specialty.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String key = (String) it.next();
System.out.println(key + "," + specialty.get(key));
}
return "insert";
}
}
2、模型驱动传值:
借助实体类,将页面的数据直接通过框架封装到对象中,在action中获得对象,action需要实现一个接口 ModelDriven<E>
示例代码:
页面emp.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>数据从页面到后台(普通属性传值)</title>
</head>
<body>
<form action="emp/emp_insert.action" method="post">
<p>
编号:<input type="text" name="id">
</p>
<p>
姓名:<input type="text" name="name">
</p>
<p>
入职时间:<input type="text" name="hiredate">
</p>
<!--传递Set集合数据 -->
<p>
爱好: <input type="checkbox" name="hobby" value="足球" checked>足球
<input type="checkbox" name="hobby" value="篮球">篮球 <input
type="checkbox" name="hobby" value="乒乓球">乒乓球
</p>
<!--传递Map集合数据 a,b为Map的key-->
<p>
特长1:<input type="text" name="specialty['a']" />
</p>
<p>
特长2:<input type="text" name="specialty['b']" />
</p>
<input type="submit">
</form>
</body>
</html>
实体类Emp
package cn.sz.vo;
import java.util.Date;
import java.util.Map;
import java.util.Set;
public class Emp {
private Integer id;
private String name;
private Date hiredate;
private Set<String> hobby;
private Map<String, String> specialty;// 特长
public Map<String, String> getSpecialty() {
return specialty;
}
public void setSpecialty(Map<String, String> specialty) {
this.specialty = specialty;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public void setHobby(Set<String> hobby) {
this.hobby = hobby;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Date getHiredate() {
return hiredate;
}
public Set<String> getHobby() {
return hobby;
}
}
后台Action类
package cn.sz.action;
import java.util.Iterator;
import java.util.Set;
import com.opensymphony.xwork2.ModelDriven;
import cn.sz.vo.Emp;
public class EmpAction implements ModelDriven<Emp> {
// 数据从页面到后台(模型驱动传值)
// 借助实体类,将页面的数据直接通过框架封装到对象中,在action中获得对象;注action需要实现ModelDriven接口
private Emp emp2 = new Emp();
public Emp getEmp2() {
return emp2;
}
public void setEmp2(Emp emp1) {
this.emp2 = emp1;
}
public String insert() {
// 测试模型驱动
System.out.println(emp2.getId() + "," + emp2.getName() + "," + emp2.getHiredate());
// 遍历爱好
for (String str : emp2.getHobby()) {
System.out.println(str);
}
// 遍历特长
Set<String> set = emp2.getSpecialty().keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String key = (String) it.next();
System.out.println(key + "," + emp2.getSpecialty().get(key));
}
return "insert";
}
@Override
public Emp getModel() {
return this.emp2;
}
}
3、域对象传值:
域对象:范围对象 。application对应ServletContext, session对应HttpSession, request对应HttpServletRequest , response对应HttpServletResponse
域对象获取有两种IOC方式和非IOC方式,下面演示的是非IOC方式。详细的资料查看 http://blog.csdn.net/benxiaohai888/article/details/78374307
示例代码:
页面index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<%
application.setAttribute("id", "001");
request.setAttribute("name", "james");
session.setAttribute("content", "content");
%>
<a href="user/user_insert.action">添加用户</a>
</body>
</html>
后台Action类:
package cn.sz.action;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import cn.sz.vo.User;
//数据从页面到后台,
public class UserAction {
// 域对象传值方法(非IOC方式)
public String insert() {
//非IOC方式获取域对象
ServletContext application = ServletActionContext.getServletContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = ServletActionContext.getRequest().getSession();
HttpServletResponse response = ServletActionContext.getResponse();
String id = (String) application.getAttribute("id");
String name = (String) request.getAttribute("name");
String sessionvalue = (String) session.getAttribute("session");
System.out.println(id + "," + name + "," + sessionvalue);
application.setAttribute("key", "application");
session.setAttribute("key", "session");
request.setAttribute("key", "request");
return "insert";
}
}
4、OGNL传值
jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h1>注册页面</h1>
<form action="isreg.action" method="post">
<p>
用户名:<input type="text" name="user.loginName" value="lisi" />
</p>
<p>
密码:<input type="text" name="user.loginPwd" value="123" />
</p>
<p>
年龄:<input type="text" name="user.age" value="30" />
</p>
<p>
生日:<input type="text" name="user.birthday"
value="2001-02-03 10:20:20" />
</p>
<hr>
<br />
<p>
<input type="submit" value="注册" />
</p>
</form>
</body>
</html>
实体类Users.java
package cn.sz.pojo;
import java.io.Serializable;
import java.util.Date;
public class Users implements Serializable {
private String loginName;
private String loginPwd;
private Integer age;
private Date birthday;
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getLoginPwd() {
return loginPwd;
}
public void setLoginPwd(String loginPwd) {
this.loginPwd = loginPwd;
}
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;
}
}
Action类:
package cn.sz.action;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import cn.sz.pojo.Users;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class UserAction extends ActionSupport {
private Users user;
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
public String isreg() {
System.out.println(user.getLoginName() + "," + user.getLoginPwd() + ","
+ user.getAge() + "," + user.getBirthday());
return "success";
}
}