0

我正在尝试在 struts 中创建一个拦截器,它设置了几个类变量(我想在标题页中使用)。这就是我所做的

struts.xml

 <interceptors>
        <interceptor class="com.googlecode.sslplugin.interceptors.SSLInterceptor" name="secure" />
        <interceptor class="org.my.action.HeaderInterceptor" name="headerInterceptor" />
        <interceptor-stack name="myStack">
            <!-- TODO : uncomment this before release
            <interceptor-ref name="secure">
                <param name="useAnnotations">true</param>
                <param name="httpsPort">443</param>
                <param name="httpPort">80</param>
            </interceptor-ref>
            -->
            <interceptor-ref name="headerInterceptor" />
            <interceptor-ref name="defaultStack"/>
        </interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/> 

拦截器代码

public class HeaderInterceptor implements Interceptor {

private static final long serialVersionUID = 1L;

//added for inputs to HEADER
private String investorName;
private String investorImage;


@Override
public void destroy() {}

@Override
public void init() {}

@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
    setHeaderAttributes();
    return actionInvocation.invoke();
}


private void setHeaderAttributes()
{
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpSession session = request.getSession();
    Object invObj = session.getAttribute(RangDeServerUtils.USER);
    if( null != invObj && invObj instanceof Investor){
    Investor investor = (Investor) invObj;
        this.investorName = investor.getFirstName();
        this.investorImage = Integer.toString(investor.getImageId());
    }
}

//have removed getters and setters for the class variables

}

在每个请求中,拦截器都会被命中并设置类变量,但它们不会显示在 jsp 上。

有什么我做错了吗?请帮忙。

4

2 回答 2

0

我理解所写的问题,但是关于这是否是您真正想要/需要/应该做的事情似乎存在争议。不管是哪种情况,你如何使用拦截器来设置 Actions 类的值。

查看参数拦截器的来源

我建议您下载当前版本的源代码,看看它是如何工作的。

public String intercept(ActionInvocation invocation){
   //get the current action
   Object action = invocation.getAction();
   //figure out if your action supports what you want to do, either check its
   // interface or use reflection (or apache Bean/Property Utils) to see if it 
   // supports the properties you are interested in...
   if(action typeof MyInterface){
       MyInterface mi = (MyInterface)action;
       //set what you need how you need it
   }


   return invocation.invoke();
}
于 2013-01-18T23:14:14.363 回答
0

而不是做

    this.investorName = investor.getFirstName();
    this.investorImage = Integer.toString(investor.getImageId());

在拦截器中,我做到了

request.setParameter("investorName", investor.getFirstName());
request.setParamter("investorImage", Integer.toString(investor.getImageId()));

它奏效了。

于 2013-01-19T16:17:42.180 回答