Struts2 i18NInterceptor no longer working with ExecuteAndWaitInterceptor - struts2

I'm using the struts2 tiles plugin (v2.2.3) and I'm having a problem using the I18nInterceptor with the ExecuteAndWaitInterceptor. Essentially when I add the execAndWait interceptor (see xml below) for some reason the localization no longer works and when I try to get localized text (e.g. TextProviderSupport.hasKey) I get a NullPointerException (see error) that I narrowed down to this code in LocalizedTextUtil...
public static String findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args) {
ValueStack valueStack = ActionContext.getContext().getValueStack();
return findText(aClass, aTextName, locale, defaultMessage, args, valueStack);
}
... I'm assuming the getValueStack() is null for some reason, but I cannot figure out why. Any ideas?
Thanks,
Ryan
<pre><code>
java.lang.NullPointerException
at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:359)
at com.opensymphony.xwork2.TextProviderSupport.hasKey(TextProviderSupport.java:98)
at com.opensymphony.xwork2.ActionSupport.hasKey(ActionSupport.java:96)
at com.test.plus.PlusSupport.getCurrentLocale(PlusSupport.java:213)
at com.test.plus.import.Test.action.TestAction.testMethod(TestAction.java:801)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
at org.apache.struts2.interceptor.BackgroundProcess$1.run(BackgroundProcess.java:57)
at java.lang.Thread.run(Unknown Source)
</code></pre>
Struts.xml
<action name="Test/m/testMethod" method="testMethod" class="com.test.plus.import.Test.action.TestAction">
<result type="tiles">/WEB-INF/jsp/import/Test/testMethod.jsp</result>
<result name="success" type="tiles">/WEB-INF/jsp/import/Test/testMethod.jsp</result>
<result name="input" type="tiles">/WEB-INF/jsp/import/Test/testMethod.jsp</result>
<result name="error" type="tiles">/WEB-INF/jsp/import/Test/testMethod.jsp</result>
<result name="wait" type="tiles">/WEB-INF/jsp/import/execAndWait.jsp</result>
<interceptor-ref name="plusStack"/>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="execAndWait">
<param name="excludeMethods">input,back,cancel</param>
</interceptor-ref>
</action>

From http://struts.apache.org/2.x/docs/execute-and-wait-interceptor.html
Important: Because the action will be running in a seperate thread, you can't use ActionContext because it is a ThreadLocal. This means if you need to access, for example, session data, you need to implement SessionAware rather than calling ActionContext.getSession().

Related

struts2 tokensession not setting request in Action excludedMethod [duplicate]

This question already has an answer here:
Prevent same action called twice as long as user is in current session
(1 answer)
Closed 5 years ago.
I'm trying to avoid double-submit problems using tokenSession. My action methods are working fine without tokenSession technique.
I add <s:token/> in upsert_crypto_sources.jsp and tokenSession interceptor in struts.xml but I receive request as null in my action excludedMethod of list().
The list page doesn't need to avoid double submit problem but if I add <s:token/> in view_crypto_sources_list.jsp and remove list() from excludedMethod then I always receive result invalid.token.
My struts.xml is like:
<struts>
<package name="key-manager" namespace="/shared/km" extends="console-default" strict-method-invocation="true">
<action name="manage_cs_*" method="{1}" class="console.shared.km.ASC_ManageCryptoProfilesAction">
<interceptor-ref name="tokenSession">
<param name="excludeMethods">
list, initInsert, load, delete
</param>
</interceptor-ref>
<result name="list">/shared/km/view_crypto_sources_list.jsp</result>
<result name="insert">/shared/km/upsert_crypto_sources.jsp</result>
<result name="update">/shared/km/upsert_crypto_sources.jsp</result>
<result name="load">/shared/km/upsert_crypto_sources.jsp</result>
<allowed-methods>list, insert, load, update, delete, testConnection, forward, cancel</allowed-methods>
</action>
My action implements ServletRequestAware interface therefore it gets the request member variable set using setServletRequest() method.
I added a defaultStack interceptor and it is working fine:
<struts>
<package name="key-manager" namespace="/shared/km" extends="console-default" strict-method-invocation="true">
<action name="manage_cs_*" method="{1}" class="console.shared.km.ASC_ManageCryptoProfilesAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="tokenSession">
<param name="excludeMethods">
list, initInsert, load, delete
</param>
</interceptor-ref>
<result name="list">/shared/km/view_crypto_sources_list.jsp</result>
<result name="insert">/shared/km/upsert_crypto_sources.jsp</result>
<result name="update">/shared/km/upsert_crypto_sources.jsp</result>
<result name="load">/shared/km/upsert_crypto_sources.jsp</result>
<allowed-methods>list, insert, load, update, delete, testConnection, forward, cancel</allowed-methods>
</action>

InstantiationException when calling action using struts1-plugin

We migrating our old Struts 1 Application to Struts 2 using the struts1-plugin to wrap our actions.
This worked great with some actions but others throw the error shown below:
java.lang.InstantiationException
sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
java.lang.reflect.Constructor.newInstance(Constructor.java:526)
java.lang.Class.newInstance(Class.java:374)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:158)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:189)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:178)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.resolveModel(ScopedModelDrivenInterceptor.java:106)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:136)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
de.dak.intranet.webtier.extranet.StartPageFilter.doFilter(StartPageFilter.java:74)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
de.dak.intranet.webtier.auth.LoginFilter.doFilter(LoginFilter.java:288)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
The configuration for this action very straightforward:
<package name="extranet" extends="struts1-default" namespace="/extranet">
<action name="acceptConditions" class="org.apache.struts2.s1.Struts1Action>
<param name="className">de.intranet.webtier.extranet.AcceptConditionsAction</param>
<interceptor-ref name="struts1Stack"/>
<result name="success">/servlet/index.jsp</result>
</action>
</package>
I tried removing the interceptors in the action but that did not change anything.
The action class looks like this (actual logic was omitted):
public class AcceptConditionsAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return mapping.findForward("success");
}
}
The link in our jsp template is build correctly with the s:action-tag and another action on the same site works perfectly when the link is clicked. This action however generates the stacktrace shown above and I cannot figure out where it is coming from or how to fix it. The breakpoint inside the action is never reached therefore the error has to occur while instantiating the action itself.
I hope somebody can help me figure out what is happening.
EDIT:
Working action for clarification:
<package name="extranet" extends="struts1-default" namespace="/extranet">
<interceptors>
<interceptor name="pwdForm" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor>
<param name="className">de.intranet.webtier.extranet.PasswordForm</param>
<param name="name">pwdForm</param>
</interceptor>
</interceptors>
<action name="editPassword" class="org.apache.struts2.s1.Struts1Action">
<param name="className">de.intranet.webtier.extranet.EditPasswordAction</param>
<interceptor-ref name="pwdForm"/>
<interceptor-ref name="struts1Stack"/>
<result name="changePassword">/extranet/changePassword.jsp</result>
</action>
</package>
Java implementation looks like the AcceptConditionsAction. Only the execute-method was overwritten and no constructor defined.
Aleksandr pointed me to the answer:
The interceptor stack provided by the struts1-plugin (called struts1Stack) creates several interceptors that require a Form (i.e. scopedModelDriven, modelDriven and some others). If you don't use the entire stack and only add the interceptors needed by the action, it is not necessary anymore to define a FormBean.
remove interceptor-ref "strutsStack", add "staticParams" will do the trick.
as shown in below
<package name="extranet" extends="struts1-default" namespace="/extranet">
<action name="acceptConditions" class="org.apache.struts2.s1.Struts1Action>
<param name="className">de.intranet.webtier.extranet.AcceptConditionsAction</param>
<!--interceptor-ref name="struts1Stack"/-->
<interceptor-ref name="staticParams"/>
<result name="success">/servlet/index.jsp</result>
</action>
</package>

How to get the preview in xls?

I'm using the below code for get the output in xls.
else if ("xls".equalsIgnoreCase(reporttype)) {
try
{
System.out.println("inside xls1");
response.setContentType("application/vnd.ms-excel");
System.out.println("inside xls2");
response.setHeader("Content-Disposition", "inline; filename=\""
+ strId + ".xls\"");
System.out.println("inside xls3");
exporter = new JRXlsExporter();
System.out.println("inside xls4");
exporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
System.out.println("inside xls5");
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
ouputStream);
System.out.println("inside xls6");
System.out.println("inside exporter for XLS: "+exporter);
}
catch(Exception e)
{
System.out.println("inside xls catch");
e.printStackTrace();
}
}
But it's occurred below error.
Struts Problem Report Struts has detected an unhandled
exception: Messages: No result defined for action
com.coin.fk.SalesAction and result Exception
File: file:/D:/blm/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/blm/WEB-INF/classes/struts.transaction.xml
Line number: 44 Column number: 77
<action name="salesAction_*" method="{1}" class="com.coin.fk.SalesAction">
<interceptor-ref name="defaultLoginStack"/>
<interceptor-ref name="defaultStack">
Stacktraces
No result defined for action com.coin.fk.SalesAction and result Exception - action - file:/D:/blm/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/blm/WEB-INF/classes/struts.transaction.xml:44:77
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:350)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Unknown Source)
You are seeing this page because development mode is enabled. Development mode, or devMode, enables extra debugging behaviors and reports to assist developers. To disable this mode, set:
struts.devMode=false
in your WEB-INF/classes/struts.properties file.
How can I face this issue.
Pls help how to debug this error.
It does seems you are trying to configure Global Exceptions without having a Global Result name Exception.
Something like this:
<global-results>
<result name="exception">jsp/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="exception" />
</global-exception-mappings>
in this specific order.
It seems that you have two Interceptors Stack too, one with your custom Interceptor, and the Default Stack. You should use only one stack, with all the Interceptors you need, to avoid executing them two times on each request:
<action name="salesAction_*" method="{1}" class="com.coin.fk.SalesAction">
<interceptor-ref name="defaultLoginStack"/>
<interceptor-ref name="defaultStack">
<!--result ecc...-->
</action>
should become
<action name="salesAction_*" method="{1}" class="com.coin.fk.SalesAction">
<interceptor-ref name="defaultLoginStack"/>
<!--result ecc...-->
</action>

Struts2: interceptor and parameters

i have done some pages with Struts 2.(J2EE project)
All was ok until i try to add an interceptor.
It seems that the Interceptor delete all properties of my Class Action and Parameters send by the jsp with url like: action?param=xxx
here is the interceptor:
public class SessionInterceptor extends AbstractInterceptor{
#Override
public String intercept(ActionInvocation invocation) throws Exception {
return invocation.invoke();
}
here is the struts.xml:
<action name="movefc_ShowFjt" class="struts2.ShowFjtAction" method="movefc">
<interceptor-ref name="sessionInterceptor"></interceptor-ref>
<result name="input" type="dispatcher">jsp/showFjt.jsp</result>
<result name="success" type="dispatcher">jsp/showFjt.jsp</result>
</action>
in the class action,
public class ShowFjtAction extends ActionSupport {
private String param;
private Personne p;
param property never receive value from the jsp (it is ok when interceptor is off). Worse, other properties in Class action seems to be erased.
Is that an normal effect of the return invocation.invoke(); of the interceptor ?
Is there anything i can do to fix that ?
y defining your own interceptor are you causing all of the default interceptors to be discarded?
Should you perhaps be defining an interceptor stack which includes your interceptor and the default stack?
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="sessionInterceptor" class="SessionInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="sessionInterceptor"/>
</interceptor-stack>
</interceptors>
<action name="movefc_ShowFjt"
class="struts2.ShowFjtAction">
<interceptor-ref name="myStack"/>
<result name="input" type="dispatcher">jsp/showFjt.jsp</result>
<result name="success" type="dispatcher">jsp/showFjt.jsp</result>
</action>
The entire concept is explained as follows
1] First when user does not writes any interceptors, then interceptors defined in struts-default.xml will be used. It is defined in struts-core.jar, it is accomplished by extending the "struts-default" extended in our package xml tag.
2] When user writes his own interceptor if you add one mode code block after sessionInterceptor ref name i.e interceptor-ref name="defaultStack" will solve your problem.
Befor trying this try to unzip the struts-core.jar and move forward with your implementation.

Using an extended interceptor in struts2 does not work w/ action parameters

I have a default package w/ an interceptor configure, and i'm extending that package into another one and calling the same interceptor
<action name="availability**">
<param name="subTab">availability</param>
<interceptor-ref name="tabStack"/>
<result>/WEB-INF/jsp/index.jsp?include=visibilit/availability.jsp</result>
</action>
The problem is that the param is not being read inside my interceptor code:
Map params = invocation.getInvocationContext().getParameters();
subTab = params.get("subTab").toString(); //NULL exception
Any idea how i can pass parameters to extended interceptors?
Thanks!
The getParameters() method which you are calling only returns the parameters from the HTTP request. The parameters set in struts.xml with are called "static parameters", and you can access them (within the intercept() method) like this:
ActionConfig config = invocation.getProxy().getConfig();
Map<String, String> parameters = config.getParams();
String subTab = params.get("subTab");
Source: StaticParametersInterceptor.java
Can you try this syntax
<action name="availability**">
<interceptor-ref name="tabStack">
<param name="subTab">availability</param>
</interceptor-ref>
</action>
I am not sure but maybe this will work

Resources