Reputation: 111
I have multilanguage installed on my spring app, and I don't want the 'lang' parameter to appear in the URL (I'm storing the locale with sessionLocaleResolver or cookieLocaleResolver/neither works). I've searched for hours to find a solution to this issue, although apparently it should already be working with what I've done. Here's my code :
The view (welcome.jsp) :
Language : <a href="?lang=en">English</a>|<a href="?lang=fr_FR">Francais</a>|<a href="?lang=ro_RO">Romanian</a>
<h2>welcome.springmvc : <spring:message code="welcome.text" text="default text" /></h2>
My servlet.xml
<!-- Multi-Language / Localization Setup -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptor>
</mvc:interceptors>
<mvc:annotation-driven ignore-default-model-on-redirect="true" />
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:lang.files/welcome</value>
<!-- <value>classpath:messages2</value> -->
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
<property name="cookieName" value="lang" />
<property name="cookieMaxAge" value="60000" />
<property name="cookiePath" value="/" />
</bean>
My Controller code - note that I'm staying in the same page when I click on the link to change the language. Because ideally it would be a dropdown, and I'd stay in the same page
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
String message = "<br/ ><div style='text-align:center;'>"
+ "<h3>********** Hello World, Spring MVC Tutorial</h3></div><br />";
ModelAndView mv = new ModelAndView();
// ModelAndView mv = new ModelAndView(new
// RedirectView("welcome.do", true, true, true));
RedirectView view = new RedirectView("/welcome", true);
view.setExposeModelAttributes(false);
mv.setView(view);
mv.addObject("message", message);
mv.addObject("activities", activityService.getAll());
mv.addObject("act", activityService.getR());
return new ModelAndView(view);
}
The result is a '404' saying that 'localhost/myapp/welcome' is not found.
Finally this the controller, working, but adding the 'lang' parameter in the URL :
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
String message = "<br/ ><div style='text-align:center;'>"
+ "<h3>********** Hello World, Spring MVC Tutorial</h3></div><br />";
ModelAndView mv = new ModelAndView();
mv.addObject("message", message);
mv.addObject("activities", activityService.getAll());
mv.addObject("act", activityService.getR());
return mv;
}
Please help. Thanks :)
Upvotes: 1
Views: 5016
Reputation: 1770
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
String message = "<br/ ><div style='text-align:center;'>"
+ "<h3>********** Hello World, Spring MVC Tutorial</h3></div><br />";
ModelAndView mv = new ModelAndView();
// ModelAndView mv = new ModelAndView(new
// RedirectView("welcome.do", true, true, true));
RedirectView view = new RedirectView("/welcome", true);
view.setExposeModelAttributes(false);
mv.setView(view);
mv.addObject("message", message);
mv.addObject("activities", activityService.getAll());
mv.addObject("act", activityService.getR());
return new ModelAndView(view);
}
This code causes an infinite loop as you are redirecting to /welcome over and over again.
Moreover when you've got a link like <a href="?lang=en">English</a>
the parameter will always appear in the url when you click on it.
The easiest way(not best) to achieve the goal you want is to create another controller method like
@RequestMapping("/changeLocale")
public ModelAndView changeLocale() {
RedirectView redirectView = new RedirectView("/welcome");
redirectView.setExposePathVariables(false);
return new ModelAndView(redirectView);
}
And links will look like
<a href="/changeLocale?lang=en">English</a>|<a href="/changeLocale?lang=fr_FR">Francais</a>|<a href="/changeLocale?lang=ro_RO">Romanian</a>
Upvotes: 1