Avoid duplicate Struts2 form Submission when clicking on refresh button - struts2

I am using struts 2 and i want to avoid the duplicate form submission when clicking on refresh button...After getting the result from the Action class, i am coming back to the same jsp and it holds the value entered and also the message that it has been successfully submitted but the problem is that when i refresh the page..it again tries to submit the request....for that i am trying to use Token Interceptor available in struts 2...but i think I'm missing something...
<package name="FOCAccept" extends="struts-default">
<action name="focSubmitRequest" class="bpel.invoke.action.FOCAcceptAction" method="execute">
<interceptor-ref name="token"/>
<result name="invalid.token">/postfocaccept.jsp</result>
<result name="input">/postfocaccept.jsp</result>
<result name="success">/postfocaccept.jsp</result>
<result name="failure">/postfocaccept.jsp</result>
<result name="invalid" type="redirectAction">LogoutCred.action</result>
</action>
</package>

I have use this and it is working fine for me...at anyhow all it's matter is the output. i have added this in the jsp and when i presses the refresh button it does not hit the controller and also makes the entered field value empty..
here is the code......
$(document).bind('keypress keydown keyup', function(e) {
if(e.which === 116) {
$("#submitRequest_pon").val("");
$(".errorMessage").html("");
return false;
}
if(e.which === 82 && e.ctrlKey) {
return false;
}
});

Related

http status 404 in struts 2 result

im in learning struts 2.
i create a simple project that can add and list the PRODUCT that user add. list is my first page and show all product that added. it is my struts.xml file:
<struts>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="listProduct" />
<action name="listProduct" class="control.ProductHandler" method="list">
<result name="success">/list.jsp</result>
</action>
<action name="addProduct" class="control.ProductHandler" method="add">
<result name="success">/listProduct</result>
</action>
<action name="*Form">
<result>{1}.jsp</result>
</action>
</package>
</struts>
when i enter localhost:8080/product in then browser it show me list.jsp file. in this file i have a link that with it i can goto add.jsp file. href of this link is addForm.
in add.jsp file i have 3 text field and a submit button. when entered data added action class return "success" and i most goto localhost:8080/product (default page) but it show me :
HTTP Status 404 - /product/listProduct
this is my action file:
public String list(){
products=Database.get();
return "success";
}
public String add(){
if (add!=null){
Product product=new Product(name, producer, price);
Database.add(product);
}
return "success";
}
where is my mistake?
thanks.
In order to redirect to another action use redirectAction result type.
<action name="addProduct" class="control.ProductHandler" method="add">
<result name="success" type="redirectAction">listProduct</result>
</action>

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>

Passing messages from action to JSP in struts2

I am trying to use addActionMessage() and addActionError() to pass messages and error from actions (e.g. in execute()) to the forwarded page.
In the JSP, I use:
<s:if test="hasActionMessages()">
<s:actionmessage/>
</s:if>
to display such messages.
But no message is shown. I am wondering if anyone could give a fix on this problem, or suggest another solution. I am new to Struts and web development, and I am not sure what is a proper pattern for passing messages from actions to pages.
EDIT: action-mapping code and java code
<action name="myAddUser" class="org.any.backend.action.UserAdminAction" method="addUser">
<result name="success" type="redirectAction">myUserAdmin</result>
<result name="input" type="redirectAction">myUserAdmin</result>
</action>
Java code:
public String addUser() throws Exception {
// check duplicate
for (User u : userList)
if (u.getUserName().equals(userName)) {
addActionError("A user with the same user name already exists. Choose another user name. ");
return INPUT;
}
if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
User newUser = new User();
newUser.setUserName(userName);
newUser.setPassword(password);
userList.add(newUser);
addActionMessage("User " + userName + " added. ");
return SUCCESS;
} else {
addActionError("User name and password cannot be empty");
return INPUT;
}
}
Your code is right.
Maybe you are using a REDIRECT-ACTION result type, or a CHAIN.
They both will lose action messages and errors, forcing you to put them in session (and clean them once displayed) for this page.
EDIT: I'm assuming that you are using the block
<s:if test="hasActionErrors()">
<s:actionerror />
</s:if>
<s:if test="hasActionMessages()">
<s:actionmessage/>
</s:if>
and not only the posted one, or you will never see the errors, only the messages...
Use session.setAttribute(...) in Action Class. and <logic:present .... > tag in jsp.
hope this will help you.
As I have seen you are using redirect action, you can still keep the action error/messages using one of the interceptors.
http://struts.apache.org/2.3.1.2/docs/message-store-interceptor.html
Here's an example in the struts.xml
<action name="submit"
class="com.dummy.SubmitAction">
<interceptor-ref name="store">
<param name="operationMode">STORE</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success" type="redirectAction">
<param name="actionName">view</param>
<param name="asnid">${id}</param>
</result>
</action>
<action name="view"
class="com.dummy.ViewUserAction">
<interceptor-ref name="store">
<param name="operationMode">RETRIEVE</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" >
<param name="workflow.excludeMethods">execute</param>
</interceptor-ref>
<result name="success">pages/user.jsp</result>
</action>

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

Tiles2 + Struts2 xml validation

i have a problem, i have my login with Strits2, Tiles2 and Struts xml validations, when i run my webapp the first page is the login and the struts2 xml validator send me the error, how do i tell that for the first time do not validate until submit?
<action name="loginPage" class="com.webapp.login.action.LoginAction">
<result name="input" type="tiles">loginPage</result>
<result name="success" type="tiles">loginPage</result>
</action>
Whenever you want to go to a "blank form", you should execute the input() method of your Action. This tells Struts 2 that you do not want to perform any validation for the action:
<action name="loginPage" class="com.webapp.login.action.LoginAction"
method="input">
<result name="input" type="tiles">loginPage</result>
<result name="success" type="tiles">loginPage</result>
</action>
Then, you should submit your login form to another action definition that executes the default action execute(). Struts 2 will then try to perform Action validation:
<action name="loginPageSubmit" class="com.webapp.login.action.LoginAction">
<result name="input" type="tiles">loginPage</result>
<result name="success" type="tiles">loginPage</result>
</action>

Resources