Prevent re-execute action in struts - struts2

this is probably an old question, but I just couldn't find an answer anywhere...
is there any way to prevent re-execute action in struts (other than re-direct to another page) when user clicks the refresh or back button on the browser?

index.jsp
<form action='call_action'>
<input type='text' name='name'/>
<s:token name="token"></s:token>
</form>
In struts.xml
<action name="call_action" class="controller.ActionClass">
<interceptor-ref name="token" />
<interceptor-ref name="basicStack"/>
<result name="invalid.token">invalid_token.jsp</result>
<result name="success">signup.jsp</result>
</action>
add invalid_token.jsp in your project.if unfortunetly user clicked second time.it automatically forward to invalid_token.jsp Page.

Related

Struts 2 Double form submit - why?

I have a few simple actions:
<action name="edit" class="EditAction">
<result>/WEB-INF/jsp/form.jsp</result>
</action>
<action name="preview" class="PreviewAction">
<result>/WEB-INF/jsp/preview.jsp</result>
<result name="input">/WEB-INF/jsp/form.jsp</result>
</action>
<action name="store" class="StoreAction">
<result>/WEB-INF/jsp/confirmation.jsp</result>
<result name="input">/WEB-INF/jsp/preview.jsp</result>
</action>
<global-results>
<result name="invalid.token">/WEB-INF/jsp/invalidToken.jsp</result>
</global-results>
and classic scenario: user interacts with text inputs, press Save, view a Preview page and press Save on it to save data, without double-press buttons, refreshing page etc.
Why double submit can occurs in such situation?
Even I had double form submit problem in struts2 on browser refresh. I resolved the problem using post-redirect-get pattern to avoid double or duplicate form submissions. It happens because hitting "refresh page" for a reponse based on a POST request will re-issue the POST request.It repeats what you did to reach current page even for double button press. It happens for both success and error in struts2.
I suggets you to make this change where-ever necessary.
<action name="onStoreRedirect">
<result name="success" type="redirect">store</result>
</action>
<action name="store" class="StoreAction">
<result>/WEB-INF/jsp/confirmation.jsp</result>
<result name="input">/WEB-INF/jsp/preview.jsp</result>
</action>

submit and load the same form using struts2

I've a test.jsp which includes an html.using the following tag
<s:form action="showTrainingPage">
<s:include value="%{objBarcodeCertificationTaskForm.strTrainingPage}"></include>
<s:submit type="image" src="../../images/next.png"/>
</s:form>
The value is developed dynamically in action.
<action name="showTrainingPage" method="getTrainingDetails" class="certificationTask">
<result name="input" >test.jsp</result>
</action>
But this does not help. When i click on Next, it says the requested source is not available. Please let me know how can this be solved.

Struts2 token interceptor always fails

Im trying to make it so that once i submit this form i cannot hit the back button, but with the current configuration I cannot even get the page/form to load. I can't seem to figure out why "invalid.token" is always being triggered thus redirecting me to index.jsp no matter what I have the token tag in my form like im supposed to. If i use the "excludeMethods" filter and exclude View then my page loads but I can hit the back button freely so it still does not work properly. I have tried moving the interceptor-ref above and below my noLoginStack but it dosen't make a difference. Based on my debugging my actual java class isn't even being hit, so its failing before then. What am I doing wrong?
My action declaration:
<action name="viewAppointmentLetter" class="edu.ucr.c3.rsummer.controller.instructor.ManageAppointmentLetters">
<interceptor-ref name="noLoginStack"/>
<interceptor-ref name="token" />
<result name="invalid.token">/index.jsp</result>
<result name="error" type="redirectAction">index.do</result>
<result name="input">/instructor/assigned_appts.jsp</result>
<result name="view">/instructor/assigned_appts.jsp</result>
<result type="redirectAction">index.do</result>
</action>
My assigned_appts.jsp:
<s:form action="saveAppointmentLetter" onsubmit="return verifySubmit();">
<s:token name="token" />
.....
</s:form>
If its any clue I always get this in my console
WARN org.apache.struts2.util.TokenHelper - Could not find token name in params.
In struts2 the order of interceptor is very important. you should follow this order.
<interceptor-ref name="token"/>
<interceptor-ref name="noLoginStack"/>
USe TokenSession interceptor.Had to handle result by result name="invalid.token" in struts.xml in specific action.
The page from which your action is generated at that page you have to write <s:token> tag in the header

get invalid.token result at the first time run action

I use struts 2.1.8, I use <s:token> between <s:form> and </s:form>
in my struts.xml like that
............
<package name="user" namespace="/user" extends="struts-default">
<action name="login"class="user.UserAction">
<result type="tiles">login.view</result>
<interceptor-ref name="token"/>
<interceptor-ref name="basicStack"/>
<result name="invalid.token">/pages/error.jsp</result>
</action>
</package>
...............
at the first time, I run login.do action, I alway get error page. Plz give to me some suggestions thank in advance.
You need to add parameter to exclude method on which you dont want to apply interceptor
for example at the very first run I used populate method to populate my page
<interceptor-ref name="token">
<param name="excludeMethods">populate</param>
</interceptor-ref>
if you have more than one method than you can use comma to separate methods
<interceptor-ref name="token">
<param name="excludeMethods">populate,add,save,anyothermethod</param>
</interceptor-ref>
Best Luck !
Regards,
Asit Jaiswal
The problem is that your token is due.
If you choose to ignore the method, then you don't need to add the token tag

Struts 2.0 page navigation

The situation is something like this. There are three pages page1,page2,page 3
Situation
Page1 >> takes to >> Page 2 >> takes to >> Page 3
Page 2 is an intermediate page, which has links and links contains the params required by the action to reach page3.
<a href="<s:url action="gotoPage3" includeParams="none">
<s:param name="request.accountId"><s:property value="#parameters['accountFrom.accountId']"/></s:param>
<s:param name="request.accountFromId"><s:property value="#parameters['accountFrom.accountId']"/></s:param>
</a>
What i want now
I want to get rid of page 2 so that
page1 >> takes to >> page 3
What I did
In the struts config, I gave the outcome of action associated page1 to chain it action associated with the page2 link.
<action name="processPage1AndGoToPage3" class="dominos" method="processPage1AndGoToPage3">
<interceptor-ref name="out-of-office-interceptor"/>
<interceptor-ref name="paramsPrepareParamsStack"/>
<interceptor-ref name="remove-hibernate-filters-interceptor"/>
<interceptor-ref name="request-deleted-interceptor"/>
<result name="success" type="chain">gotopage3</result>
</action>
Problem
Now how do i pass the params to the action associated with the page2 (which takes you to the page 3). (I was passing them through link before)
Any pointer will be helpful
<action name="processPage1AndGoToPage3" class="dominos" method="processPage1AndGoToPage3">
<interceptor-ref name="out-of-office-interceptor"/>
<interceptor-ref name="paramsPrepareParamsStack"/>
<interceptor-ref name="remove-hibernate-filters-interceptor"/>
<interceptor-ref name="request-deleted-interceptor"/>
<result name="success" type="chain">gotopage3</result>
<s:param name="page2Param1" value="page2Value1"/>
<s:param name="page2Param2" value="page2Value2"/>
</action>
Note that page2Param1 and page2Param2 will be passed to processPage1AndGoToPage3 the same way as calling http://server/webapp/processPage1AndGoToPage3.action?page2Param1=page2Value1&page2Param2=page2Value2

Resources