JavaWeb - 【Struts2】ActionContext

博客主要介绍了Struts2 Action操作三个域(request、session、application)中的数据,以及获取参数的方法。操作的数据域包括HttpServletRequest、HttpSession和ServletContext,获取参数可使用actionContext.getParameters()。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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";
}

在这里插入图片描述
在这里插入图片描述






在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值