结果页面配置
全局页面
- result标签配置action方法的返回值到不同的路径里面
- 创建两个action,执行默认的方法execute方法,让两个action的方法都返回success,返回success之后,配置到同一个页面里面
- 如果多个action,方法里面返回值相同的,到页面也是相同的,这个时候可以使用全局结果页面配置
<?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="demo1" extends="struts-default" namespace="/">
<!-- 全局结果页面配置,只在当前package中有效 -->
<global-results>
<result name="success">/hello.jsp</result>
</global-results>
<action name="book" class="cn.itcast.action.BookAction">
</action>
<action name="orders" class="cn.itcast.action.OrdersAction">
</action>
</package>
</struts>
public class BookAction extends ActionSupport {
@Override
public String execute() throws Exception {
return "success";
}
}
public class OrdersAction extends ActionSupport {
@Override
public String execute() throws Exception {
return "success";
}
}
局部页面
<?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="demo1" extends="struts-default" namespace="/">
<!-- 全局结果页面配置,只在当前package中有效 -->
<global-results>
c
</global-results>
<action name="book" class="cn.itcast.action.BookAction">
<!--局部页面-->
<result name="success">/world.jsp</result>
</action>
</package>
</struts>
- 配置全局页面,也配置了局部页面,最终以局部配置为准
result的type属性
- result标签里面除了name属性之外,还有一个属性 type属性
- type属性:如何到路径里面(转发还是重定向)
- 默认值,做转发操作,值是 dispatcher
<result name="success" type="dispatcher">/hello.jsp</result>
- 做重定向操作,值是 redirect
<result name="success" type="redirect">/hello.jsp</result>
- chain:转发到action,一般不用,缓存问题
- redirectAction:重定向到action
<?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="demo1" extends="struts-default" namespace="/">
<action name="book" class="cn.itcast.action.BookAction">
<result name="success" type="redirectAction">orders</result>
</action>
<action name="orders" class="cn.itcast.action.OrdersAction">
<result name="success" >/hello.jsp</result>
</action>
</package>
</struts>
- 当请求/book时,会先执行book的action标签,然后访问orders标签,最终返回hello页面
获取表单数据
使用ActionContext类
- 因为方法不是静态的方法,需要创建ActionContext类的对象
- 这个ActionContext类对象不是new出来的
<%@ 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 }/form4.action" method="post">
username:<input type="text" name="username"/>
<br/>
password:<input type="text" name="password"/>
<br/>
address:<input type="text" name="address"/>
<br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
<!-- 获取表单提交数据 -->
<package name="demo2" extends="struts-default" namespace="/">
<action name="form1" class="cn.itcast.form.Form1DemoAction"></action>
</package>
package cn.itcast.form;
import java.util.Map;
import java.util.Set;
import org.apache.catalina.tribes.util.Arrays;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Form1DemoAction extends ActionSupport {
public String execute() throws Exception {
//第一种方式 使用ActionContext类获取
//1 获取ActionContext对象
ActionContext context = ActionContext.getContext();
//2 调用方法得到表单数据
// key是表单输入项name属性值,value是输入的值
Map<String, Object> map = context.getParameters();
Set<String> keys = map.keySet();
for (String key : keys) {
//根据key得到value
//数组形式:因为输入项里面可能有复选框情况
Object[] obj = (Object[]) map.get(key);
System.out.println(Arrays.toString(obj));
}
//不返回可以写NONE
return NONE;
}
}
ServletActionContext
package cn.itcast.form;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.catalina.tribes.util.Arrays;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Form2DemoAction extends ActionSupport {
public String execute() throws Exception {
//第一种方式 使用ServletActionContext类获取
//1 使用ServletActionContext获取request对象
HttpServletRequest request = ServletActionContext.getRequest();
//2 调用request里面的方法得到结果
String username = request.getParameter("username");
String password = request.getParameter("password");
String address = request.getParameter("address");
System.out.println(username+":"+password+":"+address);
return NONE;
}
}
接口注入
package cn.itcast.form;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.tribes.util.Arrays;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用接口注入方式
* @author asus
*
*/
public class Form3DemoAction extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request ;
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public String execute() throws Exception {
request.getParameter("");
return NONE;
}
}
操作域对象
package cn.itcast.form;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.catalina.tribes.util.Arrays;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Form2DemoAction extends ActionSupport {
public String execute() throws Exception {
//操作三个域对象
//1 request域
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("req", "reqValue");
//2 session域
HttpSession session = request.getSession();
session.setAttribute("sess", "sessValue");
//3 ServletContext域
ServletContext context = ServletActionContext.getServletContext();
context.setAttribute("contextname", "contextValue");
return NONE;
}
}
实体类封装
属性封装
- 直接把表单提交属性封装到action的属性里面
- 在action成员变量位置定义变量:变量名称和表单输入项的name属性值一样
- 生成变量的set方法(把set和get方法都写出来)
- 使用属性封装获取表单数据到属性里面,不能把数据直接封装到实体类对象里面
package cn.itcast.data;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用属性封装获取表单数据
* @author asus
*
*/
public class DataDemo1Action extends ActionSupport {
//1 定义变量
//变量的名称和表单输入项name属性值一样
private String username;
private String password;
private String address;
//2 生成变量的set和get方法
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String execute() throws Exception {
System.out.println(username+":"+password+":"+address);
return NONE;
}
}
模型驱动
- action实现接口 ModelDriven
- 实现接口里面的方法 getModel方法,把创建对象返回
- 在action里面创建实体类对象
package cn.itcast.data;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.itcast.entity.User;
/**
* 使用模型驱动获取表单数据
* @author asus
*
*/
public class DataDemo2Action extends ActionSupport implements ModelDriven<User>{
//创建对象
//前提要求: 表单输入项name属性值 和 实体类属性名称一样
private User user = new User();
public User getModel() {
//返回创建user对象
return user;
}
@Override
public String execute() throws Exception {
System.out.println(user);
return NONE;
}
}
表达式封装
- 在action里面声明实体类
- 生成实体类变量的set和get方法
package cn.itcast.data;
import com.opensymphony.xwork2.ActionSupport;
import cn.itcast.entity.Book;
import cn.itcast.entity.User;
/**
* 使用表达式封装数据到实体类对象
* @author asus
*
*/
public class DataDemo3Action extends ActionSupport {
//1 声明实体类
private User user;
//2 生成实体类变量的set和get方法
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;
}
}
- 在表单输入项的name属性值里面写表达式形式
<%@ 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 }/data3.action" method="post">
username:<input type="text" name="user.username"/>
<br/>
password:<input type="text" name="user.password"/>
<br/>
address:<input type="text" name="user.address"/>
<br/>
bname: <input type="text" name="book.bname"/>
<br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
集合封装
List封装
- 第一步 在action声明List
- 第二步 生成list变量的set和get方法
package cn.itcast.data;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import cn.itcast.entity.User;
/**
* 封装数据到list集合
* @author asus
*
*/
public class ListAction extends ActionSupport {
// 1 声明List变量
private List<User> list;
// 2 生成get和set方法
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
@Override
public String execute() throws Exception {
System.out.println(list);
return NONE;
}
}
- 第三步 在表单输入项里面写表达式
<%@ 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 }/list.action" method="post">
<!-- list[0] : 表示list集合中第一个user对象 -->
username:<input type="text" name="list[0].username"/>
<br/>
password:<input type="text" name="list[0].password"/>
<br/>
address:<input type="text" name="list[0].address"/>
<br/><br/>
username:<input type="text" name="list[1].username"/>
<br/>
password:<input type="text" name="list[1].password"/>
<br/>
address:<input type="text" name="list[1].address"/>
<br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
map封装
- 第一步 声明map集合
- 第二步 生成get和set方法
package cn.itcast.data;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import cn.itcast.entity.User;
/**
* 封装数据到map集合
* @author asus
*
*/
public class MapAction extends ActionSupport {
// 1 声明map集合
private Map<String,User> map;
public Map<String, User> getMap() {
return map;
}
public void setMap(Map<String, User> map) {
this.map = map;
}
@Override
public String execute() throws Exception {
System.out.println(map);
return NONE;
}
}
- 第三步 在表单输入项的name属性值里面写表达式
<%@ 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 }/map.action" method="post">
<!-- 设置key值 map['key值']
设置value值
-->
username:<input type="text" name="map['one'].username"/>
<br/>
password:<input type="text" name="map['one'].password"/>
<br/>
address:<input type="text" name="map['one'].address"/>
<br/><br/>
username:<input type="text" name="map['abcd'].username"/>
<br/>
password:<input type="text" name="map['abcd'].password"/>
<br/>
address:<input type="text" name="map['abcd'].address"/>
<br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>