Token Session Using tokens to prevent duplicate form submits? - struts2

I use Token Session to prevent duplicate form submits, but the first time I make a request to server, I always get error page
<action name="show" class="ClientAction">
<interceptor-ref name="tokenSession" />
<interceptor-ref name="basicStack" />
<result name="invalid.token">/WEB-INF/error.jsp</result>
result type="tiles" name="success">page.view</result>
</action>
"<s:token />" was added to may success page between <s:form> and </s:form>, but it doesn't run correctly.
plz help me to solve them, is there another way prevent duplicate form submits. I wait for suggestion, thank u very much. : )

It seems that you are not using proper interceptor name. If you want to use the session token, it is token-session.
try using token-session instead of tokenSession.
Hope that helps.

tag <s:token /> must be inserted into form which is double-submitted, not into success form. If token tag is missing, interceptor resolve the submitted request as invalid even if it's the first attempt.

Related

Struts won't pass parameter to jsp file

I have an action which (depending on the result) redirects to a suitable file:
<!-- /web/addaccount -->
<action name="addaccount" class="com.x.y.z.WebCreateAccountAction">
<result name="INVALIDLOGIN">/delete/confirm.jsp?err=SIGNIN</result>
<result name="ERROR">/delete/error.html</result>
</action>
For some reason, when redirecting to delete/confirm.jsp, the parameter erris not passed in. I'm at a loss to why this is happening. Is this an incorrect way of passing params to JSP via Struts?
Was just missing a:
type="redirect"
<result name="INVALIDLOGIN" type="redirect">/delete/confirm.jsp?err=SIGNIN</result>
Note: This is advised against, it would redirect directly to a JSP, which would be considered an S2 anti-pattern. See comments below.

accept attribute is not working in <s:file> tag of struts2

I am trying to validate the content types of uploading files using accept attribute but it seem to be not working.
Here is my code.
<s:file theme="simple" name="fileUpload" accept="image/jpeg"/>
i also tried
<s:file theme="simple" name="fileUpload" accept="image/*"/>
Both are not working what could be the problem?
The HTML accept attribute is not supported in IE and Safari. You can define allowed mime types in struts.xml for you file upload action like that:
<action name="..." class="...">
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">image/jpeg</param>
</interceptor-ref>
<result>...</result>
</action>
See others parameters you can configure in fileUpload interceptor.

struts2 Could not find token name in params

this is the configuration of struts:
<interceptors>
<interceptor-stack name="packStack">
<interceptor-ref name="token" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="packStack" />
and i got this warning and token interceptor is not blocking the duplicate submission of requests.
WARNING: Could not find token name in params.
i have searched the web and so many forums. how to solve this?? i have tried changing the order of the interceptors and at last i have arranged like above.
Use the <s:token> tag, otherwise there won't be a token parameter, sort of like the message says.

Unable to display Struts2 fieldError

I have created the index.jsp which send action request
<meta http-equiv="REFRESH" content="0;url=./radioButton.action">
After completing this the request is forwarded to radio.jsp. In this one i am showing country, state list etc.. (so i am redirecting for this from index.jsp).
Now i am imposing validations to this form. i have created the .xml correctly for validation.When the validation fail it will be redirected to ./radioButton.action
<result name="input" type="redirectAction">radioButton</result>
I have created one interceptor which extends MethodFilterInterceptor, for this to keep the action errors in session scope which having the follow logic in after() method.
if (fieldErrors != null && fieldErrors.size() > 0)
{
Iterator it = fieldErrors.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue().toString());
}
session.put(FIELD_ERRORS_KEY, fieldErrors);
}
public static final String FIELD_ERRORS_KEY = "RedirectMessageInterceptor_FieldErrors";
i have configured the interceptor in my .xml as below
<interceptors>
<interceptor name="redirectMessage" class="com.daya.message.RedirectMessageInterceptor" />
<interceptor-stack name="sessionStack">
<interceptor-ref name="redirectMessage" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptor-stack>
</interceptors>
I am getting the error message on my console befor putting into session scope. The output printed in console is
userName = [User Name is required to login]
password = [Password is required to login]
I am using the below tag to display the field error
<s:fielderror></s:fielderror>
But in the jsp after redirect (when validation failed) messages are not getting display.
Client side validation is working well. I disabled javascript for checking server side validation
As Struts providing a way handle this using MessageStoreInterceptor but i don't know why you are doing it manually.

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

Resources