gpt4 book ai didi

java - Spring MVC 中的语言环境

转载 作者:行者123 更新时间:2023-12-01 12:42:55 25 4
gpt4 key购买 nike

我正在尝试向我们的 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

@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 Status 500 - No adapter for handler [public java.lang.String controller.AccountController.index()]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler

感谢您的帮助

最佳答案

您可以使用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" />

关于java - Spring MVC 中的语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24925053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com