Struts2_Action操作三个域中的数据及获取参数
前言:
1. 如果我们想在Java程序中获取HttpServletRequuest、HttpSession、ServletContext, 可以使用Servlet原生提供的方法去extends HttpServlet,然后重写doGet与doPost
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
2. 但是在Struts2框架中,java程序不需要继承HttpServlet,而是使用了struts2自己的一套方法去获取原生Servlet对象及操作原生Servlet域;
2.1 解耦合(仅操作域数据):ActionContext类
2.1.1 ActionContext.getContext().get("request"):Map<String, Object>
2.1.2 ActionContext.getContext().getSession():Map<String, Object>
2.1.3 ActionContext.getContext().getApplication():Map<String, Object>
2.1.4 ActionContext.getContext().getParameters():HttpParameters
2.1.4.1 public class HttpParameters implements Map<String, Parameter>, Cloneable
2.2 耦合(获取Servlet原生对象进行操作):XxxAware接口
2.2.1 RequestAware
2.2.2 SessionAware
2.2.3 ApplicationAware
2.2.4 ParametersAware
- 操作三个域中的数据
- request(HttpServletRequest)
- session(HttpSession)
- application(ServletContext)
- 获取参数
- HttpParameters parameters = actionContext.getParameters()
一:操作三个域中的数据
public String save(){
// 操作域中数据
// page(X)
// request
// session
// servletContext -> Application
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> applicationScope = actionContext.getApplication();
applicationScope.put("ApplicationKey",new String[]{"ApplicationValue1","ApplicationValue2"});
Map<String, Object> sessionScope = actionContext.getSession();
sessionScope.put("SessionKey","SessionValue");
Map<String, Object> requestScope = (Map<String, Object>) actionContext.get("request");
requestScope.put("RequestKey", new Product());
return "success";
}
二:获取参数
HttpParameters parameters = actionContext.getParameters()
→ 可读不可写
public String save() {
// 操作域中数据
// page(X)
// request
// session
// servletContext -> Application
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> applicationScope = actionContext.getApplication();
applicationScope.put("ApplicationKey", new String[]{"ApplicationValue1", "ApplicationValue2"});
Map<String, Object> sessionScope = actionContext.getSession();
sessionScope.put("SessionKey", "SessionValue");
Map<String, Object> requestScope = (Map<String, Object>) actionContext.get("request");
requestScope.put("RequestKey", new Product());
// public class HttpParameters implements Map<String, Parameter>, Cloneable
HttpParameters parameters = actionContext.getParameters();
System.out.println(parameters.get("productId")); // Empty{name='productId'}
System.out.println(parameters.get("productName")); // CPU
System.out.println(parameters.get("productDesc")); // AMD
System.out.println(parameters.get("productPrice").getValue()); // 2590
System.out.println(parameters.get("productPrice").getName()); // productPrice
System.out.println(parameters.get("productPrice").isDefined()); // true
System.out.println(parameters.get("productPrice").isMultiple()); // false
System.out.println(Arrays.toString(parameters.get("productPrice").getMultipleValues())); // [2590]
System.out.println(parameters.get("productPrice").getObject()); // [Ljava.lang.String;@4152ca51
return "success";
}