aspose file tools*
The moose likes Struts and the fly likes How to access the request from an ActionServlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to access the request from an ActionServlet?" Watch "How to access the request from an ActionServlet?" New topic
Author

How to access the request from an ActionServlet?

Ulrich Doe
Greenhorn

Joined: Dec 09, 2010
Posts: 7
Hi,

this is a newbe question but I just can't find out:

How do you access the request from an ActionServlet?



Background:
In my Struts app I have a locale specific Resource file:
myResources_pl_PL.properties

In the request I submit the polish locale (pl):
When I retrieve the Locale from the request inside an Action, it is really pl.

Unfortunately Struts does not select the pl_PL.properties file automatically. I want it do display texts from the polish properties file with <bean:message>. Struts ignores this file.

Probably I have to modify the Locale in the session via the request.

I am new to Struts. Obviously the ActionServlet is the central entry point.

So what I'd like to try is to override the ActionServlet.init method and somehow write the Locale from the request into the user session.

Something like this might do the trick maybe? Found this somewhere else:



But inside the ActionServlet.init method, how do I access the request, since it does not have pageContext? Just can't find out. Please help.
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

To get the request you can just implement RequestAware interface which you can provide a setRequest(Map) method and the interceptors will set the request to whatever local Map you have in your action.

To get the session you can just implement SessionAware interface which you can provide a setSession(Map) method and the interceptors will set the session to whatever local Map you have in your action.

That should answer your original question, but I'm not sure what you are trying to do but you might actually be able to write a custom interceptor to do whatever it is you are trying to do.


Ulrich Doe
Greenhorn

Joined: Dec 09, 2010
Posts: 7
Thanks for your reply. SessionAware and RequestAware seem to be specific to Struts 2. I forgot to mention that it is Struts 1.

My problem is this: I need to internationalize an application with ResourceBundles (.properties files). I don't know this application very well yet, neither Struts. There is a general myResources.properties file It is configured in struts-config:



myResources properties file contains german language strings. It works fine with JSPs, using <bean:message>. Now I have created a Resource file for polish language and placed it in the same directory: myResources_pl.properties. Just in case I also created myResources_pl_PL.properties.

In my browser I have set polish locale and removed all others. In the HTTP header the browser sends:

Accept-Language: pl

Also as mentioned here:
http://struts.apache.org/1.x/apidocs/org/apache/struts/util/package-summary.html
I have added a locale init param to the web.xml:



And added a tag to the JSP which I use for testing:



Now,when building the application and running it, it still displays strings from myResources properties when using:



myResources_pl.properties is ignored unfortunately. Then I put this code inside a custom tag (extends TagSupport) and printed out the "key" string which DOES display the string from the pl_PL file:



So, Struts does retrieve the polish locale from the request and it can select the localized Resource bundle.

Now, the problem remains that when printing something with <bean:message/> those strings still come from the myResource.properties.

As a last resort I thought about accessing the Action.LOCALE_KEY somehow and change it programatically, maybe by overriding the ActionServlet.init method. So I was asking for help with accessing session and request from this method although I can't believe that something like this really needs to be done.

Any ideas?
Ulrich Doe
Greenhorn

Joined: Dec 09, 2010
Posts: 7
Hi,

I am happy that this was solved. The locallized properties files also needed to be copied on deployment. Becauses they were missing, Struts defaulted to the base file.
 
 
subject: How to access the request from an ActionServlet?