struts 2 token Interceptor - struts2

I am getting like "WARNING: Could not find token name in params."
In Struts.xml
<interceptors>
<interceptor name="entityInterceptor" class="entity.jpa.EntityInterceptor"/>
<interceptor name="sessionInterceptor" class="env.actionitems.struts.SessionInterceptor"/>
<interceptor-stack name="entityStack">
<interceptor-ref name="entityInterceptor"/>
<interceptor-ref name="sessionInterceptor"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="defaultStack">
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="entityStack"></default-interceptor-ref>
<action name="*acegitran*jsp*pkg*create*dcode*" method="{1}" class="env.transactions.{3}.{4}">
<interceptor-ref name="token"/>
<interceptor-ref name="entityStack"/>
<result name="invalid.token">Transaction.{2}iport{5}create</result>
<result name="success" type="tiles">Transaction.{2}iport{5}create</result>
<result name="error" type="tiles">Transaction.{2}iport{5}create</result>
<result name="input" type="tiles">Transaction.{2}iport{5}create</result>
</action>
In JSP
<s:token name="clientToken"/>
Any other parameters should be passed in struts.xml to solve.

I had a similar problem. You might want to try changing "invalid.token" to "invalid.clientToken" in the Struts.xml file
eg
result name="invalid.clientToken"
Please remember that the Struts.xml file will only take effect after the server is restarted!
Hope that helps

Related

validation failing yet its still running execute on struts2

im doing a modification on an existing app. my problem is even tho the validation fails for the form submit, its still executing the execute method.
my struts file
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- <include file="com/paritysys/util/struts.xml" /> -->
<constant name="struts.url.includeParams" value="none" />
<constant name="struts.action.extension" value="html,action" />
<package name="public" extends="struts-default">
<interceptors>
<interceptor name="websiteOnline"
class="parity.action.website.OnlineInterceptor" />
<interceptor name="websiteLogin"
class="parity.action.website.LoginInterceptor" />
<interceptor-stack name="appStack">
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<!-- <interceptor-ref name="paritySessionStack"/> -->
<interceptor-ref name="websiteOnline" />
<interceptor-ref name="websiteLogin" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="appStack" />
<global-results>
<result name="login" type="redirectAction">
<param name="actionName">index</param>
</result>
<result name="exception" type="freemarker">/public/error.html.ftl</result>
<result name="error" type="freemarker">/public/error.html.ftl</result>
<result type="freemarker" name="maintenance">/public/maintenance.html
</result>
<result type="freemarker" name="pre-offline">/public/pre-offline.html
</result>
<result type="freemarker" name="post-offline">/public/post-offline.html
</result>
</global-results>
<action name="index" class="parity.action.website.LoginAction">
<result type="freemarker" name="success">/public/index.html.ftl</result>
</action>
<action name="login" class="parity.action.website.SubmitLoginAction">
<result type="freemarker" name="success">/public/questionnaire.html.ftl
</result>
<result type="freemarker" name="input">/public/index.html.ftl</result>
</action>
<action name="submit" class="parity.action.website.SubmitQuestionnaireAction">
<result type="freemarker" name="success">/public/thanks.html.ftl
</result>
<result type="freemarker" name="input">/public/questionnaire.html.ftl
</result>
</action>
<action name="whereIsMyId" class="parity.action.website.LoginAction">
<result type="freemarker" name="success">/public/whereIsMyId.html.ftl
</result>
</action>
<action name="loadCollegeFinder" class="parity.action.website.LoadCollegeFinderAction">
<result type="freemarker" name="success">/public/college_finder.html.ftl
</result>
</action>
<action name="findCollege" class="parity.action.website.FindCollegeAction">
<result type="freemarker" name="success">/public/college_finder.html.ftl
</result>
<result type="freemarker" name="input">/public/college_finder.html.ftl
</result>
</action>
</package>
my action class code
public void validate() {
logger.debug("validate fired");
Bla bla bla
addFieldError("username","error");
if (hasFieldErrors()) {
logger.debug("Field errors is true");
}
}
public String execute() throws Exception {
logger.debug("execute firing");
return result;
}
any ideas why this would happen? for some reason even tho the login.action is failing, its still sending down the success return and moving forward.
You interceptor stack doesn't include the "workflow" interceptor stack, which is what determines what to do on a validation failure.
For that matter, it doesn't include the "params" interceptor, which is how parameters are set on the action, so it will never work anyway. You can't just arbitrarily remove interceptors--it's where S2 gets the bulk of its functionality. See the interceptor docs.
Also, you can set a default result type--you may want to do that if most everything is a FreeMarker result rather than type it over and over.

struts 2 Bean does not populate in action class

I am using Struts 2-Spring framework in my assignment.
I have defined a bean in struts 2 action class
One interceptor for checking authorisation before executing every action.
The bean is used for defining controls like textfield, Radio button, etc on jsp page.
On submit of this page control goes properly to defined action class, but finds bean object as null in action class. Hence unable to perform futher operations.
Removing interceptors works fine.
Any pointer will be appreciated.
I haven't defined stack for interceptors defined in struts.xml.
After defining below stack in struts.xml its works fine:
<interceptors>
<interceptor-stack name="applicationStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="debugging"/>
<!-- User defined interceptor -->
<interceptor-ref name="contextSecurityInterceptor"/>
</interceptor-stack>

Struts2 token-Session interceptor problem

I'm using Struts 2 in my business web application, and to prevent double entry at the time of refresh page I had add interceptor in my struts.xml file in action
<interceptor-ref name="tokenSession"/>
When I will insert this statement, it will always redirect to invalid.token and goes to tiles - requisition.tiles. What is the mistake I've done?
And if I will remove above interceptor then it will work fine, so what's the problem?
My code in struts2.xml
<action name="*Requisition" class="com.sttl.rpsc.action.RequisitionAction" method="{1}Requisition">
<interceptor-ref name="basicStack" />
<interceptor-ref name="validation">
<param name="excludeMethods">setupRequisition,setupPostRequisition,setupQualificationRequisition,setUpForUpdateQualificationRequisition,
setupAgeRequisition,setupReservationRequisition,deletePostRequisition,showSaveCancelRequisition,setUpForUpdateAgeRequisition,setUpForUpdateReservationRequisition,
deleteQualificationRequisition,deleteAgeRequisition,deleteReservationRequisition,setScrutinizePostRequisition</param>
</interceptor-ref>
<interceptor-ref name="tokenSession"/>
<result name="invalid.token" type="tiles">/requisition.tiles</result>
<result name="input" type="tiles">/requisition.tiles</result>
<result name="success" type="tiles">/requisition.tiles</result>
<result name="error" type="tiles">/requisition.tiles</result>
<result name="cancel" type="redirect">rpscadminsecure.action</result>
<result name="getRequisitionList" type="chain">setupRequisition</result>
<result name="reqsuccess" type="chain">setupPostRequisition</result>
<result name="postsuccess" type="tiles">/requisitionpost.tiles</result>
<result name="qualisuccess" type="tiles">/requisitionqualification.tiles</result>
<result name="agesuccess" type="tiles">/requisitionage.tiles</result>
<result name="ressuccess" type="tiles">/requisitionreservation.tiles</result>
<result name="saveOrCancelsuccess" type="tiles">/saveOrCancelrequisition.tiles</result>
<result name="cancelSuccess" type="redirect">setupRequisition</result>
<result name="successSetupScrutinize" type="tiles">/requisitionpost.tiles</result>
</action>
You have to change the order of the declaration in the interceptors. Something like this:
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="token-session/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>
The basicStack is the last in the order.

Struts2 input values lost when calling interceptor

i have a problem with Struts2
This is my action configuration, before executing the roleDete in the Action, the interceptor is call, but after this, the values of the input are lost, there is no id to delete, if i remove the interceptor then the id to delete exist, could some one guide me to solve this?
<action name="roleDelete" method="roleDelete" class="com.webapp.role.action.RoleAction">
<interceptor-ref name="validateUser"/>
<result name="input" type="tiles">usertypePage</result>
<result name="success" type="redirect">usertypeForm</result>
</action>
Thanks
It sounds like you've defined a custom validateUser interceptor stack that doesn't include a required Struts 2 interceptor. By default, Struts 2 invokes the following interceptors on every request unless you define your own stack (as you've done):
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
My advice would be to go through the above list and add them back in one at a time until you figure out which you need to properly populate your request variable. You can read more about interceptors in the Struts 2 docs.
<interceptors>
<interceptor name="vendorStoreInterceptor" class="br.org.myapp.actions.interceptors.VendorStoreInterceptor"></interceptor>
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
<interceptor-stack name="mainStack">
<interceptor-ref name="vendorStoreInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="mainStack"/>
--
Use that. You have to include your interceptor and the defaultStack after (watch the last 'interceptor-stack' with my 'vendorStoreInterceptor' and the 'defaultStack').
That's it!

struts2: After locale switching how to return on same page where user was on?

I am using struts2 and in my struts.xml I have written following code for locale switching -
<action name="switchToEnglish">
<interceptor-ref name="i18n"/>
<interceptor-ref name="basicStack"/>
<result name="input">error.jsp</result>
<result name="success">login.jsp</result>
</action>
<action name="switchToFrench">
<interceptor-ref name="i18n"/>
<interceptor-ref name="basicStack"/>
<result name="input">error.jsp</result>
<result name="success">login.jsp</result>
</action>
Now, after language switching same page (login.jsp) appears. But, I want to return on the page where user was before language switching.
Thanks in advance.
I had the same problem. I resolved it by passing the page name to the action(by GET or POST), then I use it in the result this way :
<action name="switchToEnglish">
<interceptor-ref name="i18n"/>
<interceptor-ref name="basicStack"/>
<result name="input">error.jsp</result>
<result name="success">%{currentPage}</result>
</action>
<action name="switchToFrench">
<interceptor-ref name="i18n"/>
<interceptor-ref name="basicStack"/>
<result name="input">error.jsp</result>
<result name="success">%{currentPage}</result>
</action>
Don't forget to set the getter/setter for "currentPage" in the action Class.
It's not the best way to do it but it was ok for my app.
Also I have made this, with an AJAX request to the LocaleAction, on success, just refresh the page with jQuery and you will stay on the same page where you was before the locale change.
script:
<script type="text/javascript">
$(document).ready(function(){
$(".lang").click(function() {
var id = $(this).attr("id");
$.ajax({
type: "POST",
url: "locale.action?lang="+id,
cache: false,
success: function(){
window.location.href='';
}
});
return false;
});
});
</script>
And the links:
<span style="float: right;">
<s:a id="ro" cssClass="lang">Română</s:a>
•
<s:a id="ru" cssClass="lang">Русский</s:a>
•
<s:a id="en" cssClass="lang">English</s:a>
</span>

Resources