Chantz
Chantz

Reputation: 5963

HOWTO set parameter via "result" tag in struts2 and retrieve it in the resulting template jsp

I have a workflow where I need to render an exception page. But this exception page is generic and can be called form various situations. I need to display some custom text in each of these exception pages. So I figured I can set some "param" in the result tag & it will be automatically available in the eventual jsp page. But I am unable to retrieve (or set) it. Here is my relevant code -

struts.xml -

 <global-results>
        <result name="TV_ACCESS_NOT_ALLLOWED">
            <param name="location">/jsp/base/exception/accessNotAllowedException.jsp</param>
            <param name="appDisplayName">Television</param>
        </result>
        <result name="RADIO_ACCESS_NOT_ALLLOWED">
            <param name="location">/jsp/base/exception/accessNotAllowedException.jsp</param>
            <param name="appDisplayName">Radio</param>
        </result>            
    </global-results>

In my JSP page I tried below options -

<gs:property value="%{appDisplayName}" />
<gs:property value="#appDisplayName" />
<gs:property value="appDisplayName" />
<gs:property value="%{param.appDisplayName}" />
<gs:property value="#param.appDisplayName" />
<gs:property value="param.appDisplayName" />

None of them worked. If you guys can point me to right direction it will be appreciated. Also is there a way to print all the variables available in the current context incl. session, page, request scopes?

UPDATE I ended up resolving the issue by setting up variable in the parent action, which originally triggered the exception. I did not want to do this originally because I did not want to pollute the parent action, but I modified my template for handling normal flow of things to make use of the new variable.

Upvotes: 1

Views: 1382

Answers (1)

Quaternion
Quaternion

Reputation: 10458

Use global exception mappings for this purpose not a result, well you'll need a result but it is the exception mapping that resolves the result see here: https://cwiki.apache.org/WW/exception-handling.html

I'm not sure but I would manually print out want I want, the #session is a map of iteratable items so the following should work (not tested):

<h1>From Session</h1>
<s:iterator value="#session">  
 Session Key: <s:property/>
 <ol>
 <s:iterator>
   <li><s:parameter/></li>
 </s:iterator>
 </ol>
</s:iterator>

You would need to do the above for #application, #request, #page... whatever.

An easier way if you don't mind json... is to create an action with all the "Aware" interfaces that you would like with a json result type (using the struts2-json plugin) what is really nice about this is the json plugin will serialize all the public members of those objects no matter how deeply nested.

Upvotes: 1

Related Questions