无法从 jsf-resource-library 找到或提供本地化资源

Unable to find or serve localized resource from jsf-resource-library

我正在尝试复制书中的示例,

结构可以从下面的附图中看出-

已更新

将使用该库的视图就像下面给出的一样简单-

<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>

@Named
@RequestScoped
public class LocaleBean {

    private String mylocale = "fr";
    // getters & setters
}

我正在使用 Glassfish 4.1.1 Application Server,内部使用 Mojarra 2.2.12

rendered 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&amp;v=1_01_2">
        </script><link type="text/css" rel="stylesheet" href="/glassfish-ee/javax.faces.resource/css/rafa.css.xhtml?ln=fr/default&amp;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">
                <!--&lt;h:graphicImage library="fr/default" name="img/rafa.png"/&gt; -->
                <img src="/glassfish-ee/javax.faces.resource/img/rafa.png.xhtml?ln=fr/default&amp;v=1_0" />
            </div>
    </body>
</html>

呈现的感兴趣的相对 url html-

http://localhost:8080/glassfish-ee/javax.faces.resource/rafa.js.xhtml?ln=fr/default&v=1_01_2
http://localhost:8080/glassfish-ee/javax.faces.resource/css/rafa.css.xhtml?ln=fr/default&v=1_0
http://localhost:8080/glassfish-ee/javax.faces.resource/img/rafa.png.xhtml?ln=fr/default&v=1_0

在控制台上,我得到这个-

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.

Google chrome 的控制台选项卡显示-

所以最后,我哪里错了?
请建议。


在得到下面建议的答案的帮助后,进行了更改,

有条目-

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)");

即使在我通过按所需的 command button 更改 locale 之后,我也看到弹出相同的警报。

为什么?还需要做什么?提前致谢。

资源本地化功能在 JSF specification. On the contrary to what you'd intuitively expect, the localized subfolders won't be matched against <f:view locale>. Instead, they will be matched against the <message-bundle> entry identified with the key ResourceHandler.LOCALE_PREFIX which has a value of javax.faces.resource.localePrefix 中的记录非常少。此外,您不需要在 library 属性中明确包含 #{view.locale}

为了使其正常工作,请确保您已在 faces-config.xml 中声明了 <message-bundle>

<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" />

我承认这可以做得更好。当未指定 <message-bundle> and/or 时 javax.faces.resource.localePrefix 不存在,那么它应该简单地回退到 UIViewRoot#getLocale()。尽管如此,这无疑是最不常用的 JSF 特性之一。坦率地说,直到今天我才尝试过它,我认为我永远不会在现实世界的应用程序中使用它。