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>
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.
I want to have an action default but not in XML because I want to use annotations using the Struts2 Conventions Plugin.
So I want to replace
<package abstract="true" namespace="/" name="mypackage" extends="struts-default">
<default-action-ref name="index"/>
</package>
with something in annotations, so I don't have to use a struts.xml file but also redirect to a specific action when an unknown action is specified in the URL.
Is there support for this in the Struts2 Convention Plugin or are there any good workarounds that are annotation based?
Currently there is no support but you can request at here.
I don't think that there is a simple tip or workaround!
This answer may come a bit late but still be useful to someone.
As mentioned in Struts documentation, an alternative to default actions is using wildcards:
<action name="*">
<result>/index.jsp</result>
</action>
Or using an annotation:
#Action(value = "*")
#Result(location = "/index.jsp")
public class HomeAction extends ActionSupport {
}
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>
In struts.xml file
<action name="myJasperTest" class="com.sample.SupplierEnquiryReport">
<result name="success" type="jasper">
<param name="location">/reports/xyz.jasper</param>
<param name="dataSource">myList</param>
<param name="format">PDF</param>
</result>
</action>
I need to return multiple pdf files after one action.
Is it possible?
No, but from your UI JSP you can:
open multiple actions, each one returning a PDF;
open another JSP, with multiple <iframe>, each one pointing (with src attribute) to a different Action (or better, to the same, but passing different parameters), and returning a PDF.
Then several pages each one with a PDF, or a big page with several iframes, each iframe with a PDF.
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>