我一直很高兴地使用总括的Faces.getLocale()来获取当前登录用户使用的区域设置(这反过来是从<f:view>定义中获得的)。我非常喜欢从视图到客户端到系统默认区域设置的回退方法,因为它符合我的应用程序中区域选择的要求:
Accept-Languages header的最高排名语言现在,我已经开始使用JAX (resteasy实现),并且发现很难编写一个能够为当前用户的地区提供后端代码的服务。
我不能使用Faces.getLocale(),因为这需要一个在JAX请求处理过程中不存在的FacesContext。
我不能在@Context SecurityContext中使用@Provider注释(这将给我用户首选的区域设置)或@Context HttpHeaders (访问客户端区域设置),因为JAX只在它使用提供者本身时注入这些注释,而不是在后端代码实例化类时。
而且我不想用Locale参数乱扔我的方法签名,因为几乎所有的东西都需要一个区域设置。
要有一个具体的例子:我有一个vcard生成器,它根据用户喜欢的地区生成很少的注释字段。我可以通过JSF/EL调用vcard生成方法:
<h:commandLink action="#{vcfGenerator.forPerson(person)}"
value="Go" target="_blank" />通过休息服务:
@GET @Path('person/{id:[1-9][0-9]*}/vcard')
@Produces('text/vcard')
String exportVcard(@PathParam('id') Long personId, @Context HttpHeaders headers) {
VcfGenerator exporter = Component.getInstance(VcfGenerator) as VcfGenerator
Person person = entityManager.find(Person, personId)
if (! person)
return Response.noContent().build()
def locale = headers.acceptableLanguages[0] ?: Locale.ROOT
return exporter.generateVCF(person, locale).toString()
}这是可行的(VcfGenerator有一组只使用Faces.getLocale()的JSF方法),但是维护起来很痛苦。因此,与其传递Locale对象,我想说的是:
Vcard generateVCF(Person person) {
Locale activeLocale = LocaleProvider.instance().getContext(VcfGenerator.class)
ResourceBundle bundle = ResourceBundle.getBundle("messages", activeLocale, new MyControl())
// use bundle to construct the vcard
}有没有人做过类似的工作并能分享自己的见解?
发布于 2014-04-23 14:31:50
我知道这是在一段时间前发布的,但由于它尚未被标记为已解决,下面是我如何解决这一具体情况的方法:
现在,这在JSF和JAX上下文中都可以使用。
希望这能帮上忙
https://stackoverflow.com/questions/18594566
复制相似问题