欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Struts2获取Servlet的api的两种方式,解决ParameterAware过时的问题

程序员文章站 2022-03-28 19:59:17
servlet API通过ActionContext进行获取 获取HttpSession对应的Map对象:public Map getSession 获取ServletContext对应的MAP对象:public Map getApplication 请求参数对应的Map对象:public Map ......

servlet api通过actioncontext进行获取

struts2对httpservletrequest,httpsession和servletcontext进行了封装,构造了3个map对象替代以上以上3个对象。在action中可以直接使用httpservletrequest,httpservletsession,servletcontext对应的map对象来保存和读取对象。

actioncontext是action执行的上下文,在actioncontext保存了action,执行所需要的所有对象,包括parameters,request,session,application
  • 获取httpsession对应的map对象:public map getsession
  • 获取servletcontext对应的map对象:public map getapplication
  • 请求参数对应的map对象:public map getparameters()

代码实现

public class actioncontexttest {

    /**
     * 执行execute方法
     * @return string
     */
    public string execute() {
        // 获取actioncontext对象
        actioncontext actioncontext = actioncontext.getcontext();
        // 获取applicationmap
        map<string, object> applicationmap = actioncontext.getapplication();
        // 可以通过jsp页面获取${applicationscope.get}
        applicationmap.put("applicationkey","applicationvalue");
        // 也可以从jsp页面获取设置的值
        system.out.println(applicationmap.get("date"));

        // 在session中添加值
        map<string, object> sessionmap = actioncontext.getsession();
        sessionmap.put("sessionkey","sessionvalue");
        // 获取页面设置的值
        system.out.println(sessionmap.get("date"));

        // 由于底层没有提供actioncontext.getrequest()方法,所以需要自己手动获取
        map<string, object> requestmap = (map<string, object>) actioncontext.get("request");
        requestmap.put("requestkey","requestvalue");

        // 获取提交url的get参数详情,原来的是一个map集合对象,而现在是一个map集合的实现类,该map实现类无法添加参数只能做读取(url提交的属性)
        httpparameters parameters = actioncontext.getparameters();
        system.out.println(parameters.get("name"));
        return success;
    }

其中struts2.xml中配置如下:

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="actioncontexttest" class="com.codegeekgao.actioncontext.actioncontexttest" method="execute">
            <result>/testactioncontext.jsp</result>
        </action>
    </package>
</struts>

成功后跳转的jsp页面

<%@ page import="java.util.date" %>
<%@ page import="java.text.simpledateformat" %><%--
  created by intellij idea.
  user: donniegao
  date: 2018/9/4
  time: 23:47
  to change this template use file | settings | file templates.
--%>
<%@ page contenttype="text/html;charset=utf-8"%>
<html>
<head>
    <title>title</title>
</head>
<body>
<%--获取applicationscope的值--%>
application: ${applicationscope.get("applicationkey")}

<%--获取日期的的值--%>
<%
    if (application.getattribute("date")==null) {
        application.setattribute("date",new date());
    }
%>
<%--获取session的值--%>
<br/>
session:${sessionscope.get("sessionkey")}
<%
    if (session.getattribute("date")==null) {
        simpledateformat sdf = new simpledateformat("yyyy-mm-dd");
        string format = sdf.format(new date());
        session.setattribute("date",format);
    }
%>
<br/>
request:${requestscope.get("requestkey")}

<br/>
parameters:${parameters.name}

</body>
</html>

实现接口方式获取servlet的api

public class testactionaware implements applicationaware, sessionaware, requestaware, httpparametersaware {

    private map<string, object> application;
    private map<string, object> session;
    private map<string, object> request;

    /**
     * execute执行的方法
     *
     * @return
     */
    public string execute() {
        // 获取application的内容属性
        application.put("applicationkey", "applicationvalue");
        // 获取页面设置的application值
        system.out.println(application.get("date"));
        // 设置session值
        session.put("sessionkey", "sessionvalue");
        // 设置request的值
        request.put("requestkey", "requestvalue");

        return success;
    }

    @override
    public void setapplication(map<string, object> map) {
        this.application = map;
    }

    @override
    public void setsession(map<string, object> map) {
        this.session = map;
    }

    @override
    public void setrequest(map<string, object> map) {
        this.request = map;
    }

    /**
     * 注意parameteraware已经过时需要httpaware进行设置值,并且不能直接调用put方法,否则会报
     * httpparameters are immutable, you cannot put value directly! 的错误
     * @param httpparameters
     */
    @override
    public void setparameters(httpparameters httpparameters) {
        map<string, parameter> newparams = new hashmap<string, parameter>();
        for (string key : httpparameters.keyset()) {
            parameter p = httpparameters.get(key);
            if (p instanceof parameter.request) {
                newparams.put(key, new parameter.request(key, new
                        string[]{ p.getvalue()}));
            }
        }
        httpparameters.appendall(newparams);
    }
}

其中struts2.xml中配置如下:

<struts>
    <package name="default" namespace="/" extends="struts-default">
     <action name="actionaware" class="com.codegeekgao.actionaware.testactionaware" method="execute">
        <result>/testactionaware.jsp</result>
    </action>
    </package>
</struts>

成功后跳转的jsp页面

<%@ page import="java.util.date" %><%--
  created by intellij idea.
  user: donniegao
  date: 2018/9/4
  time: 23:47
  to change this template use file | settings | file templates.
--%>
<%@ page contenttype="text/html;charset=utf-8"%>
<html>
<head>
    <title>title</title>
</head>
<body>
<%--获取applicationscope的值--%>
application: ${applicationscope.get("applicationkey")}

<%--获取日期的的值--%>
<%
    if (application.getattribute("date")==null) {
        application.setattribute("date",new date());
    }
%>
</br>
<%--获取sessionscope的值--%>
session: ${sessionscope.get("sessionkey")}
</br>
<%--获取sessionscope的值--%>
request: ${requestscope.get("requestkey")}
</body>
</html>