I want to use a struts2 interceptor for all actions which start with the specific name:
Say I want to intercept all actions which start with Module. Is there any way I can configure an interceptor something like the Spring AOP does e.g: com.acme.web.actions.Module*
I can't use AOP because it breaks all page params (As I guess there is no way the params interceptor is invoked there).
is this possible?
In Struts you can define different interceptor stacks and apply different stacks to different actions.
So in your situation you can define a different stack like this:
<interceptors>
<interceptor name="myInterceptor" class="com.company.security.MyInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="myInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
And, using wildcard mappings, the configuration bellow is essentially "map all actions named ModuleSomething to a class com.acme.web.actions.ModuleSometing, and apply myStack of interceptors to it:
<action name="Module*" class="com.acme.web.actions.Module{1}">
<result>{1}.jsp</result>
<interceptor-ref name="myStack"/>
</action>
Related
I know I can call a url with the "request_locale" parameter but that won't last long. Maybe storing it in a cookie? Which I don't know how to do.
After many hours of searching I found:
A low of dead links in the apache API
Setting the locale in session attribute "org.apache.struts.action.LOCALE"
... or "request_locale"
... or "request_cookie_locale"
... or "WW_TRANS_I18N_LOCALE"
Setting the locale in ActionContext.getContext()
Something about interceptors that make my application throws exceptions so I don't know if I should pursue...
There's a mix of Struts 1 and Struts 2 in there which is not making things easier...
Here's what I have so far:
In the XML config:
<action name="languageSelection" class="changeSelectionAction">
<result name="input" type="redirect">${redirectUrl}</result>
</action>
I figured I'd use "Input" to go back to the previous page after the user selected a language.
The redirect variable is defined in my Action Class.
In the Web Page:
<webwork:url id="url" action="languageSelection">
<webwork:param name="lang">fr</webwork:param>
</webwork:url>
<webwork:a href="%{url}">French</webwork:a>
This sends "lang=fr" which I can read in my Action Class.
In the Action Class, I have:
HttpServletRequest request = ServletActionContext.getRequest();
String refererUrl = ServletActionContext.getRequest().getHeader("Referer"); // gives me the source page so I can go back to it.
setRedirectUrl(refererUrl);
String[] langs = request.getParameterValues("lang"); // getting the language the user selected
... followed by a lot of garbage that doesn't work and some insults for the compiler that are commented out.
I'm not the best web developer so any basic web advice will be welcome.
You can implement a i18n interceptor which can store your locale in the session or a cookie. See https://struts.apache.org/core-developers/i18n-interceptor.html
<interceptor name="i18nCookie" class="org.apache.struts2.interceptor.I18nInterceptor"/>
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="i18nCookie">
<param name="localeStorage">cookie</param>
</interceptor-ref>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>
We use the org.apache.struts2.interceptor.I18nInterceptor in our default interceptor stack. After that struts listen to the parameter request_locale and every action that gets a ?request_locale=LANGCODE parameter will change the language for the user. Normally this setting is stored in the session of the user, so make sure user sessions are enabled too.
With the link from #TheSlavMan you can read all the possible options available for this interceptor.
Of course a simple "change language and redirect to a page"-action is possible. You are on the right path, my first guess would be to change the lang-parameter to request_locale.
everyone.
I'm having problem on struts2 namespace..
First of all, this is my developing environment.
server : tomcat
(currently my project is in ROOT folder in /tomcat/webapps/ROOT)
framework : struts2
Here is my problem.
lets say there are two pages. admin_index.jsp and front_index.jsp
when i want to call admin_index.jsp from action. i use
<package name="admin" namespace="/dl_adm" extends="struts-default">
<action name="/index" method="index"class="kr.co.www2.controller.front.AdminMainController">
<result name="success">/WEB-INF/jsp/admin/admin_index.jsp</result>
</action>
</package>
and it works fine by calling http://.../dl_adm/index.do
and to call this has problem for me.
<package name="front" namespace="/" extends="struts-default">
<action name="/index" method="index"class="kr.co.www2.controller.front.FrontMainController">
<result name="success">/WEB-INF/jsp/admin/front_index.jsp</result>
</action>
</package>
when i go for the http://.../index.do, it gives 404...
although i aware that namespace="/" is for default namespace...
QUESTIONS:
Is there anyway to ignore the default namespace? because i want to use that / because i just want to go through http://.../ and action name without namespace...
or if there isn't a way to do that. any suggestions?
Is there anyway to ignore the default namespace? because i want to use that / because i just want to go through http://.../ and action name without namespace..
No, you can't ignore the default namespace. The default namespace is empty and it's used if you omit namespace attribute in the package declaration.
or if there isnt a way to do that. any suggestions?
I would not use slashes in action name using xml configuration. The action mapper might incorrectly add an additional slash to the action name to infer the mapping from the URL.
So you should use
<package name="front" namespace="/" extends="struts-default">
<action name="index" method="index"class="kr.co.www2.controller.front.FrontMainController">
<result name="success">/WEB-INF/jsp/admin/front_index.jsp</result>
</action>
</package>
Im wondering if it's possible to set a default result that would apply to every action in struts2.
For example to say that result "FOO" will always redirect to action BAR or to jsp "BAR.jsp" instead of adding <result name="FOO">xxx</result> to every action I define ...
That would help me to create a generic error page
Thanks
Yes, you can set global results for a package.
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirectAction">Logon!input</result>
</global-results>
For more details read Result Configuration Page
One quick solution that comes to my mind is global result configuration.Most often, results are nested with the action element. But some results apply to multiple actions. In a secure application, a client might try to access a page without being authorized, and many actions may need access to a "logon" result.
If actions need to share results, a set of global results can be defined for each package. The framework will first look for a local result nested in the action. If a local match is not found, then the global results are checked.
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirectAction">Logon!input</result>
</global-results>
For more details refer to the official doc
result-configuration
I am developing a web application in struts2. I have declared global properties in struts.xml like:
<constant name="struts.custom.i18n.resources" value="LableResources,MessageResources" />
How do I use those properties in struts.xml itself? I want to implement a system in which request URI names come from global properties file.
As far as I know, you can't do that. I18n resources are for messages an runtime stuff not for configuration.
You can use parameters in the configuration but at runtime they have to be available as request parameters or in the value stack.
Like this:
<result name="input" type="redirectAction">
<param name="actionName">${from}</param>
<param name="id">${id}</param>
</result>
In my struts 2 project when using redirect action i m loosing all my values such as action error and field errors.
I looked it up on net and found 2 options
Chain - This isn't used much i donno why ..
MessageStoreInterceptor - This needs to be placed in every action
So can any one please let me know when is redirect(or RedirectAction) preferred and when is chain preferred.
Redirecting an action looses the current value stack (anything in request scope) you can of course set up your action to preserve these values by passing them as parameters to the next action, but it is a bit of a pain.
Chain preserves the value stack, so the next action can work on parameters created from the previous action without needing to explicitly pass them, also since there is this snow ball effect you can use all the parameters in the view.
But it is generally recognized that a top down solution (maybe top down isn't the best word... 'structured') is better than building a maze of spaghetti actions.
So when you're under pressure to get something working and not overly familiar with struts2 then use chain or redirection, and then definitely come back and fix it! In general you should use an interceptor.
In the event of an action that routes to other actions based on some condition it would be better to make that an interceptor apply that to a package and put all actions which require this interesting behavior in that package. Then it is very clear which actions this applies to.
First option
<action name="remove" class="com.action.firstAction" method="remove">
<result name="success" type="redirectAction">
secondaction
<param name="actionName">secondaction</param>
<param name="namespace">/</param>
<param name="param name">${param value}</param>
</result>
</action>
<action name="secondaction" class="com.action.secondAction" method="result">
<result name="success">result.jsp</result>
</action>
Another option
<action name="remove" class="com.action.firstAction" method="remove">
<result name="success" type="chain">secondaction</result>
</action>
<action name="second action" class="com.action.secondAction" method="result">
<result name="success">result.jsp</result>
</action>