jstl java代码,如何从Java代码设置JSTL语言环境?

I want to set the JSTL locale which is used by and friends. I know this is possible with , but I need to do it dynamically (depending on user data retrieved from my DB) and would prefer Java code - a filter class, to be precise.

I thought setting the session attribute javax.servlet.jsp.jstl.fmt.locale to my desired Locale instance would do the trick, but it is ignored: The JSTL tags keep using the browser locale.

I verified there are no page context or request attributes of the same name.

So what am I doing wrong? Or do I really need to do it from a JSP?

Reading the JSTL code, I found references to a LocalizationContext and think I need to set one. I couldn't quite figure out exactly how it fits into the picture or how to set one, though.

解决方案

you need the 2nd:

3 ways to set JSTL locale:

/as well as default application resource bundle, time zone, and data source/

Set by a JSTL action – this allows specification of scope by the scope attribute.

Set Programmatically – allows specification of scope via the Config API.

import javax.servlet.jsp.jstl.core.Config;

(...)

Config.set( session, Config.FMT_LOCALE, new java.util.Locale("en", "US") )

// or Locale.forLanguageTag("en-US") (java 1.7 and later)

Set by Context Initialization Parameters – specifies value used if setting not found in any of the standard scopes.

javax.servlet.jsp.jstl.fmt.locale

en_US

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值