I am building struts2 parameters values from variables and in one case it works and in the other it doesn't. Here is the 'result' from a menu item click :
<result name="WidgetList" type="redirectAction">
<param name="actionName">actList</param>
<param name="object">Widget</param>
</result>
And the Action mapping :
<action name="actList" class="MyClass" method="execute">
<interceptor-ref name="newStack" />
<result name="success">
<param name="location">jsp + ${object} + List.jsp</param>
</result>
<result name="Edit" type="redirectAction">
<param name="actionName">actEdit + ${object}</param>
</result>
</action>
In the 'Edit' result (which is returned after the user clicks a Widget in the list) the actionName is built correctly and that action runs to display a page.
actEdit + ${object}
becomes :
actEditWidget
However the menu click that redirects to 'actList' does NOT get build correctly, even though the ${object} parameter is correctly replaced.
jsp + ${object} + List.jsp
becomes the string :
"jsp+Widget+List.jsp"
And I get :
HTTP Status 404 - /MyApp/jsp+Widget+List.jsp
Why does this work in one case and not the other... and how can I 'fix' this?
Dave is right - just squishing it together does work, although it looks really ugly.
jsp${object}List.jsp
I'm still curious to know WHY there is a difference, but for some reason the "location" param above respects every character in the param value. However the "actionName" param strips internal spaces and reads both + and || as concatenations. So this works :
a || c tEdi + t${object}
Makes no sense to me but at least I can do what I wanted to.
Thanks Dave!
Related
I am developing a web application using struts2 .. In that I am downloding a file when user clicks a link ..
When user clicks the link I am checking in my action class whether requested file is existed or not if it is existed then it is working fine ...
WHen it is not existed I am giving a action error message .. But That page is redirecting to error page that i have mapped globally .. IN my console ther is no exception message except these error lines ..
Can not find a java.io.InputStream with the name [fileInputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
And I can see that action message in my error page ... I have debugged the problem also .. Mapping also perfect ..
<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
<param name="bufferSize">1024</param>
</result>
Is It possible to map back to the same JSP when The file is not existed in SYSTEM .. Thanks
Check whether the file exists or not in the action, and return success & error respectively. You can do something like this :
<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
<param name="bufferSize">1024</param>
</result>
<result name="error">/error.jsp</result>
error.jsp
<s:actionerror/>
<s:actionmessage/>
or your hard-coded message
But since, you need to go back to the same page, then I suggest calling the download action using ajax and return 200 on success and 204 on error something like this :
<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
<param name="bufferSize">1024</param>
</result>
<result name="error" type="httpheader">
<param name="status">204</param>
</result>
The ajax call can be something like - docs
$.ajax({
statusCode: {
404: function() {
alert( "page not found" );
},
204: function() {
alert( "file not found" );
}
}
});
I have a page where I'm "Viewing session info" (where session in this context is a business element. Think of a session like a training session). Part of this session info is a list of files. (An example of a session file would be the sign-in sheet of all the people who attended the session.) Clicking the link at the end of the list of files will delete the file from the session via a method called `inativeateSe
In my struts config for inactivateSessionMaterial, I have the following result
<result name="success">
/secure/courses/sessions/view_session_info.jsp
</result>
But this is insufficient. It really needs to be view_session_info.jsp?sessionId=1234. How can I add that variable (session id) to the end of this? Something like
<result name="success">
/secure/courses/sessions/view_session_info.jsp?sessionId=$sessionId
</result>
Just use <param> tag inside your action for all results in this action
<action>
<param name="sessionId">${sessionId}</param>
<result>...</result>
</action>
or for specific action result.
<action>
<result>
<param name="location">...</param>
<param name="sessionId">${sessionId}</param>
</result>
</action>
I have multiple actions that after completion redirect back to a general page (showStuff). I'm looking for a way to NOT repeat the list of parameters for every redirect-action.
What I have is this:
<action name="doThis" class="com.domain.package.MyAction" method="doThis">
<result type="redirectAction">
<param name="actionName">showStuff</param>
<param name="parse">true</param>
<param name="selectedYear">${selectedYear}</param>
<param name="selectedMonth">${selectedMonth}</param>
<param name="selectedDay">${selectedDay}</param>
</result>
</action>
<action name="doThat" class="com.domain.package.MyAction" method="doThat">
<result type="redirectAction">
<param name="actionName">showStuff</param>
<param name="parse">true</param>
<param name="selectedYear">${selectedYear}</param>
<param name="selectedMonth">${selectedMonth}</param>
<param name="selectedDay">${selectedDay}</param>
</result>
</action>
I would like to keep the parameter list within the showStuff action definition, and then use is like so:
<action name="doThis" class="com.domain.package.MyAction" method="doThis">
<result type="redirectAction">
<param name="actionName">showStuff</param>
</result>
</action>
<action name="doThat" class="com.domain.package.MyAction" method="doThat">
<result type="redirectAction">
<param name="actionName">showStuff</param>
</result>
</action>
Is it possible?
There are a few options.
Honestly, I'd skip most of my workarounds, and put them into session.
Once they're in session, create an interceptor and interface (Dateable or something). In the interceptor check the session for the variables (see below) and if the action is a Dateable, set them on the action, and you're done.
Another option is to encapsulate these variables as a date and either use the built-in converter or use your own converter. Then you'd only need a single parameter. This option would work with the interceptor idea as well.
As it turns out, it is very much possible. This is how you do it:
Add a global result:
<global-results>
<result name="show-stats" type="redirectAction">
<param name="actionName">showStats</param>
<param name="parse">true</param>
<param name="selectedYear">${selectedYear}</param>
<param name="selectedMonth">${selectedMonth}</param>
<param name="selectedDay">${selectedDay}</param>
</result>
And then for the actions:
<action name="doThis" class="com.domain.package.MyAction" method="doThis"/>
<action name="doThat" class="com.domain.package.MyAction" method="doThat"/>
Finally in the java code, just:
return "show-stats";
And you're done.
As a sidenote, why do I have to spend so much time trying to adhere to the very basic DRY principle? Aren't all these frameworks supposed to .. you know.. simplify stuff? Just wondering...
I was facing the same problem with an endless list of params getting longer and longer, repeated in several places. What I ended up doing was that I created an external file and declared it in struts.xml as an entity then included it instead of repeating all the params
This goes in the doctype tag
<!ENTITY referenceName SYSTEM "fileName">
Then you include it like so
&referenceName;
I have this exact same line in 5 places in my struts xml -
<result name="error" type="json"><param name="root">response</param></result>
Is there a way i can declare this as some sort of custom result and include it in the 5 places i'm using it?
You dont have to use it at multiple places instead define this as global result.
<global-results>
<result name="error" type="json">
<param name="root">response</param>
</result>
</global-results>
So when your action will return error it will use this result from the global result and use it.
But if you want something like
<action name="someaction" class="somepackage.someAction">
<result name="error" type="json">ReferSomeOhterResult</result>
</action>
this is not possible, you can only chain, redirect to a different action but one result cannot refer to another result.
In the Below code,
<result type="redirectAction" name="success">
<param name="actionName">
loadManagePrdtprty{1}?prtycrnid=${prtCrnId}&exitWindow=${exitWindow}
</param>
</result>
application is not getting start..What will be the problem.
I don't think that
loadManagePrdtprty{1}?prtycrnid=${prtCrnId}&exitWindow=${exitWindow}
is your Action's name. You can look up on how o use redirectAction here : http://www.vitarara.org/cms/struts_2_cookbook/post_and_redirect