Spring MVC中的Locale

时间:2014-07-24 04:18:20

标签: java spring spring-mvc

我正在尝试为Spring MVC应用程序添加国际化和本地化支持。

在我的dispatcherservlet.xml中,我使用DefaultAnnotationHandlerMapping进行映射

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

在我的web.xml中

<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时,一切都好 但是课程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 - 处理程序没有适配器[public java.lang.String controller.AccountController.index()]:DispatcherServlet配置需要包含支持此处理程序的HandlerAdapter

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您可以使用ControllerClassNameHandlerMapping

<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" />