1:在Action中如何获得作用域(request,session,application)对象;
取得Map(键值对映射集)类型的requet,session,application;
对数据操作的所有方法:(即把数据保存到域中)
主要使用的是方式2和方式3;
方式1:直接获取ServletApi,核心类是ServletActionContext提供的静态的方法;
代码语言:javascript复制 1 package com.bie.lesson04;
2
3 import javax.servlet.ServletContext;
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpSession;
6
7 import org.apache.struts2.ServletActionContext;
8
9 import com.opensymphony.xwork2.ActionSupport;
10
11 /**
12 * @author Author:别先生
13 * @date Date:2017年9月11日 下午10:06:41
14 *
15 *
16 */
17 public class StrutsData extends ActionSupport{
18
19 /**
20 *
21 */
22 private static final long serialVersionUID = 1L;
23
24 @Override
25 public String execute() throws Exception {
26
27 //数据保存到域中
28 //方式一,直接拿到servletApi执行操作
29 HttpServletRequest request = ServletActionContext.getRequest();
30 HttpSession session = request.getSession();
31 ServletContext application = ServletActionContext.getServletContext();
32 //操作
33 request.setAttribute("request_data", "request_data");
34 session.setAttribute("session_data", "session_data");
35 application.setAttribute("application_data", "application_data");
36
37
38 return SUCCESS;
39 }
40 }
方式2:通过ActionContext获取不同(代表request/session/application)的map;
代码语言:javascript复制 1 public class ScopeAction01 {
2
3 private Map<String,Object> request;
4 private Map<String,Object> session;
5 private Map<String,Object> application;
6
7
8 public ScopeAction01(){
9 request = (Map<String, Object>) ActionContext.getContext().get("request");
10 session = ActionContext.getContext().getSession();
11 application = ActionContext.getContext().getApplication();
12
13 }
14
15
16 public String execute(){
17 //向作用域中存储数据
18 request.put("requestKey", "requestValue");
19 session.put("sessionKey", "sessionValue");
20 application.put("applicationKey", "applicationValue");
21
22 return "success";
23 }
24
25 }
方式3:实现接口的方法:(RequestAware/SessionAware/ApplicationAware);
代码语言:javascript复制 1 package com.bie.action01;
2
3 import java.util.Map;
4
5 import org.apache.struts2.interceptor.ApplicationAware;
6 import org.apache.struts2.interceptor.RequestAware;
7 import org.apache.struts2.interceptor.SessionAware;
8
9 /**
10 * @author 作者:别先生
11 * @version 创建时间:2017年5月7日 上午8:37:05
12 * 类说明
13 * 作用域的使用方法2
14 */
15
16 public class ScopeAction2 implements RequestAware,SessionAware,ApplicationAware{
17
18 private Map<String,Object> request;
19 private Map<String,Object> session;
20 private Map<String,Object> application;
21
22
23 public void setApplication(Map<String, Object> application) {
24 this.application = application;
25 }
26
27 public void setSession(Map<String, Object> session) {
28 this.session = session;
29 }
30
31 public void setRequest(Map<String, Object> request) {
32 this.request = request;
33 }
34
35 public String execute(){
36 //map使用put设置值
37 request.put("requestKey", "requestValue");
38 session.put("sessionKey", "sessionbValue");
39 application.put("applicationKey", "applicationValue");
40
41 return "success";
42 }
43
44
45 }
方式1和方式2简介:
第一步: 引包,省去
第二步:配置Struts2的过滤器
代码语言:javascript复制 1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
3 <display-name>struts2_20170221</display-name>
4 <welcome-file-list>
5 <welcome-file>index.html</welcome-file>
6 <welcome-file>index.htm</welcome-file>
7 <welcome-file>index.jsp</welcome-file>
8 <welcome-file>default.html</welcome-file>
9 <welcome-file>default.htm</welcome-file>
10 <welcome-file>default.jsp</welcome-file>
11 </welcome-file-list>
12
13 <!-- struts2过滤器 -->
14 <filter>
15 <!-- 过滤器名称 -->
16 <filter-name>struts2</filter-name>
17 <!-- 过滤器类 -->
18 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
19 </filter>
20 <!-- struts2过滤器映射 -->
21 <filter-mapping>
22 <!-- 过滤器名称 -->
23 <filter-name>struts2</filter-name>
24 <!-- 过滤器映射 -->
25 <url-pattern>/*</url-pattern>
26 </filter-mapping>
27
28 </web-app>
第三步:开发Action,方式一和方式二的对比
代码语言:javascript复制 1 package com.bie;
2
3 import java.util.Map;
4
5 import javax.servlet.ServletContext;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpSession;
8
9 import org.apache.struts2.ServletActionContext;
10
11 import com.opensymphony.xwork2.ActionContext;
12 import com.opensymphony.xwork2.ActionSupport;
13
14 /**
15 * @author BieHongLi
16 * @version 创建时间:2017年2月21日 下午4:46:14
17 *
18 */
19 public class FinalAction extends ActionSupport{
20
21 private static final long serialVersionUID = 1L;
22
23 @Override
24 public String execute() throws Exception {
25 //1:请求数据封装 2:调用service处理业务逻辑,拿到结果数据
26 //3:数据保存到域中
27
28 //【struts2和servlet耦合的方法,
29 // 比如获取目录application.getRealPath("");等等】
30 //Struts中对数据操作,方式1:直接拿到ServletApi,执行操作
31 /*HttpServletRequest request=ServletActionContext.getRequest();
32 HttpSession session=request.getSession();
33 ServletContext application=ServletActionContext.getServletContext();
34
35 request.setAttribute("request_data", "request请求的数据");
36 session.setAttribute("session_data", "session请求的数据");
37 application.setAttribute("application_data", "application请求的数据");*/
38
39
40
41 //[推荐:解耦的方式实现对数据的操作,没有和servlet有耦合,
42 // 向域对象中存储值推荐]
43 //Struts中对数据操作,方式2:直接拿到ActionContext,执行操作
44 //getContext()发发一个静态方法,可以直接调用,返回值就是ActionContext
45 ActionContext ac=ActionContext.getContext();
46 //得到Strust对HttpServletRequest对象进行了封装,封装为了一个Map
47 //拿到表示request对象 的map
48 Map<String, Object> request=ac.getContextMap();
49 //拿到session对象的map
50 Map<String, Object> session=ac.getSession();
51 //拿到application对象的map
52 Map<String, Object> application=ac.getApplication();
53
54 //数据
55 request.put("request_data", "request_data_actionContext");
56 session.put("session_data", "session_date_actionContext");
57 application.put("application_data", "application_date_actionContext");
58 return SUCCESS;
59 }
60
61 }
第四步:Struts2的配置文件。
代码语言:javascript复制 1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6 <struts>
7
8 <!-- 声明包 -->
9 <package name="finalPackage" extends="struts-default">
10 <action name="requestAction" class="com.bie.FinalAction">
11 <result name="success">success.jsp</result>
12 </action>
13
14 <action name="ImplAction" class="com.bie.ImplAction">
15 <result name="success">success.jsp</result>
16 </action>
17 </package>
18
19 </struts>
方式三:只有开发Action的过程不一样,其他一样,这里只写了开发Action的代码
代码语言:javascript复制 1 package com.bie;
2
3 import java.util.Map;
4
5 import org.apache.struts2.interceptor.ApplicationAware;
6 import org.apache.struts2.interceptor.RequestAware;
7 import org.apache.struts2.interceptor.SessionAware;
8
9 import com.opensymphony.xwork2.ActionSupport;
10
11 /**
12 * @author BieHongLi
13 * @version 创建时间:2017年2月21日 下午7:40:02
14 *
15 */
16 public class ImplAction extends ActionSupport
17 implements RequestAware,SessionAware,ApplicationAware{
18
19 private static final long serialVersionUID = 1L;
20 private Map<String, Object> request;
21 private Map<String, Object> session;
22 private Map<String,Object> application;
23
24 //struts2运行的时候,会把代表request的map对象注入
25 @Override
26 public void setRequest(Map<String, Object> request) {
27 this.request=request;
28 }
29
30 //struts2运行的时候,会把代表session的map注入
31 @Override
32 public void setApplication(Map<String, Object> session) {
33 this.session=session;
34 }
35
36 //struts2运行的时候,会把代表application的map注入
37 @Override
38 public void setSession(Map<String, Object> application) {
39 this.application=application;
40 }
41
42 @Override
43 public String execute() throws Exception {
44 request.put("request_data", "request_dataAware");
45 session.put("session_data", "session_dataAware");
46 application.put("application_data","application_dataAware");
47 return SUCCESS;
48 }
49 }