javaEE Struts2,Action接收参数的三种方式
程序员文章站
2022-05-08 16:03:34
...
Action接收参数的底层原理是,params拦截器通过OGNL实现的。
Demo1Action.java(方式一:属性驱动,属性会让Action类看着很乱,不常用):
package cn.xxx.c_param;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
//struts2如何获得参数
//每次请求Action时都会创建新的Action实例对象 (Action线程安全,servlet是线程不安全的)
public class Demo1Action extends ActionSupport {
public Demo1Action() {
super();
System.out.println("demo1Action被创建了!"); // 每次请求都会创建Action实例,线程安全。
}
//准备与参数键名称相同的属性 (自动将接收到的参数赋值给对应的属性)
private String name; // 属性要有对应的getter和setter方法
//自动类型转换 只能转换8大基本数据类型以及对应包装类
private Integer age;
//支持特定类型字符串转换为Date ,例如 yyyy-MM-dd
private Date birthday;
public String execute() throws Exception {
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;
}
}
Demo2Action.java(方式二:对象驱动):
package cn.xxx.c_param;
import com.opensymphony.xwork2.ActionSupport;
import cn.xxx.domain.User;
//struts2如何获得参数-方式2
public class Demo2Action extends ActionSupport {
//准备user对象(其实也是属性,有getter和setter方法) form表单元素的name属性值对应是user.age
private User user;
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
form.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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Demo2Action">
用户名:<input type="text" name="user.name" /><br> <!-- user.name表示封装到user对象的name属性 -->
年龄:<input type="text" name="user.age" /><br>
生日:<input type="text" name="user.birthday" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
Demo3Action.java(方式三:模型驱动,实现ModelDriven接口):
package cn.xxx.c_param;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.xxx.domain.User;
//struts2如何获得参数-方式3
public class Demo3Action extends ActionSupport implements ModelDriven<User> { // 实现ModelDriven接口
//准备user 成员变量(没有getter和setter方法)
private User user = new User();
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
// 重写ModelDriven接口的方法
@Override
public User getModel() {
return user; // 返回成员变量 user
}
}
User.java(JavaBean类):
package cn.xxx.domain;
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 + "]";
}
}