Is there a way to declare a action result? - struts2

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.

Related

struts2 tokensession not setting request in Action excludedMethod [duplicate]

This question already has an answer here:
Prevent same action called twice as long as user is in current session
(1 answer)
Closed 5 years ago.
I'm trying to avoid double-submit problems using tokenSession. My action methods are working fine without tokenSession technique.
I add <s:token/> in upsert_crypto_sources.jsp and tokenSession interceptor in struts.xml but I receive request as null in my action excludedMethod of list().
The list page doesn't need to avoid double submit problem but if I add <s:token/> in view_crypto_sources_list.jsp and remove list() from excludedMethod then I always receive result invalid.token.
My struts.xml is like:
<struts>
<package name="key-manager" namespace="/shared/km" extends="console-default" strict-method-invocation="true">
<action name="manage_cs_*" method="{1}" class="console.shared.km.ASC_ManageCryptoProfilesAction">
<interceptor-ref name="tokenSession">
<param name="excludeMethods">
list, initInsert, load, delete
</param>
</interceptor-ref>
<result name="list">/shared/km/view_crypto_sources_list.jsp</result>
<result name="insert">/shared/km/upsert_crypto_sources.jsp</result>
<result name="update">/shared/km/upsert_crypto_sources.jsp</result>
<result name="load">/shared/km/upsert_crypto_sources.jsp</result>
<allowed-methods>list, insert, load, update, delete, testConnection, forward, cancel</allowed-methods>
</action>
My action implements ServletRequestAware interface therefore it gets the request member variable set using setServletRequest() method.
I added a defaultStack interceptor and it is working fine:
<struts>
<package name="key-manager" namespace="/shared/km" extends="console-default" strict-method-invocation="true">
<action name="manage_cs_*" method="{1}" class="console.shared.km.ASC_ManageCryptoProfilesAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="tokenSession">
<param name="excludeMethods">
list, initInsert, load, delete
</param>
</interceptor-ref>
<result name="list">/shared/km/view_crypto_sources_list.jsp</result>
<result name="insert">/shared/km/upsert_crypto_sources.jsp</result>
<result name="update">/shared/km/upsert_crypto_sources.jsp</result>
<result name="load">/shared/km/upsert_crypto_sources.jsp</result>
<allowed-methods>list, insert, load, update, delete, testConnection, forward, cancel</allowed-methods>
</action>

How to redirect with parameter

<action name="AddedPaid" class="iland.payment.SupplierPaidAction" method="insert">
<result name="success" type="redirect">ShowPaid</result>
<result name="input">/pages/payment/addToPay.jsp</result>
<result name="login">/pages/login.jsp</result>
</action>
<action name="ShowPaid" class="iland.payment.SupplierPaidAction" method="fetchAllByfk">
<result name="success">/pages/paid/showPaidDetails.jsp</result>
<result name="input">/pages/payment/ShowPay.jsp</result>
<result name="login">/pages/login.jsp</result>
</action>
Here AddedPaid Action is used to add form data in to database.
After adding data in to database I am redirecting result to ShowPaid action.
This is working properly.
Now I want whenever I redirect AddedPaid action to ShowPaid.
ShowPaid must show data of perticular supplierPaymentId for which I have added data in AddedPaid.
After redirect it is howing url
http://localhost:8082/ClothStore/ShowPaid
I want
http://localhost:8082/ClothStore/ShowPaid?supplierPaymentId=4
It's strange, usually people have 2 and want 1 :)
Btw, since you are using PostRedirectGet, you need to manually pass the parameter in Struts configuration.
Assuming you have a variable named supplierPaymentId with getter and setter, it's achievable like follows:
<action name="AddedPaid" class="iland.payment.SupplierPaidAction" method="insert">
<result name="success" type="redirectAction">
<param name="actionName">ShowPaid</param>
<param name="supplierPaymentId">${supplierPaymentId}</param>
</result>
<result name="input">/pages/payment/addToPay.jsp</result>
<result name="login">/pages/login.jsp</result>
</action>
Also use redirectAction instead of redirect, that is meant to be used to redirect to external URLs or non-Action URLs
First of all use redirectAction result instead of redirect to redirect to another action.
And use param tag to add parameters in your result configuration.
<result type="redirectAction">
<param name="actionName">ShowPaid</param>
<param name="supplierPaymentId">${supplierPaymentId}</param>
</result>
Note you need to have getter/setter for supplierPaymentId in your action class.

Strange effects when chaining results in struts 2

When an error event occurs I am chaining the result to an error action, this works okay when its all happening in the same namespace but when chaining across namespaces the results of the chained action are in the chaining actions namespace.
<package name="mynamespace-htm" namespace="/mynamespace/htm" extends="mybase-base">
<action name="newsItem" class="com.mypackage.control.GetNewsItemAction">
<result name="success">newsItem.jsp</result>
<result name="error" type="chain">
<param name="actionName">invalidSite</param>
<param name="namespace">/mynamespace</param>
</result>
</action>
</package>
<package name="mynamespace" namespace="/mynamespace" extends="mybase-base">
<action name="invalidSite" class="com.mypackage.control.GetInvalidSiteAction">
<result name="success">commonviews/invalidSite.jsp</result>
</action>
</package>
If I access the newsItem action in a way that causes an error result I get an exception javax.servlet.ServletException: File "/mynamespace/htm/commonviews/invalidSite.jsp" not found why is it trying to find the file relative to the original namespace mynamespace/htm instead of mynamespace?

Reuse of redirect-action in struts.xml?

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;

How to send parametrs to the url in struts.xml

Hi any body know how to send parametrs to the url in struts.xml
mycode is
<action name="deleteDocDetails" class="com.myDrDirect.doctor.action.DoctorEditAction"
method="deleteSingleDoctor">
<result name="success" type="redirect">salesManDoctorhome </result>
</action>
i want to pass searchIndicator=Allsearch&search=&pageLink=doctor these variable to this parameter.
i want my url to be like this salesManDoctorhome?searchIndicator=Allsearch&search=&pageLink=doctor
i have already tried this
<action name="deleteDocDetails" class="com.myDrDirect.doctor.action.DoctorEditAction"
method="deleteSingleDoctor">
<result name="success" type="redirect">salesManDoctorhome </result>
<param name="searchIndicator">Allsearch</param>
<param name="pageLink">doctor</param>
</action>
But it is not working..
the variable i am sending is static variable. I am using struts2 in my project
You may want use variable in url, you may have {variable name} this variable must have public get method in action.

Resources