Web tier: servlets, JSP, Web frameworks: Struts: Forward to same page
Imagine you have the possibility to change the displayed language on every page and when you change the language you get the same page in the choosen language. In my case I have a drop down box to change the displayed language. Until now I forward the user to the mainpage what is not very user friendly.
I search a solution you can write something like this
(of course this method doesn't exist but should show my problem more clearly)
I think it will be not so simple but I hope there's a way to do it.
Does everybody know how to do solve this problem?
- Struts: Forward to same page by Amin Mohammed-Coleman on August 04 2005 09:57 EDT
- Struts: Forward to same page by Michael Jouravlev on August 04 2005 13:15 EDT
Things you can do..
1) Keep a track of where the user is e.g hidden field
2) In the action get the location of the user
3) In the action perform the function of changing the language.
4) In the forward method return the user to the location that you got from the request.
You could then define the forward in the struts-config file.
Thanks for your tips. After some train of thoughts I thought: Why not save the users last action and insert it into the ActionForward?
So, I save on each page the last action the user have done in his session.
request.getSession().setAttribute(USER_LAST_ACTION, request.getServletPath() + "?" + request.getQueryString());
Then in the action where I need to know where he comes from (in my case: when he changes the displayed language) I modify the ActionForward. This works as follow:
ActionForward actionForward = mapping.findForward("success"); // get an existing forward...
ActionForward newActionForward = new ActionForward(actionForward); // ...to create a new one...
newActionForward.setPath((String) request.getSession().getAttribute(USER_LAST_ACTION)); // ...and change it.
This works now fine for me. Maybe there are easier ways to do it. If so I'm interested in it.
Consider this approach:
* you navigate from browser strictly to actions
* each action can show one or more JSP pages
* each action/actionform combination describes a stateful object; for this you would have DispatchAction-style action with event handlers and corresponding ActionForm with "session" scope
* when you simply navigate to action, it displays a page corresponding to current action state.
In the above approach you would change language and then "reload" current action. It would display a page corresponding to its current state. In your case, it will redisplay the page, and will load messages from another resource for different language.
If you are OK with session-scoped actionforms and with DispatchAction, you may try Struts Dialogs extension: struts.sourceforge.net/strutsdialogs.
That's a good point you mention. Maybe I should consider Struts Dialogs in this case. It seems useful for many opportunities. Thanks for the tip.
In my current project I use the solution only for a tiny part. The main part works in an other way where I use a DB for the needed information.
But there's one thing I want to know. You speak from "If you are OK with session-scoped...". What is the alternative in my case? I know you can use also request-scope but for this problem I don't see the use of it.
You speak from "If you are OK with session-scoped...". What is the alternative in my case? I know you can use also request-scope but for this problem I don't see the use of it.Well, I meant that action form has session scope. It is important if you use redirect (I use redirection in my library). Of course, the language setting itself should reside somewhere in the session despite form scope.
Ok then it's ok. Yes, the language setting is stored in session and not only in the form as a Locale object. I don't have printed the code for that here.
You can get input of action from "mapping.getInput()"