JSF2自定义组件编程系列 第八部分

 

 Ajax

 Ajax特性很简单,修改一下xhtml文件的commandButton代码如下:

    <h:commandButton id="clickButton" value="Click Me!" actionListener="#{inputBean.print}">

      <f:ajax execute="in" render="out" />

    </h:commandbutton>

    通过Ajax发送给Server的数据包含在id为"in"的inputText tag中,这里用execute标志要发送"in"的数据。render表示Ajax调用成功后,要重新渲染id为out的outputText tag。

 

  国际化

  我希望显示的信息能够支持两种语言:中文和英文。现在在src/main/java/com/freebird目录下添加language目录,然后添加两个文件messages_en.properties和messages_zh_CN.properties.然后在faces-config.xml文件中配置如下信息:

  <application>

    <locale-config>

      <default-locale>zh_CN</default-locale>

    </locale-config>

    <resource-bundle>

      <base-name>com.freebird.language.messages</base-name>

      <var>msg</var>

    </resource-bundle>

  </application>

 

  为了将properties文件打包到jar包的classes目录下,需要修改pom.xml文件,添加build如下:

  <build>

    <resources>

      <resource>

        <directory>src/main/java</directory>

        <includes>

          <include> **/*.properties</include>

        </includes>

      </resource>

      <resource>

        <directory>src/main/resources</directory>

        <includes>

          <include> **/*.xml</include>

 <include> **/*.xhtml</include>

        </includes>

      </resource>

    </resources>

  </build>

 

  好了现在修改一下xhtml文件的commandButton的value属性如下:

    <h:commandButton id="clickButton" value="#{msg.buttonTitle}" actionListener="#{inputBean.print}">

      <f:ajax execute="in" render="out" />

    </h:commandButton>

  JSF runtime会根据faces-config.xml文件中的配置查找classes/com/freebird/language目录下的messages_en或者messages_zh_CN文件,具体使用哪一个取决于使用jsfex的web应用程序的Local设置。如何在Web应用程序中实现国际化,修改Local请参考下面的文章:http://www.mkyong.com/jsf2/jsf-2-internationalization-example/

  先在messages_en.properties文件中添加一行:

buttonTitle=Click Me!

  然后应该在message_zh_CN.properties文件中添加相应的中文信息.注意,由于需要转码,不能直接写入中文,而是可以在临时文件中写入中文,然后通过一个工具转换。比如通常我会在messages_zh_CN文件中写入:buttonTitle=点击我.然后运行下面的命令:

native2ascii -encoding utf8 messages_zh_CN messages_zh_CN.properties

  为了方便起见,我在register.sh脚本中添加对应的命令,文件内容如下:

cd src/main/java/com/freebird/language/

native2ascii -encoding utf8 messages_zh_CN messages_zh_CN.properties

cd -

mvn clean install

mvn install:install-file -Dfile=target/jsfex-1.0.jar -DgroupId=com.freebird -DartifactId=jsfex -Dversion=1.0 -Dpackaging=jar

 

好了,部署成功。

未完,待续。下一节,将尝试着自定义JSF标准验证错误的信息。

 

转载于:https://www.cnblogs.com/believeit/archive/2011/01/09/2183545.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值