所有的MVC框架都需要负责解析HTTP的请求参数,并对请求参数进行封装和解析处理,Struts2自带有很强大的类型转换器,能够将前端传来的参数自动转换成Action类中成员变量(基于OGNL的类型转换),所以,使用Struts2来接收前端传来的基本类型参数的时候,只需要在Action类中定义与传来参数对应名称的成员变量,并提供getter和setter方法,就可以接收参数;而对于复杂类型的参数的封装的时候,前端提供的请求参数名需要按照OGNL表达式的形式进行传参。
接收基本类型
首先,定义一个Action类ParamterAction,接收前端传来的name和password参数,并打印到控制台
package com.bran.g_paramter;
import com.opensymphony.xwork2.ActionSupport;
//struts2如何获得参数
public class ParamterAction extends ActionSupport {
//准备与参数键名称相同的属性
private String name;
private String password;
@Override
public String execute() throws Exception {
System.out.println("用户名:"+name);
System.out.println("密码:"+password);
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在struts.xml中对该Action进行配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="paramter" namespace="/" extends="struts-default">
<action name="ParamterAction"
class="com.bran.g_paramter.ParamterAction" method="execute">
<result name="success" type="dispatcher">/form/form1.jsp</result>
</action>
</package>
</struts>
再定义一个表单form1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>接收参数</title>
<style type="text/css">
.a {
width: 300px;
height: 30px;
}
#b {
width: 500px;
text-align: right;
}
#b button {
width: 60px;
margin-right: 120px;
}
</style>
</head>
<body>
<div id="b">
<form action="${pageContext.request.contextPath }/ParamterAction">
<strong>用户名:</strong><input type="text" class="a" name="name" /><br/><br/>
<strong>密 码:</strong><input type="password" class="a" name="password" /><br/><br/>
<button type="submit">提交</button>
</form>
</div>
</body>
</html>
</html>
启动项目,在form1.jsp页面向ParamterAction提交数据
点击提交,就可以在后台就可以看到打印结果
接收数据并封装成对象类型
定义一个对象User
package com.bran.domain;
import java.util.Date;
public class User {
private String name;
private Integer age;
private Date birthday;
public User() {}
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 + "]";
}
}
再新建一个Action类Paramter2Action,用于将接收的参数封装到User对象中,并且将User对象的信息打印到控制台
package com.bran.g_paramter;
import com.bran.domain.User;
import com.opensymphony.xwork2.ActionSupport;
public class Paramter2Action extends ActionSupport {
//准备User对象
private User user;
@Override
public String execute() throws Exception {
if(user!=null)
System.out.println("用户:"+user.toString());
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
在struts.xml中对该Action进行配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="paramter" namespace="/" extends="struts-default">
<action name="Paramter2Action"
class="com.bran.g_paramter.Paramter2Action" method="execute">
<result name="success" type="dispatcher">/form/form2.jsp</result>
</action>
</package>
</struts>
新建一个表单form2.jsp,用于提交数据
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>接收参数</title>
<style type="text/css">
.a {
width: 300px;
height: 30px;
}
#b {
width: 500px;
text-align: right;
}
#b button {
width: 60px;
margin-right: 120px;
}
</style>
</head>
<body>
<div id="b">
<form action="${pageContext.request.contextPath }/Paramter2Action">
<strong>用户名:</strong><input type="text" class="a" name="user.name" /><br/><br/>
<strong>年龄:</strong><input type="text" class="a" name="user.age" /><br/><br/>
<strong>生日:</strong><input type="date" class="a" name="user.birthday" /><br/>
<button type="submit">提交</button>
</form>
</div>
</body>
</html>
这里的请求参数名并不是普通的参数名,而是用OGNL表达式的形式,写成user.name和user.age和user.birthday,这样,Struts2会将user.name的参数的值赋给Action实例的user属性的name属性,并将user.age的参数的值赋给Action实例的user属性的age属性,以及将user.birthday的参数的值赋给Action实例的user属性的birthday属性,通过这种方式,来完成对象类型数据的封装。
注意:
这里,Struts2是通过反射来创建User类的实例,User类中必须带有无参的构造方法;而且,通过使用user.name请求参数的形式来为Action实例的user属性的name属性赋值,实质上是调用了User类中的setName方法进行赋值,所以User类中必须包含有该属性对应的set方法,要想获取该属性当然还需要get方法。
启动项目,在form2.jsp页面中提交表单数据
就可以在控制台看到打印出user的数据
基于实现ModelDriven接口来接收对象类型数据
上面介绍了一种基于OGNL表达式来将参数封装到对象类型里面,还有一种方式也可以实现将普通参数封装到对象类型里面,通过实现ModelDriven这个接口来完成。
首先,定义一个Action类Paramter3Action来测试,实现了ModelDriven接口,Model类型为User类型,User类使用的是上面定义过的User。
package com.bran.g_paramter;
import com.bran.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
//struts2如何获得参数
public class Paramter3Action extends ActionSupport implements ModelDriven<User>{
//准备User 成员变量
private User user = new User();
@Override
public String execute() throws Exception {
if(user!=null)
System.out.println("用户:"+user.toString());
return SUCCESS;
}
@Override
public User getModel() {
return user;
}
}
struts.xml中进行配置Action
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="paramter" namespace="/" extends="struts-default">
<action name="Paramter3Action"
class="com.bran.g_paramter.Paramter3Action" method="execute">
<result name="success" type="dispatcher">/form/form3.jsp</result>
</action>
</package>
</struts>
同样再写的一个表单页面form3.jsp,提交到Paramter3Action中
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>接收参数</title>
<style type="text/css">
.a {
width: 300px;
height: 30px;
}
#b {
width: 500px;
text-align: right;
}
#b button {
width: 60px;
margin-right: 120px;
}
</style>
</head>
<body>
<div id="b">
<form action="${pageContext.request.contextPath }/Paramter3Action">
<strong>用户名:</strong><input type="text" class="a" name="name" /><br/><br/>
<strong>年龄:</strong><input type="text" class="a" name="age" /><br/><br/>
<strong>生日:</strong><input type="date" class="a" name="birthday" /><br/>
<button type="submit">提交</button>
</form>
</div>
</body>
</html>
这里面的请求参数名不需要写成OGNL表达式的形式,直接写User里面对应的属性名就行了
启动项目,在form3.jsp页面提交数据
控制台就可以打印出User数据
封装成List和Map类型的数据
list和map类型的数据也可以通过OGNL表达式来进行封装的。
首先。新建一个测试封装集合类型的Action类ParamterListAction
package com.bran.g_paramter;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
//struts2封装集合类型参数
public class ParamterListAction extends ActionSupport {
// list
private List<String> list;
// map
private Map<String, String> map;
@Override
public String execute() throws Exception {
System.out.println("list:" + list);
System.out.println("map:" + map);
return SUCCESS;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
struts.xml中进行配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="paramter" namespace="/" extends="struts-default">
<action name="ParamterListAction"
class="com.bran.g_paramter.ParamterListAction" method="execute">
<result name="success" type="dispatcher">/form/form4.jsp</result>
</action>
</package>
</struts>
写一个测试表单form4.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>接收参数</title>
<style type="text/css">
.a {
width: 300px;
height: 30px;
}
#b {
width: 500px;
text-align: right;
}
#b button {
width: 60px;
margin-right: 120px;
}
</style>
</head>
<body>
<div id="b">
<form action="${pageContext.request.contextPath }/ParamterListAction"
method="post">
<strong>list:</strong><input type="text" class="a" name="list" /><br/><br/>
<strong>list:</strong><input type="text" class="a" name="list" /><br/> <br/>
<strong>map:</strong><input type="text" class="a" name="map['skt']" /><br/> <br/>
<button type="submit">提交</button>
</form>
</div>
</body>
</html>
启动项目,在form4.jsp页面提交数据
就可以在控制台打印出数据