In struts.xml file
<action name="myJasperTest" class="com.sample.SupplierEnquiryReport">
<result name="success" type="jasper">
<param name="location">/reports/xyz.jasper</param>
<param name="dataSource">myList</param>
<param name="format">PDF</param>
</result>
</action>
I need to return multiple pdf files after one action.
Is it possible?
No, but from your UI JSP you can:
open multiple actions, each one returning a PDF;
open another JSP, with multiple <iframe>, each one pointing (with src attribute) to a different Action (or better, to the same, but passing different parameters), and returning a PDF.
Then several pages each one with a PDF, or a big page with several iframes, each iframe with a PDF.
Related
Im wondering if it's possible to set a default result that would apply to every action in struts2.
For example to say that result "FOO" will always redirect to action BAR or to jsp "BAR.jsp" instead of adding <result name="FOO">xxx</result> to every action I define ...
That would help me to create a generic error page
Thanks
Yes, you can set global results for a package.
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirectAction">Logon!input</result>
</global-results>
For more details read Result Configuration Page
One quick solution that comes to my mind is global result configuration.Most often, results are nested with the action element. But some results apply to multiple actions. In a secure application, a client might try to access a page without being authorized, and many actions may need access to a "logon" result.
If actions need to share results, a set of global results can be defined for each package. The framework will first look for a local result nested in the action. If a local match is not found, then the global results are checked.
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirectAction">Logon!input</result>
</global-results>
For more details refer to the official doc
result-configuration
I am developing a web application in struts2. I have declared global properties in struts.xml like:
<constant name="struts.custom.i18n.resources" value="LableResources,MessageResources" />
How do I use those properties in struts.xml itself? I want to implement a system in which request URI names come from global properties file.
As far as I know, you can't do that. I18n resources are for messages an runtime stuff not for configuration.
You can use parameters in the configuration but at runtime they have to be available as request parameters or in the value stack.
Like this:
<result name="input" type="redirectAction">
<param name="actionName">${from}</param>
<param name="id">${id}</param>
</result>
I'm developing a Struts 2 project where I need to implement 2 friendly URLs. Since my 2 URLs need to be something like URL/name/id and URL/id/title, the only way I found to manager this was to use an 1st step action to process the request which then forwards to the correct Action1 or Action2, like this:
<action name="/*/*" class="web.ProcessRequestAction">
<param name="firstParam">{1}</param>
<param name="secondParam">{2}</param>
<result name="action1" type="chain">Action1</result>
<result name="action2" type="chain">Action2</result>
</action>
And have both actions defined as well like this:
<action name="Action1" class="web.Action1" method="execute">
<result name="success">/WEB-INF/content/Action1.jsp</result>
<result name="input">/WEB-INF/content/Action1.jsp</result>
<result name="error">/WEB-INF/content/Action1.jsp</result>
</action>
However, if I invoke URL/parameter/parameter, and then on ProcessRequestAction I return "Action1", I'll get the Action1.jsp (as intended) but all messy, seems like all the CSS is missing.
First of all, is my approach correct? If so, what I'm I doing wrong here, what can I improve?
Thansk in advance!
As to why your CSS isn't working, we have no way of knowing--a JSP is a JSP, and follows normal JSP/HTML rules.
As for the first issue: I'd recommend against action chaining; it's actively discouraged, and usually just makes a mess of things.
Without knowing more about your application architecture, it's difficult to give targeted advice, but you may find parameters in namespaces helpful.
Even if it's not immediately applicable, you may be able to play games with a PatternMatcher or ActionMapper that's more targeted to your actual needs--the implementation would depend on a variety of factors, particularly the format(s) of the name, id, and title URL components.
Here are a few resources that may help you achieve the clean URLs you're looking for:
Better URLs with Struts2
https://stackoverflow.com/questions/4772737/hidden-features-of-struts-2-framework/4837917#4837917
I'm using Struts 2.1.8.1. I have a requirement to embed some pages from another server on my own app, so the users will access to them through my application, without accessing directly the other server. My idea is to have a package definition for that, so any access to that package would be redirected to the internal server.
<package name="eco-marketing" namespace="/marketing" extends="eco-default">
<action name="*">
<result name="success" type="dispatcher">
<param name="location">http://myotherserver:8080/test/{1}</param>
</result>
</action>
</package>
But it does not work, I got a Error 404--Not Found, so I suppouse is not as easy as it sounds. Any ideas on how to do this?
TIA
I'll assume that you are accessing just html, then see: http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html
I would recommend creating an action in that package to do the work for you, and use the value of the parameter to get the required data.
You probably already know but an iframe in the consumer action will make this easier to use than trying to parse what you need out.
After you have that figured out, if you decide to create a custom result type, please post it back to us here it would be very interesting.
An example of a custom result type can be found here (4th code block from the top): http://siriwardana.blogspot.com/2008/12/creating-custom-result-type-struts-2.html
In my struts 2 project when using redirect action i m loosing all my values such as action error and field errors.
I looked it up on net and found 2 options
Chain - This isn't used much i donno why ..
MessageStoreInterceptor - This needs to be placed in every action
So can any one please let me know when is redirect(or RedirectAction) preferred and when is chain preferred.
Redirecting an action looses the current value stack (anything in request scope) you can of course set up your action to preserve these values by passing them as parameters to the next action, but it is a bit of a pain.
Chain preserves the value stack, so the next action can work on parameters created from the previous action without needing to explicitly pass them, also since there is this snow ball effect you can use all the parameters in the view.
But it is generally recognized that a top down solution (maybe top down isn't the best word... 'structured') is better than building a maze of spaghetti actions.
So when you're under pressure to get something working and not overly familiar with struts2 then use chain or redirection, and then definitely come back and fix it! In general you should use an interceptor.
In the event of an action that routes to other actions based on some condition it would be better to make that an interceptor apply that to a package and put all actions which require this interesting behavior in that package. Then it is very clear which actions this applies to.
First option
<action name="remove" class="com.action.firstAction" method="remove">
<result name="success" type="redirectAction">
secondaction
<param name="actionName">secondaction</param>
<param name="namespace">/</param>
<param name="param name">${param value}</param>
</result>
</action>
<action name="secondaction" class="com.action.secondAction" method="result">
<result name="success">result.jsp</result>
</action>
Another option
<action name="remove" class="com.action.firstAction" method="remove">
<result name="success" type="chain">secondaction</result>
</action>
<action name="second action" class="com.action.secondAction" method="result">
<result name="success">result.jsp</result>
</action>