2014-04-13 1 views
0

spring-mvc 4로 웹 서비스를하고 있는데, 그것을 multilanguage으로 만들어야합니다. 이렇게 : 들어오는 url 요청이 터키어의 경우 '..?lang=tr', 영어의 경우 '..?lang=en'을 포함하는 경우. stackoverflow 그 <mvc:annotation-driven />LocaleChangeInterceptor 재정의 읽었습니다. 하지만 제거하면 앱이 작동하지 않습니다. 그리고 this tutorial을 사용하고있었습니다. 나는 아직 그 해결책을 찾지 못했다. 아래의 구성에서 언어를 전환하더라도 메시지는 항상 영어로 표시됩니다. 또한 jsp 대신 java에서 lang 메시지를 가져와야합니다. 하지만 그것은 항상 syso 때 Java 클래스에서 기본 메시지를 반환합니다. 나는 여기에서 엉망이되었다. 그리고 당신의 도움을 청합니다.spring-mvc 응용 프로그램을 다국어로 작성하는 방법은 무엇입니까?

@Autowired 
private MessageSource messageSource; 

Locale locale = LocaleContextHolder.getLocale(); 
String msg = messageSource.getMessage("deneme", null, "Deault Message!", locale); 

디스패처-servlet.xml에

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:jee="http://www.springframework.org/schema/jee" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.1.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
      http://www.springframework.org/schema/jee 
      http://www.springframework.org/schema/jee/spring-jee-3.1.xsd"> 

    <mvc:interceptors> 
     <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
       <property name="paramName" value="lang"></property> 
     </bean> 
    </mvc:interceptors> 

    <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource"> 
     <property name="basename" value="classpath:/messages"></property> 
     <property name="defaultEncoding" value="UTF-8"></property> 
    </bean> 

    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" id="localeChangeInterceptor"> 
     <property name="paramName" value="lang"></property> 
    </bean> 

    <bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver"> 
     <property name="defaultLocale" value="en"></property> 
    </bean> 

     <!-- Defining which view resolver to use --> 
    <bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" > 
      <property name="prefix" value="/WEB-INF/views/" /> 
      <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 
+1

가능한 일 ofhttp 중복됩니다 //stackoverflow.com/를 질문/22490558/internationalization-with-spring-mvc? rq = 1 – GreyBeardedGeek

+0

예. 내 질문처럼 보이는 조금. 그러나 나는 또한 자바에서 메시지를 얻는 방법을 묻는다. 해당 게시물에 따라 XML을 변경하면 'bean 명명되지 않음'이라는 localeChangeInterceptor가 '예외가 정의됩니다.'라는 메시지가 나타납니다. 그리고이 오류 역시 : 'Class'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping '은 deprecated로 표시되었습니다.' – demlik

답변

0

당신의 localChangeInterceptor을 등록하려면 XML이를 추가하고이

<mvc:annotation-driven/> 
    <context:component-scan 
     base-package="controller"> 
    </context:component-scan> 

    <bean id="handlerMapping" 
      class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> 

     </bean> 
관련 문제