How to get the preview in xls? - struts2

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>

Related

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>

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 i18NInterceptor no longer working with ExecuteAndWaitInterceptor

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().

Cannot exclude method in struts2 interceptor

I am having the opposite problem of the fellow in this question:
Struts 2: excluding method from validation from just the defaultStack interceptor
The above question involved all methods being excluded, my issue is that no methods are being excluded!
I'm trying to get my authenticationInterceptor to ignore the showLogin method of my LoginAction:
<interceptors>
<interceptor name="authorizationInterceptor" class="org.companyname.struts.interceptor.AuthorizationInterceptor"/>
<interceptor-stack name="appDefault">
<interceptor-ref name="authorizationInterceptor"/>
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="appDefault" />
<action name="loginInitial" class="org.companyname.struts.action.LoginAction" method="showLogin">
<interceptor-ref name="appDefault">
<param name="authorizationInterceptor.excludeMethods">showLogin</param>
</interceptor-ref>
<result name="success">/login.jsp</result>
</action>
However, each time I forward to loginInitial, the interceptor grabs it, even though my showLogin method is being excluded.
I've checked for naming issues, and have tried putting several different values in the interceptor-ref within the action, and nothing seems to work.
What's the proper way to skip the authorizationInterceptor when I forward to loginIntial?
I believe my issue is the type of interceptor I'm using, the AbstractInterceptor. So, I just use different interceptor stacks on the actions I don't want intercepted:
<interceptor-stack name="appDefaultWithAuth">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
<interceptor-ref name="authorizationInterceptor"/>
</interceptor-stack>
<interceptor-stack name="appDefaultNoAuth">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
</interceptor-stack>
<default-interceptor-ref name="appDefaultWithAuth" />
<action name="loginInitial" class="org.company.struts.action.LoginAction" method="showLogin">
<interceptor-ref name="appDefaultNoAuth"/>
<result name="success">/login.jsp</result>
</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.

Resources