【问题标题】:how to fetch params defined in the action url in Interceptor如何获取拦截器中操作 url 中定义的参数
【发布时间】:2020-09-12 20:56:42
【问题描述】:

我在 struts.xml 中定义了这样的动作

<action name="*/*/execute" class="com.test.project1.abc" method="execute">
        <param name="username">{1}</param>
        <param name="resource">{2}</param>

如何在拦截器中获取用户名和资源的值?

我已经使用

在动作类“com.test.project1.abc”中获取了这些值
ActionContext context = ActionContext.getContext();
Map<String, Object> params = context.getParameters();

但是,上述内容不会在拦截器中产生结果。那么在这种情况下我应该如何获取参数呢?

【问题讨论】:

  • 你的拦截器堆栈是什么样的?
  • 在动作名称之后使用参数,而不是之前。详情见this答案。

标签: java url struts2 struts2-interceptors


【解决方案1】:

你可以试试这样的:

public String intercept(ActionInvocation invocation) throws Exception {
    final ActionContext context = invocation.getInvocationContext();
    Map<String,Object> reqParams = (Map<String,Object>)context.get(ActionContext.PARAMETERS);

    /**
    * Your logic
    */

    return invocation.invoke();
}

【讨论】:

  • 但是为什么ActionContext.getContext().getParameters()返回的是查询参数而不是拦截器中的url参数?
  • @SivakamiSubbu 你能分享你的拦截器堆栈吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多