struts2不能通过ONGL方式取出request中的Atrribute

请看下面一个很简单的Action

package com.ahgw.main.action;

import org.springframework.stereotype.Controller;

/**
 * Created with IntelliJ IDEA.
 * User: HYY
 * Date: 13-11-19
 * Time: 下午7:08
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class Test2Action {
    private String hyy;
    
    public String execute() {
        System.out.println("hyy = " + hyy);
        return "success";
    }

    public String getHyy() {
        return hyy;
    }

    public void setHyy(String hyy) {
        this.hyy = hyy;
    }
}

 一般来说,如果request有hyy这个parameter,那么可以获取该值。然后如果request中仅有key为hyy的Atrribute,那么这一段代码是获取不了该值的。

请继续看:

public class TestInterceptor extends AbstractInterceptor {

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        HttpServletRequest request = (HttpServletRequest) actionInvocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
        request.setAttribute("hyy", "heyiyong");
        System.out.println("经过测试拦截器");
        return actionInvocation.invoke();
    }
}
        <interceptors>
            <interceptor name="testInterceptor" class="com.ahgw.admin.interceptor.TestInterceptor"></interceptor>
        </interceptors>
<action name="test2Action" class="test2Action"> <interceptor-ref name="testInterceptor"></interceptor-ref> <result>/test.jsp</result> </action>

了解清楚后,在浏览器输入/test2Action.do发现输出如下:

经过测试拦截器
hyy = null

 

总结:在struts2的action中,通过OGNL方式不能获取atrribute而只能获取到parameter的值。

posted @ 2013-11-19 19:30  无忧之路  阅读(276)  评论(0编辑  收藏  举报
无忧之路