Two ways of getting Servlet api from Struts2 to solve the problem of obsolete ParameterAware

servlet API is obtained through ActionContext

Struts2 encapsulates HttpServletRequest,HttpSession and ServletContext, and constructs three Map objects to replace the above three objects. In Action, you can directly use the Map objects corresponding to HttpServletRequest, HttpServletSession, and ServletContext to save and read objects.

ActionContext is the context of action execution. Action is saved in ActionContext. All objects needed for execution, including parameters, request, session, application
  • Get the Map object corresponding to HttpSession: public Map getSession
  • Get the MAP object corresponding to ServletContext: public Map getApplication
  • Map object corresponding to request parameter: public Map getParameters()

code implementation

public class ActionContextTest {

    /**
     * execute method
     * @return String
     */
    public String execute() {
        // Get actionContext object
        ActionContext actionContext = ActionContext.getContext();
        // Get applicationMap
        Map<String, Object> applicationMap = actionContext.getApplication();
        // You can get ${applicationScope.get} through the jsp page
        applicationMap.put("applicationKey","applicationValue");
        // You can also get the value of the setting from the jsp page
        System.out.println(applicationMap.get("date"));

        // Add value to session
        Map<String, Object> sessionMap = actionContext.getSession();
        sessionMap.put("sessionKey","sessionValue");
        // Get the value of page setup
        System.out.println(sessionMap.get("date"));

        // Because the actionContext.getRequest() method is not provided in the underlying layer, you need to get it manually
        Map<String, Object> requestMap = (Map<String, Object>) actionContext.get("request");
        requestMap.put("requestKey","requestValue");

        // Get the details of the get parameters of the submitted url. It used to be a Map collection object, but now it is an implementation class of the Map collection. The Map implementation class can't add parameters and can only read (url submitted properties)
        HttpParameters parameters = actionContext.getParameters();
        System.out.println(parameters.get("name"));
        return SUCCESS;
    }

The configuration in Struts2.xml is as follows:

<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 to jump after success

<%@ 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>
<%--Obtain applicationScope Value--%>
application: ${applicationScope.get("applicationKey")}

<%--Get the value of the date--%>
<%
    if (application.getAttribute("date")==null) {
        application.setAttribute("date",new Date());
    }
%>
<%--Obtain session Value--%>
<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>

The api of getting Servlet by interface

public class TestActionAware implements ApplicationAware, SessionAware, RequestAware, HttpParametersAware {

    private Map<String, Object> application;
    private Map<String, Object> session;
    private Map<String, Object> request;

    /**
     * execute Method of execution
     *
     * @return
     */
    public String execute() {
        // Get content properties of application
        application.put("applicationKey", "applicationValue");
        // Get the application value of page settings
        System.out.println(application.get("date"));
        // Set session value
        session.put("sessionKey", "sessionValue");
        // Set the value of 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;
    }

    /**
     * Note that the ParameterAware is out of date, and HttpAWare is required to set the value, and the put method cannot be called directly, otherwise it will be reported
     * HttpParameters are immutable, you cannot put value directly! Mistake
     * @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);
    }
}

The configuration in Struts2.xml is as follows:

<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 to jump after success

<%@ 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>
<%--Obtain applicationScope Value--%>
application: ${applicationScope.get("applicationKey")}

<%--Get the value of the date--%>
<%
    if (application.getAttribute("date")==null) {
        application.setAttribute("date",new Date());
    }
%>
</br>
<%--Obtain sessionScope Value--%>
session: ${sessionScope.get("sessionKey")}
</br>
<%--Obtain sessionScope Value--%>
request: ${requestScope.get("requestKey")}
</body>
</html>

Tags: Java Session JSP Struts

Posted on Sat, 04 Jan 2020 02:33:09 -0800 by MetaDark