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>
Related
<interceptor-stack name="DefaultTEST">
<interceptor-ref name="exception" />
<!-- some more interceptors go in here -->
<interceptor-ref name="debugging" />
</interceptor-stack>
<default-interceptor-ref name="DefaultTEST" />
<action name="welcome">
<result type="tiles">WELCOME_PAGE</result>
</action>
<action name="">
<result ...>...</result>
</action>
... <!-- more actions -->
So my question is how to override the default interceptor stack so that for welcome action some other interceptors (or interceptor stack) can be loaded while the default one is not.
You can override the interceptors config if you reference an interceptor or interceptors stack in the action config explicitly.
<action name="welcome">
<interceptor-ref name="defaultStack" />
<result type="tiles">WELCOME_PAGE</result>
</action>
Only defaultStack will be executed for welcome action. Other actions that don't override the interceptors config in this package will use DefaultTEST because it's configured as default.
<interceptors>
<interceptor-stack name="appDefault">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
<interceptor-ref name="tokenSession">
<param name="includeMethods">upload,view,delete,confirm</param>
</interceptor-ref>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/jpg,image/png,image/pjpeg,application /pdf</param>
<param name="maximumSize">1048576</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="appDefault" />
<action name="upload_*" class="nic.rto.sow4.actions.UploadAction" method="{1}">
----
-----
<result name="invalid.token">/jsp/invalid_token.jsp</result>
</action>
This exception occurred:
SEVERE: Servlet.service() for servlet default threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:914)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:574)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
Try to configure interceptor for individual action as given below.
<action name="yourAction" class="com.YourActionClass">
<interceptor-ref name="tokenSession"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/update.jsp</result>
</action>
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
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!
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>