Spring MVC 3 Locale changing using a link not working - spring-mvc

Edit: My Spring framework version 3.0.5
A small issue here, The language is not changing when I click the language changer link.
The language files (messages_xx.properties) are in the classpath i18n directory. The files are:
i18n/messages_en.properties
i18n/messages_ar.properties
Spring Configuration
<!-- Component scanner. This is used to automatically find Spring annotations like #Service and #Repository -->
<context:component-scan base-package="com.keype" />
<!-- Annotation driven programming model -->
<mvc:annotation-driven />
<context:annotation-config />
<mvc:resources mapping="/static/**" location="/static/" />
<!-- Session Object Configuration -->
<bean id="session" class="com.keype.system.Session" scope="session">
<aop:scoped-proxy />
</bean>
<!-- The View Resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"
/>
<!-- i18n Configuration. Default language is english. Change language using ?language=en -->
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<!-- Message text files. This is set UTF-8 to display Arabic UTF correctly. -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:i18n/messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
A section from the JSP Code
<spring:message code="header.arabic" /> |
<spring:message code="header.english" />
The issue is, when I click the above link to change the language, the locale changing functionality is not working. I tested by changing the "defaultLocate" to "ar" and I'm getting Arabic text.
What could possibly be wrong here? There is nothing in the tomcat log also.

You have to register the localeChangeInterceptor among the MVC interceptors for Spring-MVC to consider it. Add the interceptor to the configuration:
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"></property>
</bean>
</mvc:interceptors>

<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="lang" />
</mvc:interceptors>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>

Another thing that can help others:
In my case, I MUST add in the applicationContext.xml. Putting it in the spring-servlet (ref. dispatcher), not worked at all.

You need to register the LocaleChangeInterceptor inside the mvc interceptors tag as below,
E.g.
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="siteLanguage" />
</bean>
</mvc:interceptors>
I was getting the same error and it worked using this code :-)

Related

In Spring MVC 3 local is not getting changed when clicking local changing link

I have set up i18n to my Spring MVC project . The language is not changing when I click on the link:
Hindi
English
controller-servlet.xml
<mvc:annotation-driven/>
<context:component-scan base-package="com.avvas.search.controller" />
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:resources/messages" />
<property name="defaultEncoding" value="UTF-8"/>
<property name="cacheSeconds" value="0" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/javascript/**" location="/javascript/" />
local changing links:
Hindi
English
The issue is that when i click on hindi the page should be displayed in Hindi language, Language is not getting changed. However i changed default language to hindi <property name="defaultLocale" value="hi"/> then it is taking local as hindi but when i click on english local change link the local is not getting changed.
<mvc:annotation-driven/> is registering its own handler mapping (RequestMappingHandlerMapping) and your interceptor configuration is not applied to that.
Define your interceptor like this:
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
Here is the Complete code. Basically we need to register our interceptors explicitly.
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
In above given xml you have given "localeChangeInterceptor" but you haven't mentioned anywhere that it should intercept all the requests by default.
So Spring will load all the beans but it wouldn't register it as interceptor until or unless you define that which requests it should intercept.
In below example i am defining it in mvc interceptor and telling the framework that it should consult that interceptor before processing any request.
<mvc:interceptor>
<mvc:mapping path="/**/**/" />
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="locale" />
</bean>
</mvc:interceptor>
</mvc:interceptors>

Spring locale resolver - current locale in url

I use this simple configuration
<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basename" value="classpath:localization/messages" />
<beans:property name="defaultEncoding" value="UTF-8" />
</beans:bean>
<interceptors>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
</interceptors>
<beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<beans:property name="defaultLocale" value="en" />
<beans:property name="cookieMaxAge" value="100000" />
</beans:bean>
Works nice but what if someone will send an url to someone else? This works only with a client browser, If user changes a localization, it will set a cookie. I have seen a lot of websites that they have a localization after the first slash in the url (e.g. nette.org/en/about). When someone send this link, it will resolve localization depending on url. Does exist any simple solution to do this in spring framework? Somehow map this place in url?
Instead of CookieLocaleResolver use SessionLocaleResolver.
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
I think this should resolve your problem.
Cheers.

spring generating wrong path to forward

I have added view resolver to my spring-servlet.xml like this
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="suffix" value="/WEB-INF/jsp/"></property>
<property name="prefix" value=".jsp"></property>
</bean>
but when i'm running my web application it returns a wrong path as shown in log
Forwarding to resource [.jsphello/WEB-INF/jsp/] in InternalResourceView 'hello'
whereas it should be WEB-INF/jsp/hello.jsp
Why is this happening?
Switch prefix and suffix in your configuration:
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
Then it will result in:
/WEB-INF/jsp/hello.jsp
Prefix always goes before, while suffix goes after.

How to resolve the confliction between "<resources" tag and "<context:component-scan.." tag for Spring MVC showcase project

I'm currently working on a Spring MVC project and have some issue with using "<MVC:resource "tag of SpringMVC to load static resource. So I downloaded the springMVC showcase project and did some change on it to check this tag.
Since my project is a simple one, seems to me the two tags for "conversionservice" is not necessary.
However after I removed this two tag, something wired happend.
If I have both the tag for static resources "<resources mapping="/resources/.." and the "<context:component-scan base-package="org.springframework.samples.mvc" />" tag (in controllers.xml) configged, then I cann't access any uri that anotated on controllers- it returns a 404 not found error. if I comment out the resource mapping tag, then those controllers works fine.
Anyone have ever experience this situation? Any idea how to get around that?
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven conversion-service="conversionService">
<argument-resolvers>
<beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/>
</argument-resolvers>
</annotation-driven>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Only needed because we install custom converters to support the examples in the org.springframewok.samples.mvc.convert package -->
<beans:bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<beans:property name="formatters">
<beans:bean class="org.springframework.samples.mvc.convert.MaskFormatAnnotationFormatterFactory" />
</beans:property>
</beans:bean>
<!-- Imports user-defined #Controller beans that process client requests -->
<beans:import resource="controllers.xml" />
</beans:beans>
<context:annotation-config/> just don't work on Spring 3.1.0, but <mvc:annotation-driven/> just works, I referenced this post
I got the same issue and what I did is to remove the "**" in the resource tag.
I had faced similar issue - SO Question
You can either use <mvc:annotation-driven/> or provide handler mapping yourself with order of higher precedence -
<bean id="annotationUrlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="0" />
</bean>
<bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

Glassfish: web application deployed with non-root context interprets requests relative to domain1/docroot

The webapp uses Spring MVC.
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/*" value-ref="defaultHandler"/>
</map>
</property>
<property name="order" value="2"/>
</bean>
<bean name="defaultHandler" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=""/>
</bean>
So requests like http://localhost:8080/application-context-folder/index.jsp
should resolve to application-context-folder/index.jsp and they resolve to domain1/docroot/application-context-folder.
Is it by design or do I need to change something in the application or configuration ?
#Edit: there was a typo, the requested URL is http://localhost:8080/application-context-folder/index.jsp, not http://localhost:8080/index.jsp
Use redirect to your application context. Place an index.html file in the docroot folder of your domain. File may look something like this:
<html>
<head>
<title>Your application title</title>
<frameset>
<frame src="http://localhost:8080/[application_context]">
</frameset>
</head>
<body>
Redirecting to Some title...
</body>

Resources