My interceptor (validation) is not getting called before or after the action. Any ideas how to get it work ?
Note : Everytime the default interceptor is being called.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default,json-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
<result-type name="json" class="org.apache.struts2.json.JSONResult" />
</result-types>
<interceptors>
<interceptor name="validation" class="ValidatorBaseAction"/>
<interceptor-stack name="default">
<interceptor-ref name="logger"/>
</interceptor-stack>
<interceptor-stack name="validationStack">
<interceptor-ref name="validation"/>
<interceptor-ref name="default"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="default" />
<action
name="viewRequest"
class="ViewAction"
method="execute">
<interceptor-ref name="validationStack" />
<result name="input" type="redirectAction">explore</result>
<result name="success" type="redirect">/showRequest.do?${explorerParameters}</result>
</action>
</package>
</struts>
Main problem:
class both for Actions and Interceptors must specify the FQCN, not only the name. Then change it to something like:
<interceptor name="validation" class="com.foo.bar.ValidatorBaseAction"/>
and also change your action to
<action name="viewRequest" class="com.foo.bar.ViewAction" method="execute">
Side problems:
Don't call it ValidatorBaseAction if it is an Interceptor, call it ValidatorBaseInterceptor. And ensure there is nothing of an Action inside it;
Don't use an Interceptor Stack with only one Interceptor, I'm pretty sure it would be useless in 99% of the cases. If you are not sure, just use the defaultStack, adding your interceptor to it.
Polishing:
json-default already extends struts-default, so this
<package ... extends="struts-default,json-default"
is equivalent to this
<package ... extends="json-default"
that is cleaner;
Since you extends json-default, you don't need to redefine the JSON result, then remove
<result-type name="json" class="org.apache.struts2.json.JSONResult" />
that is useless.
Try always to prefer redirectAction result when redirecting to an Action, and use redirect result only when redirecting to other resources or external URLs
Related
I am migrating the Struts from 1.1 to 2.3.35 version, I have written struts.xml looks like below:
==============================
<!-- <package name="default" extends="struts-default"> -->
<constant name="struts.devMode" value="true" />
<package name="struts-migration" extends="struts-default" namespace="/">
<global-results>
<result name="welcome" type="redirect">/pages/Login</result>
<result name="failure" type="redirect">add_new_test_exercise_status.jsp</result>
</global-results>
<action name="/pages/Login" class="org.apache.struts2.s1.Struts1Action">
<param name="className">struts.actions.LoginAction</param>
<result name="success">/struts/login.jsp</result>
</action>
</package>
==============================
When i am trying the access the page using the RUL, getting page not found exception.
http://localhost:8080/Struts/pages/Login
Could anyone please share any document or steps to migrate from struts 1.1 to 2.3.35 version .
I'm trying to create an Interceptor able to change the language but I can not change it.
Relevant Interceptor code:
public String intercept(ActionInvocation invocation) throws Exception {
ActionMapping mapping = (ActionMapping) invocation
.getInvocationContext()
.get(ServletActionContext.ACTION_MAPPING);
Map<String, Object> params = mapping.getParams();
if (params != null) {
Locale locale = (Locale) params.remove(LOCALE_PARAMETER);
if (locale != null) {
ActionContext.getContext().setLocale(locale);
}
}
return invocation.invoke();
}
struts.xml:
<package name="default" extends="struts-default" namespace="/">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<interceptors>
<interceptor name="navigator"
class="it.apps.mca.web.interceptors.NavigatorInterceptor">
</interceptor>
<interceptor name="locale"
class="it.apps.mca.web.interceptors.internationalizations.LocaleInterceptor">
</interceptor>
<interceptor-stack name="customStack">
<interceptor-ref name="navigator" />
<interceptor-ref name="locale" />
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<action name="locale"
class="it.apps.mca.web.actions.internationalizations.LocaleAction">
<interceptor-ref name="customStack"></interceptor-ref>
<result name="success" type="redirect">
<param name="location">${target}</param>
<param name="parse">true</param>
</result>
</action>
<action name="login"
class="it.apps.mca.web.actions.authentication.LoginAction">
<result name="success" type="tiles">/welcome.tiles</result>
<result name="input">index.jsp</result>
<result name="error">index.jsp</result>
</action>
</package>
I know i18n interceptor already exists.
I added an interceptor that performs redirections. After adding this interceptor, I lose the location of the page.
The same thing happens if I perform a redirect through the action. You know tell me the reason?
That Interceptor already exists: i18n Interceptor
It is part of the defaultStack, so you don't have to manually include it.
Example of usage
Other info in this answer.
I'm trying to enable slashes in Struts 2 action names. I've put this in my config file:
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value=","/>
<constant name="struts.multipart.maxSize" value="2147483648" />
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<package name="myApp" extends="struts-default">
<action name="home" class="net.myapp.actions.HomeAction" method="execute">
<result name="landing">/landing.jsp</result>
</action>
<action name="/ajax/foo" class="net.myApp.actions.ajax.FooAction" method="execute">
<result name="success">/foo.jsp</result>
</action>
</package>
</struts>
When I go to just the homepage of this app, e.g http://localhost:8034/myApp, I see the homepage correctly. But if I visit http://localhost:8034/myApp/ajax/foo, I get the error: There is no Action mapped for action name ajax/foo. even though I have described it above as the 2nd action.
What am I doing wrong?
I'd say you need a namespace as well, so ajax would be the namespace in your case:
<package name="myApp" extends="struts-default" namespace="/ajax">
<action name="foo" class="net.myApp.actions.ajax.FooAction" method="execute">
<result name="success">/foo.jsp</result>
</action>
</package>
Note that you could use slashes in your action names, e.g. foo/bar, but it's not advisable, since some plugins (e.g. the conversation plugin) might have difficulties to determine the namespace and action from a string like /ajax/foo/bar.
Btw, the error message says There is no Action mapped for action name ajax/foo., i.e. struts looks for an action named ajax/foo but you only have an action /ajax/foo.
Here is my struts.xml file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<package name="Authentiate" extends="struts-default">
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<action name="loginAuthenticate*" class="com.authenticate.actions.LoginAuthenticate" method="{1}">
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
<result name="redirectRegister" type="redirect">/registration.jsp</result>
</action>
</package>
I haven't used velocity templates but I am getting the following error.
java.lang.RuntimeException: com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=org.apache.struts2.views.velocity.VelocityManager, name='default'] in public void org.apache.struts2.osgi.OsgiConfigurationProvider.setVelocityManager(org.apache.struts2.views.velocity.VelocityManager). - Class: com.opensymphony.xwork2.inject.ContainerBuilder
First of all try to point result type explicitly using default type dispatcher:
<result name="success" type="dispatcher">/welcome.jsp</result>
<result name="error" type="dispatcher">/error.jsp</result>
<result name="redirectRegister" type="redirect">/registration.jsp</result>
If this doesn't help, it looks like there are some struts dependencies that can't be resolved. According to this bug at apache's bugtracker you should add VelocityManager bean to your struts.xml:
<bean class="org.apache.struts2.views.velocity.VelocityManager" name="default" optional="true" />
This is really weird error. Fixed by removing spring-struts jar from dependencies.
Velocity is not necessary to add as a dependency if you are not using velocity templates in your results. By default Struts2 uses Freemarker templates.
Errors may not seen if your server not utilizing JMX, but if you run in JMX enabled appserver like JBOSS it might raise.
I am using struts2, for that my struts.xml file contains code like :
<?xml version="1.0" encoding="UTF-8" ?>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<include file="strutsAuthentication.xml"/>
<package name="default" extends="struts-default">
<interceptors>
<interceptor-stack name="crudStack">
<interceptor-ref name="checkbox" />
<interceptor-ref name="params" />
<interceptor-ref name="static-params" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
</package>
And i have specified all the required actions inside the strutsAuthentication.xml. That code is :
<struts>
<package name="authentication" extends="default" namespace="/authentication">
<action name="saveCountry" class="saveCountryAction">
<interceptor-ref name="defaultStack" />
<result name="success">/savecountry.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
When i am deploying my application into tomcat, it gives me warning that :
WARN (org.apache.struts2.components.Form:308) - No configuration found for the specified action: 'saveCountry' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
It means struts.xml can't include strutsAuthentication.xml. Anyone have a solution ?? Thanx in advance....
Got d solution.... For above problem i was done a mistake in calling the action from jsp page. So namespace name "authentication" should be included at the time of calling the action class. Final solution is : "authentication/saveCountry.action".
I don't know what version of struts2 you're using but if you're using the 2.1.x branch you should look at the convention plugin http://cwiki.apache.org/S2PLUGINS/convention-plugin.html. You can get rid of 99% XML configuration.
As an add note. We should never use "xyz.action" in JSP pages. If we later need or decide to change the url-pattern from .action to .do or .html etc. We have to change all JSP pages. A better approach to compose links is:
Link Text