Previously, using the SSH framework, the queried data is always stored in the session or request in the action, and then transmitted to the foreground for display; but then I saw some code declare a list or other member variables in the action, and then assign the queried data to this member variable, then, the JSP page can be displayed on the foreground through $ {list} or $ {variable name. I have never understood the reason why this is feasible after reading the materials:
Use $ {tip} of jstl to access the tip attribute defined in Action: Private string tip,
$ {Tip} is actually the tip in the access request scope. It actually calls the gettip () method in action, so it can be accessed.
Struts2 then saves the value stack in the request. You can get it through request. getattribute ("struts. valuestack.
After my analysis and research, the conclusions are as follows:In fact, the data is stored in requestcontext, And the request object and its session and other objects are placed in actioncontext. The ognl expression is used to obtain the objects stored in requestcontext from actioncontext, jstl is used to directly obtain objects from the request.
That is, jstl is used for direct acquisition, while ognl is used to indirectly obtain data in the request object from the actioncontext.
Actioncontext contains a large amount of environment information, including locale, application, session, and valuestack.
View the source code of this class and find that it is decorated with the getattribute () method:
Java code
?
123456789101112131415161718192021222324252627282930 |
public
Object getAttribute(String s) {
if
(s != null && s.startsWith(
"javax.servlet" )) {
// don't bother with the standard javax.servlet attributes, we can short-circuit this
// see WW-953 and the forums post linked in that issue for more info
return
super .getAttribute(s);
}
ActionContext ctx = ActionContext.getContext();
Object attribute =
super .getAttribute(s);
boolean
alreadyIn = false ;
Boolean b = (Boolean) ctx.get( "__requestWrapper.getAttribute" );
if
(b != null ) {
alreadyIn = b.booleanValue();
}
// note: we don't let # come through or else a request for
// #attr.foo or #request.foo could cause an endless loop
if
(!alreadyIn && attribute == null
&& s.indexOf( "#" ) == - 1 ) {
try
{
// If not found, then try the ValueStack
ctx.put( "__requestWrapper.getAttribute" , Boolean.TRUE);
ValueStack stack = ctx.getValueStack();
if
(stack != null ) {
attribute = stack.findValue(s);
}
}
finally {
ctx.put( "__requestWrapper.getAttribute" , Boolean.FALSE);
}
} |
Conclusion:
El can access the content in the value stack.
This method is accessible because El can access the getattribute () method of the request. This method is decorated by sturts2, and actioncontext can be accessed to access the value stack.
Summary:
Why can jstl access the attributes in action, such:
Private string tip;
Public String gettip (){
Return this. Tip;
}
You can use $ {tip} to access the tip attribute on the JSP page.
Summary:
① Jstl can access the value set through request. setattribute ("") in action. This is familiar to everyone.
② Jstl can access the attributes in Action (access through the getxxx method ).
Because valuestack exists in the request, the order in which $ {tip} is accessed is: request first accesses valuestack, and valuestack finds the corresponding value of tip.