public class ParmValueAction extends ActionSupport{ private Usere user; @Override public String execute() throws Exception { ActionContext ctx = ActionContext.getContext(); HttpServletRequest req = ServletActionContext.getRequest(); Map<String, Object> requestMap = (Map<String, Object>) ctx .get("request"); ctx.put("userKey", user); requestMap.put("userReqMap", user); req.setAttribute("userReq", user); return SUCCESS; } public Usere getUser() { return user; } public void setUser(Usere user) { this.user = user; }}
JSP page:
Method 1: use the property tag <br> <s: property value = "user. name "/> <s: property value =" user. age "/> <br> Method 2: Use the property tag and # userkey to search for the value stack <br> <s: property value =" # userkey. name "/> <s: property value =" # userkey. age "/> <br> method 3: Use the property tag, use # action to search for the value stack, and use the default search method. This method is equivalent to method 1, this method is unavailable in 2.0, and 2.1 is available <br> <s: property value = "# action. user. name "/> <s: property value =" # action. user. age "/> <br> Method 4: Use the property tag and use # userreq to search for the value stack. This value cannot be found here, because the key <br> <s: property value = "# userreq. user. name "/> <s: property value =" # userreq. user. age "/> <br> Method 5: Use the property tag and use # userreqmap to find the value stack. This value is not found here, because the key <br> <s: property value = "# userreqmap. name "/> <s: property value =" # userreqmap. age "/> <br> <HR> request value method, which is saved to user: <br> <s: property value =" # request. userreqmap. name "/> <s: property value =" # request. userreqmap. age "/> <br> request value method, which obtains and stores the user: <br> <s: property value =" # request. userkey. name "/> <s: property value =" # request. userkey. age "/> <br> the request value method is used to obtain and store the user: <br> <s: property value =" # request. userreq. name "/> <s: property value =" # request. userreq. age "/> <br> method 1 of request value. The user in the action is <br> <s: property value =" # request. user. name "/> <s: property value =" # request. user. age "/> <br> method 2 of the request, and the user in the action is: <br> <s: property value =" # request. action. user. name "/> <s: property value =" # request. action. user. age "/> <br> <HR> the jstl value method is used to obtain and store the user: <br> <C: Out value =" $ {request. userreq. name} "> </C: Out> <C: Out value =" $ {request. userreq. age} "> </C: Out> <br> the jstl value method is used to obtain and save the user: <br> <C: Out value =" $ {request. userreqmap. name} "> </C: Out> <C: Out value =" $ {request. userreqmap. age} "> </C: Out> <br> the jstl value method is used to obtain and save the user: <br> <C: Out value =" $ {request. userkey. name} "> </C: Out> <C: Out value =" $ {request. userkey. age} "> </C: Out> <br> method 1 of the jstl value is used to obtain and save the user: <br> <C: Out value =" $ {request. user. name} "> </C: Out> <C: Out value =" $ {request. user. age} "> </C: Out> <br> method 2 of jstl value. The user: <br> <C: Out value =" $ {request. action. user. name} "> </C: Out> <C: Out value =" $ {request. action. user. age} "> </C: Out> <br> <HR> use Java encoding directly to obtain: <% usere user = (usere) request. getattribute ("userreq"); usere usermap = (usere) request. getattribute ("userreqmap"); usere userkey = (usere) request. getattribute ("userkey"); // action cannot be obtained in 2.0. The test version is 2.1.8.1 parmvalueaction action = (parmvalueaction) request. getattribute ("action"); // usere useraction = action. getuser (); // string name = useraction. getname (); // int age = useraction. getage (); %> get directly: <% = user. getname () %> <% = user. getage () %> <br> Get usermap: <% = usermap. getname () %> <% = usermap. getage () %> <br> Get userkey: <% = userkey. getname () %> <% = userkey. getage () %> <br> get action: <% = action. getuser (). getname () %> <% = action. getuser (). getage () %> </body>
Result:
Method 1: use the property tag
Terje 25
Method 2: Use the property tag and # userkey to search for the value stack.
Terje 25
Method 3: Use the property tag, use # action to search for the value stack, and use the default search method. This method is equivalent to method 1. In method 2.0, this method is unavailable and 2.1 is available.
Terje 25
Method 4: Use the property tag and use # userreq to search for the value stack. This value cannot be found because the key is not found in the value stack.
Method 5: Use the property tag and use # userreqmap to find the value stack. This value cannot be found here because the key is not found in the value stack.
The request value method is used to obtain and save the user in userreqmap:
Terje 25
Request value method, which is used to save the user in userkey:
Terje 25
The request value method is used to obtain and save the user in userreq:
Terje 25
Request Value Method 1, which is used to save the user in the action:
Terje 25
Method 2 of the Request value. Save the request with the user in the action:
Terje 25
The jstl value method is used to obtain and save the user in userreq:
Terje 25
The jstl value method is used to obtain and save the user in userreqmap:
Terje 25
The jstl value method is used to obtain and save the user in userkey:
Terje 25
Jstl Value Method 1: Save the user in action:
Terje 25
Jstl Value Method 2: Use action to save the user in action:
Terje 25
Use Java encoding directly to get: Terje 25
Usermap: Terje 25
Userkey: Terje 25
Take action: Terje 25
Cause: View
org.apache.struts2.dispatcher.StrutsRequestWrapper
Source code. The following code is packaged by the Apache community for the request. In this way, if the value cannot be found through the traditional value method on the page, it will be found in the value stack, in this way, not only the direct values of jstl and Java code are compatible, but also the struts2 tag can be obtained smoothly.
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); if (ctx != null) { if (attribute == null) { 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 && 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); } } } } return attribute; }