本节内容:
1.结果页面配置
2.在Action获取表单提交数据
3.Struts2提供获取表单数据的方式
4.Struts2获取数据封装到集合
5.扩展-表达式封装和模型驱动比较
一、结果页面配置
1.全局结果页面
在struts.xml中我们的package标签中有一个标签叫做<global-results> 全局结果集
<package name="login" namespace="/" extends="struts-default" >
<!--全局结果页面只能在一个package中有效 package中的所有的action都有作用-->
<global-results>
<result name="success">/index.jsp</result>
</global-results>
</package>
2.局部结果页面
<package name="login" namespace="/" extends="struts-default" >
<!--全局结果页面只能在一个package中有效 package中的所有的action都有作用-->
<global-results>
<result name="success">/index.jsp</result>
</global-results>
<action name="userLogin" class="com.struts.action.HelloWordAction" >
<!--局部结果页面配置:如果有全局结果页面,同时又局部结果页面,已局部结果页面为准-->
<result name="success" >/index.jsp</result>
</action>
</package>
3.result标签中的type属性
1.result标签中除了name属性还有一个属性type属性
type属性:如何到路径页面(转发还是重定向)
type属性值:
默认值:转发操作 值是:dispatcher :转发到页面
redirect:重定向到一个页面
redirectAction:重定向到一个actionchain:用于转发到action:一般不用stream:向浏览器发送InputStream对象通常用来处理文件下载
二、Action中获取表单提交数据
我们在servlet中知道我们获取表单的数据直接使用request对象调用方法来获取。在struts2中我们需要在action中获取,但是我们action中没有request对象
1.使用ActionContext类获取
public String login(){
// 使用ActionContext获取表单数据
ActionContext actionContext = ActionContext.getContext ();
HttpParameters httpParameters = actionContext.getParameters ();
Parameter parameter = httpParameters.get ("username");
String username = parameter.getValue ();
System.out.println (username);
return "success";
}
2.使用ServletActionContext获取
通过servletActionContext类调用方法可以获取request对象
// 使用ServletActionContext获取表单数据
HttpServletRequest request = ServletActionContext.getRequest ();
String username = request.getParameter ("username");
System.out.println (username);
3.使用接口注入方式获取
在这里我们需要我们的action类来实现一个接口ServletRequestAware来实现方法:setServletRequest
public class LoginAction implements ServletRequestAware{
private HttpServletRequest request;
@Override
public void setServletRequest(HttpServletRequest httpServletRequest) {
request = httpServletRequest;
}
public String login(){
String username = request.getParameter ("username");
System.out.println (username);
}
}
这种方式不常用,大家只需要知道就可以了
在Action中操作域对象:request、session、application
在action中使用ServletActionContext来获取域对象:
// 获取域对象
HttpServletRequest request = ServletActionContext.getRequest ();
request.setAttribute ("request","request");
HttpSession httpSession = request.getSession ();
httpSession.setAttribute ("httpSession","httpSession");
ServletContext servletContext = ServletActionContext.getServletContext ();
servletContext.setAttribute ("application","application");
三、Struts2提供获取表单数据方式
1.属性封装
直接把表单数据Action的属性中
实现步骤:
1.Action类中的成员变量位置定义变量
--变量名称与表单输入项的name属性值一样
生成变量的set与get方法
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<form action="register" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="pwd"/><br/>
年龄:<input type="text" name="age"/><br/>
出生日期:<input type="date" name="birthday"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
package com.struts.action;
import java.util.Date;
/**
* 使用属性封装获取表单数据
*/
public class RegisterAction {
// 属性名与表单中输入项的name值一样
private String username;
private String pwd;
private int age;
private Date birthday;
// 提供get与set方法
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String register(){
System.out.println (username);
System.out.println (pwd);
System.out.println (age);
System.out.println (birthday);
return "success";
}
}
使用属性封装只是获取数据到属性中,不能把数据直接封装到对象中。
2.模型驱动封装
可以直接把表单的数据封装到实体类中
实现步骤:
1.action实现一个接口ModelDriver
2.实现接口中的方法getModel方法
-把创建对象返回
3.在action类中创建实体类对象
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<form action="registermodel" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="pwd"/><br/>
年龄:<input type="text" name="age"/><br/>
出生日期:<input type="date" name="birthday"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
package com.struts.action;
import com.opensymphony.xwork2.ModelDriven;
/**
* 模型驱动封装
*/
public class RegisterModelAction implements ModelDriven<User>{
//创建对象
//使用模型封装的前提条件:表单输入项name的属性值和实体类中的属性名称一样
private User user = new User ();
public String register(){
System.out.println (user);
return "success";
}
@Override
public User getModel() {
//返回创建对象
return user;
}
}
实体类

package com.struts.action;
import java.util.Date;
public class User {
private String username;
private String pwd;
private int age;
private Date birthday;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", pwd='" + pwd + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
使用模型驱动和属性封装注意问题:
在一个action中获取表单数据可以使用属性封装、也可以使用模型驱动封装,不能同时使用属性封装和模型驱动封装来获取同一个表单数据,如果同时使用只会执行模型驱动不会执行属性封装。
表达式封装(会用即可)
1.实现过程
1.使用表达式封装可以把表单数据封装到实体类对象中
步骤:1.在action中声名实体类
2.生成实体类的set与get
3.在表单的输入项中name的值写成表达式形式
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<form action="registermodel" method="post">
用户名:<input type="text" name="user.username"/><br/>
密码:<input type="password" name="user.pwd"/><br/>
年龄:<input type="text" name="user.age"/><br/>
出生日期:<input type="date" name="user.birthday"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
package com.struts.action;
public class UserRegisterAction {
// 声名实体类
private User user;
// 生成实体类变量的get与set方法
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String register(){
return "success";
}
}
四、扩展-表达式封装和模型驱动比较
1.使用表达式封装和模型驱动封装都可以吧数据封装到实体类对象中
2.使用模型驱动只能吧数据封装到一个实体类中 在一个action中不能使用模型驱动把数据封装到不同的实体类对象中
3. 使用表达式封装就可以吧数据封装到不同的实体类对象中
五、Struts2中获取数据封装到集合中(会用)
1.封装到List集合
步骤:1.在action中声明list
2.生成list变量的set与get方法
3.在表单输入项中使用表达式写法
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<form action="registermodel" method="post">
<%--list[0]表示list集合中第一个user对象--%>
用户名:<input type="text" name="list[0].username"/><br/>
密码:<input type="password" name="list[0].pwd"/><br/>
年龄:<input type="text" name="list[0].age"/><br/>
出生日期:<input type="date" name="list[0].birthday"/><br/>
用户名:<input type="text" name="list[1].username"/><br/>
密码:<input type="password" name="list[1].pwd"/><br/>
年龄:<input type="text" name="list[1].age"/><br/>
出生日期:<input type="date" name="list[1].birthday"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
package com.struts.action;
import java.util.List;
//封装数据到list集合
public class RegisterListAction {
//声明list集合
private List<User> list;
// 提供get与set方法
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
public String register(){
return "success";
}
}
2.封装到Map集合
实现步骤:
1.声明map集合
2.生成get与set方法
3,。在表单中的name属性写表达式
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<form action="registermodel" method="post">
用户名:<input type="text" name="map['one'].username"/><br/>
密码:<input type="password" name="map['one'].pwd"/><br/>
年龄:<input type="text" name="map['one'].age"/><br/>
出生日期:<input type="date" name="map['one'].birthday"/><br/>
用户名:<input type="text" name="map['tow'].username"/><br/>
密码:<input type="password" name="map['tow'].pwd"/><br/>
年龄:<input type="text" name="map['tow'].age"/><br/>
出生日期:<input type="date" name="map['tow'].birthday"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
package com.struts.action;
import java.util.Map;
/**
* Map集合封装
*/
public class MapAction {
//声明map集合
private Map<String,User> map;
//生成get与set方法
public Map<String, User> getMap() {
return map;
}
public void setMap(Map<String, User> map) {
this.map = map;
}
public String register(){
return "success";
}
}
来源:https://www.cnblogs.com/wuzhilong/p/9919642.html
