添加了一个新的cookie,而不是替换现有的饼干
我刚刚使用Spring Boot Configuration将Web应用程序作为基础结束。
@Bean
public LocaleResolver localeResolver() {
return new CookieLocaleResolver();
}
由于需要,应该通过按下按钮来更改网站的语言环境/语言。所述功能是通过一点点JS和cookie实现的。
<script>
function updateCookie(lang) {
let name = "org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE"
document.cookie = name+"="+lang
location.reload()
}
</script>
<a onclick="updateCookie('de')" class="flag-icon flag-icon-de mr-2"></a>
这个想法是在单击按钮时更新上述cookie,并在整个应用程序中使用它。这可以正常工作,直到我试图在应用程序中调用特定端点为止。
为了调试我的应用程序,我使用:
window.onload = function () {
alert(document.cookie)
}
现在解决我的问题:
用户测试应用程序时,这是警报反馈:
org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=de
切换到其他页面,刷新,更改语言等。适当地以不同的值重置cookie。
但是,当调用特定端点时,我会得到以下警报:
org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=de;
org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=fr
而不是重置/更改现有的cookie,而是添加了一个新的cookie。添加了看似随机的半隆。
使用类似的逻辑和几乎相同的实现,端点不会发生这种情况。 除了我发布的JS代码之外,没有其他逻辑,也没有触摸后端中的cookie。
不幸的是,我没有想法。任何提示/帮助将不胜感激。
I just finished localizing my web application using spring boot configuration as a base.
@Bean
public LocaleResolver localeResolver() {
return new CookieLocaleResolver();
}
Due to a requirement one is supposed to be able to change locale/language of the website by pressing a button. Said function is implemented with a little bit of JS and a cookie.
<script>
function updateCookie(lang) {
let name = "org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE"
document.cookie = name+"="+lang
location.reload()
}
</script>
<a onclick="updateCookie('de')" class="flag-icon flag-icon-de mr-2"></a>
The idea is to update said cookie on click of a button and use it throughout the whole application. This works fine until I am trying to call a specific endpoint in my application.
In order to debug my application I use:
window.onload = function () {
alert(document.cookie)
}
Now to my problem:
When User-Testing the application this is the alert-feedback:
org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=de
Switching to other pages, refreshing, changing language etc. properly resets the cookie with a different value.
When calling a specific endpoint though, I get the following alert:
org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=de;
org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=fr
Instead of resetting/changing the existing cookie, a new one is added with the value 'de;'. A seemingly random semicolon is added.
This doesn't happen with endpoints using similar logic and almost identical implementation.
There is no further logic outside the little bit of JS code I've posted and I'm not touching the cookie in the backend.
Unfortunately I'm out of ideas. Any tips/help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论