无法从jsf资源库中找到或提供本地化资源
我试图复制一本书中的一个例子 结构可以从下面附的图片中可视化- 已更新 将使用库的视图与下面给出的视图一样简单-无法从jsf资源库中找到或提供本地化资源,jsf,jsf-2,localization,resources,outputstylesheet,Jsf,Jsf 2,Localization,Resources,Outputstylesheet,我试图复制一本书中的一个例子 结构可以从下面附的图片中可视化- 已更新 将使用库的视图与下面给出的视图一样简单- <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core"> <h:head> <title></title
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title></title>
<h:outputStylesheet library="#{facesContext.viewRoot.locale}/default" name="css/rafa.css"/>
<h:outputScript library="#{facesContext.viewRoot.locale}/default" name="rafa.js"/>
</h:head>
<h:body>
<f:view locale="#{localeBean.mylocale}">
<div style="width:100%; text-align:center">
<h:form>
<h:commandButton value="Rafa at Roland Garros" action="#{localeBean.changeLocale('fr')}"/>
<h:commandButton value="Rafa at Wimbledon" action="#{localeBean.changeLocale('en')}"/>
<h:commandButton value="Rafa at US Open" action="#{localeBean.changeLocale('us')}"/>
<h:commandButton value="Rafa at Australian Open" action="#{localeBean.changeLocale('au')}"/>
</h:form>
</div>
<div style="width:100%; text-align:center">
<!--<h:graphicImage library="#{facesContext.viewRoot.locale}/default" name="img/rafa.png"/> -->
<h:graphicImage value="#{resource[facesContext.viewRoot.locale+='/default:img/rafa.png']}"/>
</div>
</f:view>
</h:body>
</html>
我使用的是Glassfish 4.1.1应用服务器
,内部使用的是Mojarra 2.2.12
呈现的HTML
是-
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="j_idt2">
<title></title>
<script type="text/javascript" src="/glassfish-ee/javax.faces.resource/rafa.js.xhtml?ln=fr/default&v=1_01_2">
</script><link type="text/css" rel="stylesheet" href="/glassfish-ee/javax.faces.resource/css/rafa.css.xhtml?ln=fr/default&v=1_0" />
</head>
<body>
<div style="width:100%; text-align:center">
<form id="j_idt8" name="j_idt8" method="post" action="/glassfish-ee/ch5/ch5_12/index.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt8" value="j_idt8" />
<input type="submit" name="j_idt8:j_idt9" value="Rafa at Roland Garros" />
<input type="submit" name="j_idt8:j_idt10" value="Rafa at Wimbledon" />
<input type="submit" name="j_idt8:j_idt11" value="Rafa at US Open" />
<input type="submit" name="j_idt8:j_idt12" value="Rafa at Australian Open" />
<input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="---somevalue---" autocomplete="off" />
</form>
</div>
<div style="width:100%; text-align:center">
<!--<h:graphicImage library="fr/default" name="img/rafa.png"/> -->
<img src="/glassfish-ee/javax.faces.resource/img/rafa.png.xhtml?ln=fr/default&v=1_0" />
</div>
</body>
</html>
在控制台上,我得到这个-
2016-06-08T20:54:52.672+0530|WARNING: JSF1064: Unable to find or serve resource, rafa.js, from library, fr/default.
2016-06-08T20:54:52.673+0530|WARNING: JSF1064: Unable to find or serve resource, css/rafa.css, from library, fr/default.
2016-06-08T20:54:52.717+0530|WARNING: JSF1064: Unable to find or serve resource, img/rafa.png, from library, fr/default.
2016-06-08T20:54:57.570+0530|WARNING: JSF1064: Unable to find or serve resource, css/rafa.css, from library, fr/default.
2016-06-08T20:56:05.587+0530|WARNING: JSF1064: Unable to find or serve resource, img/rafa.png, from library, fr/default.
2016-06-08T20:56:09.542+0530|WARNING: JSF1064: Unable to find or serve resource, img/rafa.png, from library, fr/default.
谷歌浏览器的控制台标签显示-
那么最后,我错在哪里呢?请建议
在接受了下面建议的答案的帮助后,进行了一些更改 带条目-
javax.faces.resource.localePrefix=fr
通过GET请求访问页面时,会弹出一条警报,原因是1_2.js
文件中的以下条目在上面所附的结构中被描述为selected
alert("J'ai gagné Roland Garros ...(resources/fr/1_0/css/js/rafa.js/1_2.js)");
即使在按下所需的命令按钮更改区域设置后,我仍会看到相同的警报弹出
为什么??还需要做些什么?提前感谢。资源本地化功能在中的文档记录非常差。与您直观的预期相反,本地化的子文件夹不会与
匹配。相反,它们将与用值为的键标识的
条目相匹配。此外,您不需要在库
属性中显式地包含{view.locale}
为了使其正常工作,请确保已在faces config.xml
中声明了
<application>
<message-bundle>com.example.i18n.YourBundle</message-bundle>
</application>
然后,您就可以按照通常的方式引用资源,而不会弄乱区域设置前缀和版本后缀
<h:outputStylesheet library="default" name="css/rafa.css" />
<h:outputScript library="default" name="rafa.js" />
我承认这可以做得更好。当未指定
和/或缺少javax.faces.resource.localePrefix
时,它应该返回到UIViewRoot#getLocale()
。尽管如此,这无疑是JSF最少见的特性之一。坦率地说,直到今天我才尝试过它,我认为我永远不会在现实世界的应用程序中使用它。您需要在特定于语言环境的消息包中将所需的资源语言环境前缀指定为javax.faces.resource.localePrefix
。因此,例如,messages.properties
的值为fr
,messages\u en.properties
的值为en
,等等@BalusC我必须在messages\u en.properties中放入javax.faces.resource.localePrefix=en
?为什么只有消息
<application>
<message-bundle>com.example.i18n.YourBundle</message-bundle>
</application>
javax.faces.resource.localePrefix=fr
<h:outputStylesheet library="default" name="css/rafa.css" />
<h:outputScript library="default" name="rafa.js" />