Get request from two different name space and to call same action - struts2

I have below struts configuration file with me,I need add another "namespace"(/mservice) to same action,In that config file I need to remove <param name="excludeNullProperties">false</param>
<struts>
<package name="com.web.mobile" namespace="/service" extends="mobile-default">
<action name="ticketSupport!*" class="com.web.mobile.TicketSupport"
method="{1}">
<result type="json">
<!-- This parameter should remove to the "/mservice" namesapce -->
<param name="excludeNullProperties">false</param>
</result>
<result name="input" type="json"/>
</action>
</package>
</struts>
I have two mobile apps, need to call same action with different parameters,I am thinking to maintain 2 struts configuration files, How can I modify existing config file to work with both apps?

Related

Struts 2 interceptor firing when it shouldn't

I have different areas in my webapp, basically:
Login actions. No special interceptor needs.
Secure zone. Only for authenticated users. AuthenticationInterceptor redirects non authenticated users to logout action.
Admin zone. Only for admin users. AdministrationInterceptor redirects non admin users to logout action.
Hence, I have divided my actions in packages.
numeritos-default is a package with no actions, just to define the
interceptors and a couple stacks, in order to inherit from it.
numeritos-login is a package that inherits directly from
struts-default and, hence, uses the defaultStack from struts.
numeritos-secure inherits from numeritos-default and SHOULD (afaik)
use the numeritos-default default stack.
numeritos-admin inherits
from numeritos-default but defines a different default stack, in
order to add the administrationInterceptor to the default stack.
The thing is, the actions in numeritos-secure are executing the administrationInterceptor when they shouldn't, since their default stack, inherited from numeritos-default doesn't include that interceptor (I can't figure out the reason), hence only admin users can access the application. The rest of users are kicked out of the whole secure zone, when they should be only kicked out of the admin zone.
I have the following struts.xml configuration file. Sorry to post the whole of it, but I think it could help. All classes references are really Spring beans names, since all dependencies are managed by Spring.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="global-messages" />
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="numeritos-default" extends="struts-default">
<interceptors>
<!-- Session authentication interceptor -->
<interceptor name="authenticationInterceptor" class="authenticationInterceptor"/>
<!-- Administration privileges interceptor -->
<interceptor name="administrationInterceptor" class="administrationInterceptor"/>
<!-- Caching headers interceptor -->
<interceptor name="cachingHeadersInterceptor" class="cachingHeadersInterceptor"/>
<!-- Default interceptor stack for this package (default + authentication) -->
<interceptor-stack name="numeritosDefaultStack">
<interceptor-ref name="authenticationInterceptor"/>
<interceptor-ref name="cachingHeadersInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
<!-- Default interceptor stack for administration packages (default + administration) -->
<interceptor-stack name="numeritosAdminStack">
<interceptor-ref name="administrationInterceptor"/>
<interceptor-ref name="numeritosDefaultStack"/>
</interceptor-stack>
</interceptors>
<!-- Secure interceptor stack as default -->
<default-interceptor-ref name="numeritosDefaultStack"/>
</package>
<package name="numeritos-login" extends="struts-default">
<action name="welcome" class="welcomeAction">
<result name="success" type="freemarker">ftl/login.ftl</result>
</action>
<action name="login" class="loginAction">
<result name="success" type="redirectAction">exerciseLoad</result>
<result name="input" type="freemarker">ftl/login.ftl</result>
</action>
</package>
<package name="numeritos-secure" extends="numeritos-default">
<!-- Common login result -->
<global-results>
<result name="login" type="redirectAction">logout</result>
</global-results>
<action name="exerciseLoad" class="exerciseLoadAction">
<result name="simple_op" type="freemarker">ftl/simple_operation_view.ftl</result>
<result name="equation" type="freemarker">ftl/equation_view.ftl</result>
</action>
<action name="exerciseCheck" class="exerciseCheckAction">
<result name="success" type="freemarker">ftl/show_exercise_result.ftl</result>
<result name="input" type="freemarker">ftl/answer_format_error.ftl</result>
</action>
<action name="stats" class="statsAction">
<result name="success" type="freemarker">ftl/stats.ftl</result>
</action>
<action name="modulesList" class="modulesListAction">
<result name="success" type="freemarker">ftl/modules_list.ftl</result>
</action>
<action name="selectModule" class="selectModuleAction">
<result name="input" type="freemarker">ftl/modules_list.ftl</result>
<result name="success" type="redirectAction">exerciseLoad</result>
</action>
<action name="userSettings" class="userSettingsAction">
<result name="success" type="freemarker">ftl/user_settings.ftl</result>
</action>
<action name="changePassword" class="changePasswordAction">
<result name="success" type="freemarker">ftl/user_settings.ftl</result>
<result name="input" type="freemarker">ftl/user_settings.ftl</result>
</action>
<action name="logout" class="logoutAction">
<result name="success" type="freemarker">ftl/login.ftl</result>
</action>
</package>
<package name="numeritos-admin" extends="numeritos-secure">
<!-- Admin interceptor stack as default -->
<default-interceptor-ref name="numeritosAdminStack"/>
<!-- Common login result -->
<global-results>
<result name="login" type="redirectAction">logout</result>
</global-results>
<action name="adminConsole" class="adminConsoleAction">
<result name="success" type="freemarker">ftl/admin_console_results.ftl</result>
</action>
<action name="adminConsoleGroups" class="adminConsoleGroupsAction">
<result name="success" type="freemarker">ftl/admin_console_groups.ftl</result>
</action>
<action name="adminCreateGroup" class="adminConsoleGroupsAction" method="create">
<result name="success" type="redirectAction">adminConsoleGroups</result>
</action>
<action name="adminDeleteGroup" class="adminConsoleGroupsAction" method="delete">
<result name="success" type="redirectAction">adminConsoleGroups</result>
</action>
</package>
<package name="json-admin" extends="numeritos-default, json-default">
<interceptors>
<!-- Default interceptor stack for this package (default + administration + json) -->
<interceptor-stack name="numeritosJsonAdminStack">
<interceptor-ref name="administrationInterceptor"/>
<interceptor-ref name="json"/>
<interceptor-ref name="numeritosDefaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="numeritosJsonAdminStack"/>
<action name="jsonTest" class="jsonTestAction">
<result name="success" type="json">
<param name="root">users</param>
</result>
</action>
</package>
</struts>
Defining package configuration elements are only for adding additional configuration to packages that extend it. The packages can contain interceptors, interceptor stacks, actions that can be extended.
The package element has one required attribute name, which acts as the key for later reference to the package. The extends attribute is optional and allows one package to inherit the configuration of one or more previous packages
including all interceptor, interceptor-stack, and action configurations.
Usually packages are separated by namespace attribute which is essential for configuring actions. You can define the abstract package which doesn't contain the action configurations.
The optional abstract attribute creates a base package that can omit the action configuration.
The thing is, you are not using namespace, nor abstract packages and default-intercepto-ref is not inherited to the packages that extends it.

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>

Attribute parameter from <action> element - equivalent in Struts 2

In Struts1 you can use the attribute parameter from element(struts-config.xml) and access it's value within the action class via the actionMapping.getParameter() method. For actions requiring multiple steps, the parameter is often used to indicate which step the mapping is associated
with.
Ex:
<action path="\something\Step1"
type="actions.SomethingAction"
parameter="step1"> ...
<action path="\something\Step2"
type="actions.SomethingAction"
parameter="step2"> ...
Which is the alternative solution for Struts2?
Parameters in the action configuration could be used instead
<package name="something" namespace="/something" extends="struts-default">
<action name="Step1" class="actions.SomethingAction">
<param name="step1" ...
</action>
<action name="Step2" class="actions.SomethingAction">
<param name="step2" ...
</action>
</package>

Struts2 conversation plugin

Anyone has successfully used Struts conversation plugin?
The documentation says that fields annotated with #ConversationField annotation in Action class is supposed to be retained for the entire conversation, which is not happening.
Can you help me with any other plugin for Struts 2 for developing wizard-like application?
Following is my structure of struts.xml
<package name="wstest" namespace="/wstest" extends="struts-conversation-default">
<action name="*" class="com.bla.bla.WebServiceTestController" method="{1}">
<result name="upload">/WEB-INF/content/wstest/UploadWSDL.jsp</result>
<result name="selectoperation">
/WEB-INF/content/wstest/selectoperation.jsp
</result>
</action>
</package>

How to use Struts 2 with JFreeChart?

Firstly, I went here ( http://code.google.com/p/struts2-examples/downloads/list and I downloaded Hello_World_Struts2_Mvn.zip) and I run that example.
After that, I went here (http://struts.apache.org/2.x/docs/jfreechart-plugin.html), I add the dependencies for commons-lang-2.5.jar, jcommon-1.0.16.jar and jfreechart-1.0.13.jar and I modify the example downloaded from code.google.com to see how JFreeChart is working, but I receive this error:
Unable to load configuration. - action - file:/C:/.../untitled_war_exploded/WEB-INF/classes/struts.xml:34:67
Caused by: Error building results for action createChart in namespace - action - file:/C:/.../out/artifacts/untitled_war_exploded/WEB-INF/classes/struts.xml:34:67
Caused by: There is no result type defined for type 'chart' mapped with name 'success'. Did you mean 'chart'? - result - file:/C:/.../out/artifacts/untitled_war_exploded/WEB-INF/classes/struts.xml:36:49
At the line 36 in struts.xml is the this code (the code from struts2 website):
<action name="viewModerationChart" class="myapp.actions.ViewModerationChartAction">
<result name="success" type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
What I'm doing wrong?
You need to define Action mappings related to chart differently in Struts.xml.Change your project's struts.xml with Jfreechart related actions and define it in separate package.
For eg.
<package name="struts2" extends="jfreechart-default">
<action name="viewModerationChart" class="myapp.actions.ViewModerationChartAction">
<result name="success" type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
</package>

Resources