2014-07-24 55 views
0

我正在嘗試爲Spring MVC應用程序添加國際化和本地化支持。Spring MVC中的區域設置

在我的dispatcherServlet.xml,我在web.xml中使用DefaultAnnotationHandlerMapping映射

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" > 
    <property name="interceptors"> 
     <list> 
     <ref bean="localeChangeInterceptor" /> 
     </list> 
    </property> 
</bean> 

<web-app> 
<display-name>Archetype Created Web Application</display-name> 

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/dispatcherservlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>*.html</url-pattern> 
</servlet-mapping> 

<welcome-file-list> 
    <welcome-file>redirect.jsp</welcome-file> 
</welcome-file-list> 

我控制器

@Controller 
@RequestMapping(value = "/account") 
public class AccountController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String index() { 
     return "index"; 
    } 

    @RequestMapping(value = "/register", method = RequestMethod.GET) 
    public String showForm(Map<String, Object> mm) { 
     mm.put("subcriber", new Sucriber()); 
     return "register"; 
    } 

    @RequestMapping(value = "/register", method = RequestMethod.POST) 
    public String register(@ModelAttribute("subcriber") Sucriber sucriber,BindingResult result,Map<String, Object> mm) { 

     SucriberValidator sucriberValidator = new SucriberValidator(); 
     sucriberValidator.validate(sucriber, result); 

     if (result.hasErrors()) { 
      return "register"; 
     } else { 
      return "success"; 
     } 
    } 
} 

當我去myhost/myhost:myport/myproject,每件事都OK 但類DefaultAnnotationHandlerMapping已棄用。 在Spring 3.2中支持RequestMappingHandlerMapping。所以我使用類RequestMappingHandlerMapping

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" > 
    <property name="interceptors"> 
     <list> 
     <ref bean="localeChangeInterceptor" /> 
     </list> 
    </property> 
</bean> 

當我去MYHOST /爲myhost:MyPort上/ myproject,而我得到的錯誤

HTTP狀態500 - 無適配器處理程序[公共java.lang.String中controller.AccountController.index( )]:DispatcherServlet的配置需要包括支持該處理器

感謝任何幫助

+0

檢查此http://stackoverflow.com/questions/18287711/spring-mvc-3-localechangeinterceptor。 – apurvc

+0

當我使用RequestMappingHandlerMapping替換DefaultAnnotationHandlerMapping時,會發生問題。我不知道爲什麼。我將@RequestMapping(value =「/」,method = RequestMethod.GET)添加到索引方法,但結果仍爲HTTP狀態404 –

+0

如果使用'RequestMappingHandlerMapping',則還需要添加'RequestMappingHandlerAdapter'。但是,使用''然後使用''來註冊攔截器會更容易。 –

回答

1

可以使用ControllerCl一個的HandlerAdapter assNameHandlerMapping

<bean 
    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> 
<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> 

<!-- Most controllers will use the ControllerClassNameHandlerMapping above, 
    but for the index controller we are using ParameterizableViewController, 
    so we must define an explicit mapping for it. --> 
<bean id="urlMapping" 
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
     <props> 
      <prop key="index.html">indexController</prop> 
     </props> 
    </property> 
</bean> 

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 

<!-- The index controller. --> 
<bean name="indexController" 
    class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
    p:viewName="index" />