如何从JSTL /表达式语言中的JSON字符串中提取单个值?(How can I extract a single value, from a JSON String, in JSTL / Expre

编程入门 行业动态 更新时间:2024-10-26 21:32:25
如何从JSTL /表达式语言中的JSON字符串中提取单个值?(How can I extract a single value, from a JSON String, in JSTL / Expression Language?)

我在我的一个Web应用程序中实现了多种语言。 用户选择的语言保存在cookie中。 我已经能够从这个cookie中提取内容,如下所示:

${cookie.userdata.value}

我遇到的问题是语言不是我存储在这个cookie中的唯一东西。 当用户上次访问通知时也有一些时间戳。 Cookie的完整值采用JSON格式:

userdata={ "notificationsViewed":1509696860036, "documentNotificationsViewed":1509696859896, "language":"en" }

我需要的仅仅是"en" 。

在其他情况下,我使用javascript和JSONParser来获取语言,但我有一种情况,即能够使用JSTL / Expression Language获取语言会非常棒,但我一直无法找到。 到目前为止,我的尝试导致:

<c:set var="language">${cookie.userdata.value.language}</c:set> javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'language'.

我需要以某种方式使用JSON方法,但我甚至不知道它是否可能。 有人知道吗?

I am implementing multi languages in one of my web applications. The language selected by the user, is saved in a cookie. I have been able to extract the contents from this cookie, this way:

${cookie.userdata.value}

The problem I am having, is that language is not the only thing I store in this cookie. There are also some timestamps for when the user last has accessed notifications. The full value of the cookie is in JSON format:

userdata={ "notificationsViewed":1509696860036, "documentNotificationsViewed":1509696859896, "language":"en" }

What I need, is simply the "en".

In other cases, I use javascript, and JSONParser to get the language, but I have a situation where it would be brilliant to be able to fetch the language using JSTL / Expression Language, but I have been unable to find one. My attempts so far results in:

<c:set var="language">${cookie.userdata.value.language}</c:set> javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'language'.

I need to somehow use a JSON approach, but I don't even know if it is possible. Does anyone know?

最满意答案

在我工作的一个项目中,我做了以下工作:

在Java方面,将语言设置为一个meber变量,可以使用getter在JSP中访问该变量。

您可以使用您设置的Cookie或任何其他方式(用户输入,请求标题...)来执行此操作。

在JSP中,获取该值并将其用于渲染页面之前完成的任何本地化。

然后,作为呈现页面的一部分,包括本地化javascript数组:

... <script> var l10n = { "language" : "${language}" }; </script> </body>

通过这种方式,在页面被渲染后,您可以使用语言l10n.language (在JavaScript中)

这或多或少是在另一种情况下,Wordpress处理JavaScript脚本的本地化。

In a project I worked on I did the following:

On the java side set the language to a meber variable that can be accessed in the JSP with a getter.

You can do this using the cookie you set or by any other means (user input, request header, ...)

In the JSP get hold that value and use it for any localization done before rendering the page.

Then as part of the rendered page include a localization javascript array:

... <script> var l10n = { "language" : "${language}" }; </script> </body>

In this way, after the page hs been rendered you can access the language as l10n.language (in javascript)

This is more or less how, in another context, Wordpress handles localization of javascript scripts.

更多推荐

language,cookie,语言,The,电脑培训,计算机培训,IT培训"/> <meta name="descri

本文发布于:2023-08-04 20:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1421382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   字符串   语言   JSON   JSTL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!