struts2学习_04
一、结果页面配置
1、全局结果页面
(1)result标签配置action方法返回值到不同的路劲里面
(2)创建两个Action执行默认的方法execute方法,让两个方法都返回success,返回success之后,配置到同一个页面
- 如果多个action,方法里面返回值相同的,到页面也是相同的这个时候可以使用全局结果页面配置
<package name="Stationery" extends="struts-default" namespace="/">
<action name="book" class="cn.com.action.BookAction">
<result name="success">/hello.jsp</result>
</action>
<action name="pen" class="cn.com.action.PenAction">
<result name="success">/hello.jsp</result>
</action>
</package>
- 在package标签里配置全局结果页面
<!-- 全局结果页面配置 -->
<global-results>
<result name="success">/hello.jsp</result>
</global-results>
2、局部结果页面
配置了全局结果页面,也配置了局部结果页面,最终以局部结果页面配置为准
<package name="Stationery" extends="struts-default" namespace="/">
<!-- 全局结果页面配置 -->
<global-results>
<result name="success">/hello.jsp</result>
</global-results>
<!-- 局部结果页面配置 -->
<action name="book" class="cn.com.action.BookAction">
<!-- 以局部结果页面配置为准 -->
<result name="success">/book.jsp</result>
</action>
<action name="pen" class="cn.com.action.PenAction">
<result name="success">/pen.jsp</result>
</action>
</package>
3、Result标签type属性
(1)result标签除了有name属性,还有type属性
- type属性:如何到路径里面(转发和重定向)
(2)type属性值
- dispatcher:默认值,请求转发操作,
- dispatcher:重定向操作,无法共享请求范围内的数据
- chain:将action的带着原来的状态请求转发到新的action,一般不建议使用,缓存问题
- redirect-action:重定向到另一个Action,参数与chain用法相同,允许将原Action中的属性指定新名称带入新Action中
- dispatcher、redirect,这两个值一般针对到页面中配置
- chain、redirect-action配置到其他action里
二、在action获取表单提交数据
1 之前web阶段,提交表单到servlet里面,在servlet里面使用request对象里面的方法获取,getParameter,getParameterMap
2 提交表单到action,但是action没有request对象,不能直接使用request对象
3 action获取表单提交数据主要三种方式
(1)使用ActionContext类
(2)使用ServletActionContext类
(3)使用接口注入方式
1、使用ActionContext类获取
(1)使用方法
- Map<String,Object>getParameter() 返回一个含有HttpServletRequest参数信
- static ActionContext getContext() 获取当前线程的ActionContext对像
- 方法不是静态的,需要创建ActionContext类对象
- ActionContext对象不是new出来的
(2)具体实现
- 创建表单,提交表单到action
- 在action的方法中使用ActionContext获取数据
//使用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) {
//用数组形式是因为输入项可能有复选框的情况
Object[] obj = (Object[]) map.get(key);
System.out.println(Arrays.toString(obj));
}
2、使用servletActionContext类获取
- static HttpServletRequest() 获取Web应用的 HttpServletRequest对象
- static HttpServletResponse() 获取Web应用的 HttpServletResponse对象
- static ServletContext.getServletContext 获取Web应用的 ServletContext对象
- static PageContext.getPageContext 获取Web应用的 PageContext对象
(1)使用方法
- 调用类的静态方法,得到request对象
- action中代码实现
public String execute() throws Exception {
//使用ServletActionContext获取request对象
HttpServletRequest request = ServletActionContext.getRequest();
//调用request里的方法得到结果
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String address = request.getParameter("address");
return NONE;
}
3、使用接口注入方式获取
(1)让action类实现ServletRequestAware接口
public class HelloAction extends ActionSupport implements ServletRequestAware{
private HttpServletRequest request;
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public String execute() throws Exception {
request.getParameter("");
return super.execute();
}
}
4、在action操作域对象
(1)request、session、servletContext域对象
(2)使用ServletActionContext类操作
//request域
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("requestName", "requestValue");
//session域
HttpSession session = request.getSession();
session.setAttribute("sessionName", "sessionValue");
//servletContext域
ServletContext context = ServletActionContext.getServletContext();
context.setAttribute("contextName", "contextValue");
三、struts2提供获取表单数据的方式
原始方式获取表单封装到实体类对象
//获取表单数据
HttpServletRequest requesr = ServletActionContext.getRequest();
String username = requesr.getParameter("username");
String password = requesr.getParameter("password");
String address = requesr.getParameter("address");
//封装到实体类中
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.serAddress(address);
1、属性封装
(1)直接把表单提交属性封装到action的属性里面
(2)实现步骤
- 在action成员变量位置定义变量。变量名称和表单输入项的name属性值一样
- 生成变量的set和get方法
public class Damo extends ActionSupport {
//定义变量,名称和
private String username;
private String password;
private String address;
//生成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;
}
}
- 使用属性封装获取表单数据到属性里面,不能把数据直接封装到实体类对象里面
2、模型驱动封装
(1)使用模型驱动方式,可以直接把表单数据封装到实体类对象里面
(2)实现步骤
- action实现接口 ModelDriven.
public class Damo extends ActionSupport implements ModelDriven<User>
- 实现接口里面的方法 getModel方法,把创建对象返回
- 在action里面创建实体类对象
//创建对象
private User user = new User();
public User getModel() {
//返回创建user对象
return user;
}
//表单输入项name属性值和实体类属性名称一样
(3)使用模型驱动和属性封装注意问题
在一个action中,获取表单数据可以属性封装,使用模型驱动封装,
不能同时使用属性封装和模型驱动封装获取同一个表单数据
如果同时使用,只会执行模型驱动
3、表达式封装
(1)实现过程
- 使用表达式封装可以把表单数据封装到实体类对象里面
第一步 在action里面声明实体类
第二步 生成实体类变量的set和get方法
//声明实体类
private User user;
//生成实体类的get、set方法
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
第三步 在表单输入项的name属性值里面写表达式形式
用户名:<input type="text" name="user.username">
<br>
密 码:<input type="text" name="user.password">
<br>
地 址:<input type="text" name="user.address">
<br>
(2)把表达式封装归类到属性封装里面
4、表达式封装和模型驱动封装比较
(1)相同点:表达式封装和模型驱动封装都可以把数据封装到实体类对象里面
(2)不同点:
- 使用模型驱动只能把数据封装到一个实体类对象里面,在一个action里面不能使用模型驱动把数据封装到不同的实体类对象里面
- 使用表达式封装可以把数据封装到不同的实体类对象里面。(action中可以声明多个实体类)
四、struts2获取数据封装到集合中
1、封装到list集合
(1)在action声明List
(2)生成list变量的set和get方法
//声明List变量
private List<User> list;
//生成get和set方法
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
(3)在表单输入项里面写表达式
用户名:<input type="text" name="list[0].username">
<br>
密 码:<input type="text" name="list[0].password">
<br>
地 址:<input type="text" name="list[0].address">
<br>
用户名:<input type="text" name="list[1].username">
<br>
密 码:<input type="text" name="list[1].password">
<br>
地 址:<input type="text" name="list[1].address">
<br>
2、 封装到map集合
(1)action中声明map集合
(2)生成get、set方法
//声明map集合
private Map<String, User> map;
//生成set、get方法
public Map<String, User> getMap() {
return map;
}
public void setMap(Map<String, User> map) {
this.map = map;
}
(3)表单输入项的name属性值里面写表达式
<!-- 设置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['two'].username"/>
<br/>
password:<input type="text" name="map['two'].password"/>
<br/>