为什么EL表达式能访问struts2的值栈
简单地说,struts2对默认的request进行了包装,默认会先从原request里面找,如果找不到,会从值栈里面找;
具体实现细节:
public class StrutsRequestWrapper extends HttpServletRequestWrapper {
public StrutsRequestWrapper(HttpServletRequest req) {
super(req);
}
public Object getAttribute(String s) {
......
ActionContext ctx = ActionContext.getContext();
Object attribute = super.getAttribute(s);//先从request范围获取属性值
if (ctx != null) {
if (attribute == null) {//如果从request范围没有找到属性值,即从ValueStack中查找对象的属性值
......
ValueStack stack = ctx.getValueStack();
attribute = stack.findValue(s);
......
}
}
return attribute;
}
}
总结一下答案:
①JSTL能访问Action中通过request.setAttribute("")设置的值。这是大家所熟悉的。
②JSTL能访问Action中属性(通过getXXX方法实现访问)。
因为ValueStack存在于request,所以用${tip}访问时的顺序是:request先访问ValueStack,ValueStack从中找出tip对应的值