2016-06-08 130 views
2

我试图从一本书上复制一个例子服务本地化资源,无法找到或JSF的资源库

结构可以从已更新

连接如下─

图像可视化

enter image description here

因为这如下─

给出的库将用于视图所简单
<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 ServerMojarra 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> 

相对的兴趣在渲染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. 

控制台选项卡shows-

enter image description here

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


考虑答案的帮助后以下建议,以改变,

enter image description here

与入门

javax.faces.resource.localePrefix=fr 

访问页面的GET请求,警告由于在上面附加的结构中描述为selected1_2.js文件中的以下条目弹出。

alert("J'ai gagné Roland Garros ...(resources/fr/1_0/css/js/rafa.js/1_2.js)"); 

enter image description here

我看到相同的警报弹起,即使我已经通过按所需command button改变locale后。

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

回答

2

资源本地化功能在JSF specification中很少记录。与您直觉上预期的相反,本地化子文件夹将不会与<f:view locale>相匹配。相反,它们将与<message-bundle>条目相匹配,该条目使用密钥ResourceHandler.LOCALE_PREFIX确定,其值为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>和/或javax.faces.resource.localePrefix不存在时,则应该简单地回退到UIViewRoot#getLocale()。尽管如此,这绝对是最罕见的JSF特性之一。坦率地说,我从未尝试过,直到今天,我不认为我会在真实世界的应用程序中使用它。

+0

请参阅更新的部分。 –

+1

您需要在特定于语言环境的消息包中指定所需的资源语言环境前缀为'javax.faces.resource.localePrefix'。例如, 'messages.properties'的值为'fr','messages_en.properties'的值为'en'等。 – BalusC

+0

噢,我看到的情况就像资源绑定一样。我仍然是有史以来最愚蠢的学习者。对不起再次打扰你了。 –