Just another weblog

Struggling with the tag

Posted by damuchinni on March 12, 2009

I found this to be another doozey, certainly it was partly my fault as I didn’t read the Struts2 documentation on this tag very well. The problem was there was only a very short description so I skipped to the examples as I thought they would be more informative.

Anyway, this tag acts as a way to include the result of an action in your JSP. Think of it like this:

Ok, so how do you use this tag, firstly here are the caveats about its use. Once you have these in your head your life will become magically easier:

This tag NEVER goes onto the value stack! This may not be strictly accurate but thinking of it this way is certainly useful. When you are within the body of the tag it is NOT possible to reference it in the normal way. You can apparently use the value=”top.myProperty” method (the keyword here is top which references the item on the top of the stack. If top doesn’t work try #top) but I didn’t try this. The reason for this is that the tag does not get an id or var value until AFTER the tag is closed;
Once the tag is closed it is NOT on the value stack at all! The item on the top of the value stack is the original action which called the JSP page which has the on it. Therefore, even though it now has a id or var attribute you cannot reference it. My guess is that this is because this is the second action that is executed in a single result. If the first action has a parameter called name and then the second action had a parameter called name as well, which value would go onto the value stack? As I say that reason is a guess but it would make sense to me. None of the discussions I read explained the reason why so I guess it is buried deep in frameworks architecture.
Here is some example code for you, this is in your JSP:

Notice that the tag is closed and does not have any value for the body in this case. This just suited my need but it can have a body. Just make sure you understand the above two bullet points to know which way you want it to work. This is the code for the execute method of my action:

String myPropertyToPutInTheRequestObject = “Hello World!”;
getServletRequest().setAttribute(“myPropertyKeyInTheRequestObject”, myPropertyToPutInTheRequestObject);
return SUCCESS;

What is going on in there? Well, remember that the action cannot be referenced because either it is not on the stack or it does not have a id or var? That means you cannot access it’s value as normal. To work around this we MUST put the property we want to access into the request object (or another place the JSP can access it such as PageContext, ServletContext etc.). Remember that this tag will not return the result if the parameter executeResult is not set to true. This means that we can return whatever we want as the result type and it will not make any difference. I return SUCCESS but you could return cabbage or footballs, the result will not be executed. If you need to change this behaviour then set executeResult to true. Next is the rest of the code for the JSP page, this goes after your tag has closed (in my case):

This spits out the value of the item we put in the request object during out action execution. We access the property in the request object using the #attr key which is a special key for looking in the available scopes. Notice that we do NOT access this in the standard way. We can’t access it in the standard way as there is no way to reference the action, remember it is never on the value stack after the has closed and we need to use the top keyword if we are within the body of the tag.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s