首页 技术 正文
技术 2022年11月17日
0 收藏 450 点赞 2,873 浏览 5807 个字

在很多的实际开发场景中,页面提交请求参数Action ,在Action中接收参数并对接收的数据进行封装。封装到一个JavaBean中,将JavaBean传递给业务层中。Struts2数据封装分为两类:属性驱动,模型驱动。

1.模型驱动

通过实现ModelDriven接口来接收请求参数。实现接口并且重写getModel()方法

Action类代码如下:

 package com.huan.web.action; import com.huan.domain.Customer;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{ private Customer customer=new Customer(); public String add(){
System.out.println(customer); return "saveSuccess";
} @Override
public Customer getModel() { return customer;
} }

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>
<h1>处理请求参数</h1>
<form action="${pageContext.request.contextPath}/Demo10Action.action" >
姓名:<input type="text" name="name"><br/>
年龄:<input type="text" name="age"><br/>
生日:<input type="text" name="birthday"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>

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>
<package name="param" namespace="/" extends="struts-default">
<action name="Demo10Action" class="com.sturts2.day02.c_param.Demo10Action" method="execute">
<result name="success" type="dispatcher">/form3.jsp</result>
</action> </package>
</struts>

2.属性驱动(很少使用)

在Action中定义java数据类型字段并与表单数据对应,利用这些字段进行数据传递

2.1属性驱动之提供属性的set方法(做为了解)

这中方法要在Action中定义属性并提供属性的set方法,当传递数据变多,Action的属性和set方法也随之变多。会让Action变臃肿,不简洁。

form1.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}/Demo8Action">
用户名:<input type="text" name="name" /><br>
年龄:<input type="text" name="age" /><br>
生日:<input type="text" name="birthday" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>

Demo8Action.java 即Action类

 package com.sturts2.day02.c_param; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class Demo8Action extends ActionSupport {
private String name;
private Integer age;
private Date birthday;
public Demo8Action() {
super();
System.out.println("Demo8Action对象创建了");
} public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String execute() throws Exception {
System.out.println("name参数值:"+name+"age参数值:"+age+"birthday参数值"+birthday);
return SUCCESS;
} }

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>
<package name="param" namespace="/" extends="struts-default"> <action name="Demo8Action" class="com.sturts2.day02.c_param.Demo8Action" method="execute">
<result name="success" type="dispatcher">/form1.jsp</result>
</action> </package>
</struts>

打开form1.jsp页面

Struts2的数据封装

输上数据并提交,控制台显示

Struts2的数据封装

浏览器显示: URL不变请求转发到form.jsp页面

Struts2的数据封装

2.2页面提供表达式方式

在页面表单上显示表达式:

     <form action="${pageContext.request.contextPath}/Demo9Action">
用户名:<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类

 public class Demo9Action extends ActionSupport{
private User user; @Override
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
//需要提供get方法 public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} }

还需要提供User的实体类:

属性值要和表单上name属性值对应

 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() {
// TODO Auto-generated method stub
return "User [ name="+name+"\n age="+age+"\n birthday="+birthday+"]";
} }

Struts.xml 的action配置就不做显示

把程序在服务器运行,打开页面填上数据:

Struts2的数据封装

控制台显示

Struts2的数据封装

 浏览器请求转发到form2.jsp页面

Struts2的数据封装

3.Struts2中封装集合类型的数据

在开发中,有时我们需要批量插入用户或者批量插入其他对象。在Action中需要接收多个Action中封装的对象然后传递给业务层。这个时候就需要把表单的信息封装到集合中。一般我们通常使用集合 List或Map

3.1 封装到List集合中

编写页面:

 <form action="${pageContext.request.contextPath}/Demo9Action.action" >
姓名:<input type="text" name="list[0].name"><br/>
年龄:<input type="text" name="list[0].age"><br/>
生日:<input type="text" name="list[0].birthday"><br/>
姓名:<input type="text" name="list[1].name"><br/>
年龄:<input type="text" name="list[1].age"><br/>
生日:<input type="text" name="list[1].birthday"><br/>
<input type="submit" value="提交">
</form>

编写Action:

 public class Demo9Action extends ActionSupport{
private List<User> list; @Override
public String execute() throws Exception {
for(User user :list){
System.out.println(user); }
return SUCCESS;
} public List<User> getUser() {
return list;
} public void setUser(List<User> list) {
this.list = list;
}

3.2 封装数据到Map集合:

页面:

 <form action="${pageContext.request.contextPath}/Demo10Action.action" >
姓名:<input type="text" name="map['one'].name"><br/>
年龄:<input type="text" name="map['one'].age"><br/>
生日:<input type="text" name="map['one'].birthday"><br/>
姓名:<input type="text" name="map['two'].name"><br/>
年龄:<input type="text" name="map['two].age"><br/>
生日:<input type="text" name="map['two'].birthday"><br/>
<input type="submit" value="提交">
</form>

Action类:

 public class Demo10Action extends ActionSupport{
private Map<String ,User> map; @Override
public String execute() throws Exception {
for(Stirng key : map.keySet()){
User user=map.get(key);
System.out.println(key+" "+user); }
return SUCCESS;
} public Map<String ,User> getMap() {
return map;
} public void setMap(Map<String ,User> map) {
this.map = map;
}
上一篇: Python中进程
下一篇: js去重
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896