Is there an easy way to make Struts 2 action names case insensitive? Currently I have the following action defined:
<action name="printTest" class="MyClass" >
<result name="error">/WEB-INF/jsp/error.jsp</result>
<result name="input">/WEB-INF/jsp/test.jsp</result>
<result name="success">/WEB-INF/jsp/test.jsp</result>
</action>
If the user types URL /app/printtest.do instead of /app/printtest.do this action is not executed.
Other then mod_rewrite on the httpd level or something like that, the only option that I know about right now is simply adding the same exact action and changing the name to printtest. Ideally it would be a simple config change to struts.xml.
No configurable option, AFAIK, for case insensitive mapping.
So, I believe you have answered the question yourself: either Apache's mod_rewrite, or write the extra mappings in the config.
You could also write your own ActionMapper, extending DefaultActionMapper (method
parseNameAndNamespace() ), but I suspect it would be overkill... :-)
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.
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'm developing a Struts 2 project where I need to implement 2 friendly URLs. Since my 2 URLs need to be something like URL/name/id and URL/id/title, the only way I found to manager this was to use an 1st step action to process the request which then forwards to the correct Action1 or Action2, like this:
<action name="/*/*" class="web.ProcessRequestAction">
<param name="firstParam">{1}</param>
<param name="secondParam">{2}</param>
<result name="action1" type="chain">Action1</result>
<result name="action2" type="chain">Action2</result>
</action>
And have both actions defined as well like this:
<action name="Action1" class="web.Action1" method="execute">
<result name="success">/WEB-INF/content/Action1.jsp</result>
<result name="input">/WEB-INF/content/Action1.jsp</result>
<result name="error">/WEB-INF/content/Action1.jsp</result>
</action>
However, if I invoke URL/parameter/parameter, and then on ProcessRequestAction I return "Action1", I'll get the Action1.jsp (as intended) but all messy, seems like all the CSS is missing.
First of all, is my approach correct? If so, what I'm I doing wrong here, what can I improve?
Thansk in advance!
As to why your CSS isn't working, we have no way of knowing--a JSP is a JSP, and follows normal JSP/HTML rules.
As for the first issue: I'd recommend against action chaining; it's actively discouraged, and usually just makes a mess of things.
Without knowing more about your application architecture, it's difficult to give targeted advice, but you may find parameters in namespaces helpful.
Even if it's not immediately applicable, you may be able to play games with a PatternMatcher or ActionMapper that's more targeted to your actual needs--the implementation would depend on a variety of factors, particularly the format(s) of the name, id, and title URL components.
Here are a few resources that may help you achieve the clean URLs you're looking for:
Better URLs with Struts2
https://stackoverflow.com/questions/4772737/hidden-features-of-struts-2-framework/4837917#4837917
I'm using Struts 2.1.8.1. I have a requirement to embed some pages from another server on my own app, so the users will access to them through my application, without accessing directly the other server. My idea is to have a package definition for that, so any access to that package would be redirected to the internal server.
<package name="eco-marketing" namespace="/marketing" extends="eco-default">
<action name="*">
<result name="success" type="dispatcher">
<param name="location">http://myotherserver:8080/test/{1}</param>
</result>
</action>
</package>
But it does not work, I got a Error 404--Not Found, so I suppouse is not as easy as it sounds. Any ideas on how to do this?
TIA
I'll assume that you are accessing just html, then see: http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html
I would recommend creating an action in that package to do the work for you, and use the value of the parameter to get the required data.
You probably already know but an iframe in the consumer action will make this easier to use than trying to parse what you need out.
After you have that figured out, if you decide to create a custom result type, please post it back to us here it would be very interesting.
An example of a custom result type can be found here (4th code block from the top): http://siriwardana.blogspot.com/2008/12/creating-custom-result-type-struts-2.html
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>