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

标签 java spring spring-boot spring-mvc annotations

如果我的理解是正确的,您可以自由添加/删除 saveEmployee() 的参数。例如,当您按如下方式添加“loc”时,saveEmployee() 在事件发生时会收到“非空对象”。 queryParams 也是如此。

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

这个方法如何通过在此处添加参数“loc”来接收非空 Locale 对象? 我想知道这背后的逻辑。

最佳答案

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.

关于java - Spring Boot中@RequestMapping和Locale有什么关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58470252/

相关文章:

java - 为什么 Spring Cloud Consul 不能与独立的 Tomcat 一起使用?

Spring:正确设置@ComponentScan

java - 如何在 Spring Boot 中创建不同的 ThreadPoolTask​​Executor?

java - Spring boot中的RestController - 404错误而不是JSON对象

java - 为什么无限循环在加载数据时 hibernate

java - 在 Java 控制台中检测 Ctrl+C

java - 为 Java 应用程序创建启动屏幕的最佳方法?

java - ActiveMQ 在不使用 JMX、JMS 的情况下获取队列大小

java - SWT 文本 : Remove default context menu item in Linux

spring - NoSuchBeanDefinitionException :No qualifying bean of type found for dependency with spring test and Junit