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

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>
上一篇:UIViewController 生命周期


下一篇:Asp.net 主题