程序员Java开发那些事

在Action获取Scope对象

2017-01-19  本文已影响0人  重山杨

在Action操作中,关键就是Action中的exectue方法,但是此方法并没有request、session、application等对象作为参数,自然就不能利用这些对象来操作。
通常可以使用四种方式来获取这些对象:

方式一、与Servlet解耦合的非IOC方式

获取的scope对象与容器无关,通过ActionContext获取。

package test002;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Map;

/**
 * Created by yangcs on 2017/1/19.
 * 通过与servlet解耦和的非IOC方式来获取scope对象
 */
public class ScopeAction extends ActionSupport{
    private String name;
    private String req;
    private String sess;

    public String getReq() {
        return req;
    }

    public void setReq(String req) {
        this.req = req;
    }

    public String getSess() {
        return sess;
    }

    public void setSess(String sess) {
        this.sess = sess;
    }

    public String getApp() {
        return app;
    }

    public void setApp(String app) {
        this.app = app;
    }

    private String app;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    ActionContext context;   //这个是解耦和的关键
    Map request;
    Map session;
    Map application;
    @Override
    public String execute(){
        context = ActionContext.getContext();
        request = (Map) context.get("request");
        session =context.getSession();
        application = context.getApplication();

        request.put("req",req);
        session.put("sess",sess);
        application.put("app",app);
        return SUCCESS;
    }
}
分析:通过ActionContext的getContext静态方法得到ActionContext对象,然后ActionContext
对象调用get方法来获取一个存储在request范围中的对象。我们使用el或通过request.getAttribute
这样的方式均可以获取对象值,这说明了这些Map request对象实际是存储在request范围内的对象。

方式二、与Servlet解耦合的IOC方式

package test002;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import java.util.Map;

/**
 * Created by yangcs on 2017/1/19.
 * 与servlet解耦和的IOC方式(依赖于struts框架来实现对象的实例化,依赖注入、控制反转)获取scope对象
 */
public class ScopeAction02 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
    private Map request;
    private Map session;
    private Map application;
    @Override
    public void setApplication(Map<String, Object> map) {
        application = map;
    }

    @Override
    public void setRequest(Map<String, Object> map) {
        request = map;
    }

    @Override
    public void setSession(Map<String, Object> map) {
        session = map;
    }
    private String req;
    private String sess;
    private String app;

    public String getReq() {
        return req;
    }

    public void setReq(String req) {
        this.req = req;
    }

    public String getSess() {
        return sess;
    }

    public void setSess(String sess) {
        this.sess = sess;
    }

    public String getApp() {
        return app;
    }

    public void setApp(String app) {
        this.app = app;
    }

    @Override
    public String execute(){
        request.put("req",req);
        session.put("sess",sess);
        application.put("app",app);
        System.out.println("reqMap为:"+request);
        return "success";
    }
}
说明:此方法其实和方式一很相似,只是在方式一中我们需要手动的为Map request赋值,
但是在方式二中它是通过实现接口,在重写接口中的方法中完成对Map requset的赋值,所以称之IOC方式。
 强调:方式二是开发中主要用的方式,应重点掌握
 

方式三、与Servlet耦合的非IOC方式

package test002;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by yangcs on 2017/1/19.
 * 与servlet相耦合的非IOC方式获取scope对象
 * 这种方式获取的为纯粹的Scope对象,它与容器相关,这些Scope对象操作性更强
 */
public class ScopeAction03 extends ActionSupport{
    private String req;
    private String sess;
    private String app;

    public String getReq() {
        return req;
    }

    public void setReq(String req) {
        this.req = req;
    }

    public String getSess() {
        return sess;
    }

    public void setSess(String sess) {
        this.sess = sess;
    }

    public String getApp() {
        return app;
    }

    public void setApp(String app) {
        this.app = app;
    }
    HttpServletRequest request;
    HttpSession session;
    ServletContext application;
    @Override
    public String execute(){
        request = ServletActionContext.getRequest();     //这里,就是耦合了servlet了
        session = request.getSession();
        application = ServletActionContext.getServletContext();
        request.setAttribute("req",req);
        session.setAttribute("sess",sess);
        application.setAttribute("app",app);
        return SUCCESS;
    }
}
此方法获取的纯粹的Scope对象,它与容器相关,这些Scope对象可操作性更强。

方式四、与Servlet耦合的IOC方式

package test002;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by yangcs on 2017/1/19.
 * 与servlet耦合的情况下使用IOC获取scope对象
 */
public class ScopeAction04 extends ActionSupport implements ServletRequestAware,ServletContextAware{
    HttpServletRequest request;
    HttpSession session;
    ServletContext application;

    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        request = httpServletRequest;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        application = servletContext;
    }
    private String req;
    private String sess;
    private String app;

    public String getReq() {
        return req;
    }

    public void setReq(String req) {
        this.req = req;
    }

    public String getSess() {
        return sess;
    }

    public void setSess(String sess) {
        this.sess = sess;
    }

    public String getApp() {
        return app;
    }

    public void setApp(String app) {
        this.app = app;
    }
    @Override
    public String execute(){
        request.setAttribute("req",req);
        session = request.getSession();
        session.setAttribute("sess",sess);
        application.setAttribute("app",app);
        return SUCCESS;
    }
}

附:用于测试的访问页面及结果展示页面:

struts.xml配置文件:

<?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="test002" namespace="/" extends="struts-default">
        <action name="Action003" class="test002.ScopeAction">
            <result name="success">/JSP/scope01.jsp</result>
        </action>
        <action name="Action004" class="test002.ScopeAction02">
            <result name="success">/JSP/scope01.jsp</result>
        </action>
        <action name="Action005" class="test002.ScopeAction03">
            <result name="success">/JSP/scope01.jsp</result>
        </action>
        <action name="Action006" class="test002.ScopeAction04">
            <result name="success">/JSP/scope01.jsp</result>
        </action>

    </package>

</struts>

输入参数页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>scope</title>
</head>
<body>
本页面展示在action中获取scope对象<br><br>
<form method="get" action="Action003.action">
    这个表单是使用与servlet解耦和的非IOC方式来获取scope对象<br>
    request:<input type="text" name="req"/><br>
    session:<input type="text" name="sess"/><br>
    appliaction:<input type="text" name="app"/><br>
    <input type="submit"/>
</form>
<form method="get" action="Action004.action">
    这个表单是使用与servlet解耦和的IOC方式来获取scope对象(主要使用)<br>
    request:<input type="text" name="req"/><br>
    session:<input type="text" name="sess"/><br>
    appliaction:<input type="text" name="app"/><br>
    <input type="submit"/>
</form>
<form method="get" action="Action005.action">
    这个表单是使用与servlet耦和的非IOC方式来获取scope对象<br>
    request:<input type="text" name="req"/><br>
    session:<input type="text" name="sess"/><br>
    appliaction:<input type="text" name="app"/><br>
    <input type="submit"/>
</form>
<form method="get" action="Action006.action">
    这个表单是使用与servlet耦和的IOC方式来获取scope对象<br>
    request:<input type="text" name="req"/><br>
    session:<input type="text" name="sess"/><br>
    appliaction:<input type="text" name="app"/><br>
    <input type="submit"/>
</form>

</body>
</html>

获取参数展示页面:

<%--
  Created by IntelliJ IDEA.
  User: yangcs
  Date: 2017/1/19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>scope01</title>
</head>
<body>
    request:${requestScope.req}<br>
    session:${sessionScope.sess}<br>
    application:${applicationScope.app}<br>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读