【问题标题】:Why the interceptor brokes the wildcard in Struts 2?为什么拦截器会破坏 Struts 2 中的通配符?
【发布时间】:2014-03-08 08:51:42
【问题描述】:

我有这个带有通配符的操作:

@Namespace("/posts")
public class SearchPostBeansAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    private static Logger logger = Logger.getLogger(SearchPostBeansAction.class); 
    
    @Override
    @Actions({
        @Action(value="/{search1}/{param1}/",results={ 
            @Result(name=ACTION_SUCCESS,location="classic.main.general", type="tiles")})    
    })
    public String execute() throws Exception {
           logger.info("Action: " + getInvocatedURL() );
           String forward = SUCCESS;
           logger.info("getSearch1( " + getSearch1() + " )");
           logger.info("getParam1( " + getParam1() + " )");
           return forward;
    }
}

执行的结果:

 - INFO (com.silver.front.view.actions.SearchPostBeansAction) - Action:
   /posts/category/cars/
   
 - INFO (com.silver.front.view.actions.SearchPostBeansAction) -   
   getSearch1( category )

 - INFO (com.silver.front.view.actions.SearchPostBeansAction) -   
   getParam1( cars )

如果我拦截该操作:

@InterceptorRef("seoFilter")
@Namespace("/anuncios")
public class SearchPostBeansAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
...
}

执行的结果:

 - INFO (com.silver.front.view.actions.SearchPostBeansAction) - Action:
   /posts/category/cars/
   
 - INFO (com.silver.front.view.actions.SearchPostBeansAction) -   
   getSearch1( null )

 - INFO (com.silver.front.view.actions.SearchPostBeansAction) -   
   getParam1( null) 

为什么会丢失通配符的参数?

这是拦截器:

public class SEOFilter implements Interceptor{
    private static final long serialVersionUID = 1L;
    private static Logger logger = Logger.getLogger(SEOFilter.class); 
    
    ActionSupport actionSupport = null;
    
    public String intercept(ActionInvocation invocation) throws Exception {
        actionSupport = (ActionSupport) invocation.getAction();
        actionSupport.execute();
    }
}

【问题讨论】:

  • 你怎么看为什么会丢失通配符参数?
  • 当我尝试获取 search1 或 param1 时,我得到一个空值 :(
  • 先尝试设置然后就可以获取了。

标签: java configuration struts2 interceptor wildcard-mapping


【解决方案1】:

这是因为您绕过了拦截器堆栈上的所有拦截器。

要使用Interceptor,您需要调用invocation.invoke() 来告诉struts 通过Interceptor 堆栈的其余部分继续处理请求。

您手动调用该操作从而绕过拦截器堆栈。

public String intercept(ActionInvocation invocation) throws Exception {
    return invocation.invoke;
}

【讨论】:

    【解决方案2】:

    错误是您将@InterceptorRef("seoFilter") 应用于动作类,按照惯例您应该知道它应用于类中的所有动作。删除它,如果您想在操作中添加自定义拦截器,请使用 @Action 注释。

    @Action(value="/{search1}/{param1}/", results={ 
            @Result(name=ACTION_SUCCESS,location="classic.main.general", type="tiles")},
       interceptorRefs={@InterceptorRef("seoFilter"),@InterceptorRef("defaultStack")
    })    
    

    假设seoFilter 是一个不会破坏堆栈的有效拦截器。

    这是有效拦截器的代码

    public String intercept(ActionInvocation invocation) throws Exception {
        // here you can place the code that used to intercept the action
        ...
        //finally
        return invocation.invoke();
    }
    

    由于您没有发布 struts.xml,而我看不到您是如何配置 Struts 以使用通配符映射的,所以我将从文档页面为您提供 advanced wildcards 的参考来做自己动手。

    【讨论】:

    • 不起作用,我仍然遇到同样的错误。如果我删除拦截器,它运行完美。但我出于其他原因需要那个拦截器:/
    • 我说它应该是一个不会破坏堆栈的有效拦截器。您不应该从拦截器执行操作,因为该操作应该在拦截器堆栈的底部执行。您只需在修复此答案的堆栈后继续调用它。
    • 我删除了 actionSupport.execute() 并放入了 invocation.invoke(),但我仍然收到同样的错误 :(
    • getter 和 setter 提供给 params 拦截器(它已经在堆栈中),我以为您配置为在操作名称中使用通配符参数。
    • 你给了我一个线索,我发表了答案,非常感谢。
    【解决方案3】:

    我得到了我想要的!!! :)

    非常感谢鲍里斯和罗曼。

    我只是定义了一个拦截器堆栈。

    <interceptors> 
        <interceptor name="seoFilter" class="com.silver.usaditos.admin.SEOFilter"></interceptor>
        <interceptor-stack name="defaultInterceptorStack">
            <interceptor-ref name="defaultStack"/>
            <interceptor-ref name="seoFilter"/>
        </interceptor-stack>
    </interceptors>
    

    【讨论】:

      猜你喜欢
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多