gpt4 book ai didi

spring-mvc - 使用语言标识符映射 url

转载 作者:行者123 更新时间:2023-12-04 06:20:00 25 4
gpt4 key购买 nike

有没有一种很好的方法可以根据 URL 和另一方面的 map 请求来解析语言环境,而无需任何其他要求?

例如

http://example.com/ru/news    
http://example.com/iw/news

并且在 Controller 中仍然使用标准映射
@Controller
@RequestMapping(value = "/news")
public class NewsController {

// Controller methods ...

}

最佳答案

我不知道有一个开箱即用的解决方案,但是使用自定义拦截器和一些明智选择的映射很容易实现。

编写 HandlerInterceptor 的实现实现 preHandle以便从请求 URI 中提取语言环境字符串,然后使用该语言环境标记请求( see the source code for 类似的 LocalChangeInterceptor ,它执行与您需要的类似的事情,但使用请求参数而不是路径变量)。

然后 wire it up使用 <mvc:interceptor>例如

<mvc:interceptors>
<mvc:interceptor>
<mapping path="/*"/>
<bean class="x.y.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>

然后,您可以放松 Controller 上的请求映射以容忍(并忽略)URI 的区域设置部分:
@Controller
@RequestMapping(value = "*/news")
public class NewsController {

// Controller methods ...

}

关于spring-mvc - 使用语言标识符映射 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6708884/

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