Spring Boot 中@RequestMapping 和Locale 有什么关系?

What is the relation between @RequestMapping and Locale in Spring Boot?

如果我的理解是正确的,你可以 add/remove 自由地为 saveEmployee() 添加参数。例如,当您按如下方式添加 "loc" 时, saveEmployee() 会在事件发生时收到 "non-null object"。 queryParams 也是如此。

@Controller
public class Employee {
  @RequestMapping("/save")
  public void saveEmployee(Locale loc, 
                           @RequestParam Map<String, String> queryParams) {
    // saving employee
  }
}

这个方法怎么能通过在这里添加一个参数 "loc" 来接收非空的 Locale 对象呢? 我想知道这背后的逻辑。

Spring 的 DispatcherServlet 将请求从客户端转发到您的控制器,为您提供该参数。为了做到这一点,它从 bean(Controller) 所属的 ApplicationContext 中搜索对象。

Spring 通过使用 LocaleResolverLocaleContextResolver 为您完成当前请求的区域设置,实际上由可用的最具体的区域设置解析器确定,在 MVC 环境中配置的LocaleResolver / LocaleContextResolver

21.3.3 Defining @RequestMapping handler methods

An @RequestMapping handler method can have a very flexible signatures. The supported method arguments and return values are described in the following section. Most arguments can be used in arbitrary order with the only exception of BindingResult arguments.

Supported method argument types

java.util.Locale for the current request locale, determined by the most specific locale resolver available, in effect, the configured LocaleResolver / LocaleContextResolver in an MVC environment.

Spring 查看方法参数、它们的类型和注释,然后确定它是否可以提供 type/annotation.

的对象

如果不能,它会抛出一个异常,否则它会用它认为适合 type/annotation.

的对象调用方法

有关支持的列表 types/annotations,请阅读文档:
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-arguments

如您所见,列出了 java.util.Locale

我认为您需要 xml 区域设置
您要尝试以下 xml 设置

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