Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 春季及;ThymileAF:更改区域设置并停留在当前页面上_Spring_Spring Mvc_Url_Internationalization_Thymeleaf - Fatal编程技术网

Spring 春季及;ThymileAF:更改区域设置并停留在当前页面上

Spring 春季及;ThymileAF:更改区域设置并停留在当前页面上,spring,spring-mvc,url,internationalization,thymeleaf,Spring,Spring Mvc,Url,Internationalization,Thymeleaf,我有一个传统的Spring4/Thymeleaf i18n应用程序 我可以轻松地用classic切换区域设置 org.springframework.web.servlet.i18n.LocaleChangeInterceptor 及 切换时,我总是发送到服务器/home?lang=fr。它很好用。但我需要更复杂的行为。 我需要做的是在切换区域设置时保留当前页面 我用这个thymeleaf片段找到了一个半有效的解决方案: th:with="currentUrl=(${#httpServletR

我有一个传统的Spring4/Thymeleaf i18n应用程序 我可以轻松地用classic切换区域设置

org.springframework.web.servlet.i18n.LocaleChangeInterceptor

切换时,我总是发送到服务器/home?lang=fr。它很好用。但我需要更复杂的行为。 我需要做的是在切换区域设置时保留当前页面

我用这个thymeleaf片段找到了一个半有效的解决方案:

th:with="currentUrl=(${#httpServletRequest.pathInfo + '?' + #strings.defaultString(#httpServletRequest.queryString, '')})
问题是我需要在许多角落案例中实现自己:

  • 当已经存在任何查询参数时
  • 如果存在一个lang=en参数
  • 等等

有人知道如何使用本机Spring或Thymeleaf工具管理此案例吗?或者我需要为Thymeleaf编写自己的处理器?

我在项目中使用它&它工作得很好。当语言环境发生变化时,我不会特别重定向到任何URL

@Bean
public LocaleResolver localeResolver()
{
    Locale defaultLocale = new Locale( env.getProperty( Constants.DEFAULT_LOCALE ) );
    CookieLocaleResolver clr = new CookieLocaleResolver();
    clr.setDefaultLocale( defaultLocale );
    return clr;
}

@Bean
public LocaleChangeInterceptor localeChangeInterceptor()
{
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    lci.setParamName( "lang" );
    return lci;
}

最简单的解决方案是连接“requestURI”和“queryString”。 这种方法的缺点是,如果在同一个链接上单击多次,参数就会一次又一次地添加

解决方法是编写一个函数,在添加参数之前“清理”url


对于代码示例,请查看以下问题:

当更改区域设置时,用户应保持在同一页面上。您的代码不合适。如何更改区域设置?它不是在使用“lang”查询参数吗?我使用的是经典的方法,就像您一样,但我的问题是如何使用lang param动态生成当前页面链接。您是否有一个示例应用程序来测试此功能?将很高兴能够对一个已经设置的项目工作,试图找到一个解决方案。我猜您的语言选择器在每一页上都显示为页眉或页脚。我希望设置一些拦截器,为包含当前页面url的模型添加一个值。然后模板只负责为页面输出那些特定于语言的URL。这也可能是答案:Thank you就像一个符咒。只有一个精度-忘记了。build()。引用的ServletUriComponentsBuilder.fromCurrentRequest().replaceQueryParam(param.build().toUriString()中的函数调用@是的,没有缺少build()方法调用,因为根据文档:
是一个快捷方式方法,它将对build()的调用、对UriComponents.encode()的调用和对UriComponents.toUriString()的调用组合在一起。
@Bean
public LocaleResolver localeResolver()
{
    Locale defaultLocale = new Locale( env.getProperty( Constants.DEFAULT_LOCALE ) );
    CookieLocaleResolver clr = new CookieLocaleResolver();
    clr.setDefaultLocale( defaultLocale );
    return clr;
}

@Bean
public LocaleChangeInterceptor localeChangeInterceptor()
{
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    lci.setParamName( "lang" );
    return lci;
}