demo1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Struts2的数据封装</h1>
<h3>方式一:属性驱动-提供set方法</h3>
<form action="${pageContext.request.contextPath }/userAction1.action" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
年龄:<input type="text" name="age"><br>
生日:<input type="text" name="birthday"><br>
工资:<input type="text" name="salary"><br>
<input type="submit" value="提交">
</form>
<h3>方式二:属性驱动-在页面提供表达式方式</h3>
<form action="${pageContext.request.contextPath }/userAction2.action" method="post">
用户名:<input type="text" name="user.username"><br>
密码:<input type="password" name="user.password"><br>
年龄:<input type="text" name="user.age"><br>
生日:<input type="text" name="user.birthday"><br>
工资:<input type="text" name="user.salary"><br>
<input type="submit" value="提交">
</form>
<h3>方式三:模型驱动-模型驱动的方式</h3>
<form action="${pageContext.request.contextPath }/userAction3.action" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
年龄:<input type="text" name="age"><br>
生日:<input type="text" name="birthday"><br>
工资:<input type="text" name="salary"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
User.java
package com.cherry.struts2.domain;
import java.util.Date;
public class User {
private String username;
private String password;
private Integer age;
private Date birthday;
private Double salary;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", age=" + age + ", birthday=" + birthday
+ ", salary=" + salary + "]";
}
}
UserAction1.java
package com.cherry.struts2.demo2;
import java.util.Date;
import com.cherry.struts2.domain.User;
import com.opensymphony.xwork2.ActionSupport;
/**
* 数据封装方式一:提供属性的set方法
* @author zhang
*
*/
public class UserAction1 extends ActionSupport {
//提供了对应的属性
private String username;
private String password;
private Integer age;
private Date birthday;
private Double salary;
//提供属性对应的set方法
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String execute() throws Exception {
//接收数据:
System.out.println(username);
System.out.println(password);
System.out.println(age);
System.out.println(birthday);
System.out.println(salary);
//封装数据
User user=new User();
user.setUsername(username);
user.setPassword(password);
user.setAge(age);
user.setBirthday(birthday);
user.setSalary(salary);
return NONE;
}
}
UserAction2.java
package com.cherry.struts2.demo2;
import com.cherry.struts2.domain.User;
import com.opensymphony.xwork2.ActionSupport;
/**
* 数据封装的方式二:属性驱动-在页面提供表达式方式
* @author zhang
*
*/
public class UserAction2 extends ActionSupport {
//提供一个User对象
private User user;
//提供user的set和get方法:一点要提供get方法
//因为拦截器完成数据封装,需要创建User对象
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
System.out.println(user);
return NONE;
}
}
UserAction3.java
package com.cherry.struts2.demo2;
import com.cherry.struts2.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 数据封装的方式三:模型驱动
* 缺点:只能封装到一个对象中
* @author zhang
*
*/
public class UserAction3 extends ActionSupport implements ModelDriven<User>{
//模型驱动使用的对象:前提必须手动提供对象的实例
private User user=new User();//手动实例化User
@Override
//模型驱动需要使用的方法:
public User getModel() {
return user;
}
@Override
public String execute() throws Exception {
System.out.println(user);
return NONE;
}
}
struts_demo2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="demo2" extends="struts-default" namespace="/">
<action name="userAction1" class="com.cherry.struts2.demo2.UserAction1">
</action>
<action name="userAction2" class="com.cherry.struts2.demo2.UserAction2">
</action>
<action name="userAction3" class="com.cherry.struts2.demo2.UserAction3">
</action>
</package>
</struts>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置Struts2的常量 -->
<constant name="struts.action.extendsion" value="action"></constant>
<include file="com/cherry/struts2/demo2/struts_demo2.xml"></include>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2_day02</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置核心过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>