奉送我的JSF书稿中的一个实例:JSF实现的自选语言界面

版权声明:本文可以自由转载,转载时请务必标明作者信息及本声明
       作者:++yong
作者的Blog:http://blog.csdn.net/qjyong
问题描述:实现一个带自选语言栏的用户登录验证示例的国际化。对于这个实例分两部分来实现:先实现用户登录验证的国际化,再加上自选语言栏。
  
第一部分:实现用户登录验证
创建一个名为 I18N_demo JSF Web 项目。
1.         创建后台Bean
在项目中创建一个后台 Bean RegistrationBean.java
package org.qiujy.web.controller;
 
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
 
public class RegistrationBean {
    private String userName;
    private String password;
 
    // 以下是属性的 getter setter 方法
    ......
 
    public String validate() {
             boolean flag = true;
             if (!"test".equals(userName)) {
                       FacesMessage msg = MessageFactory.getMessage(FacesContext
                                         .getCurrentInstance(), "field_ISERROR",
                                         new Object[] { "userName" });
                       FacesContext.getCurrentInstance().addMessage(null, msg);
                       flag = false;
             }
             if (!"123456".equals(password)) {
                       FacesMessage msg = MessageFactory.getMessage(FacesContext
                                         .getCurrentInstance(), "field_ISERROR",
                                         new Object[] { "password" });
                       FacesContext.getCurrentInstance().addMessage(null, msg);
                       flag = false;
             }
 
             if (flag) {
                       return "success";
             } else {
                       return "failure";
             }
    }
}
这个 Bean 中提供跟页面绑定的属性,以及跟动作按钮绑定的动作处理方法 validate() ,在这个方法中需要注意的是,对用户名、密码都进行了相应的判断,如果是 test 123456 ,就是合法用户,返回结果字符串“ success ”,否则是非法用户,通过 JSF 提供的 MessageFactory 来获取并创建好一则本地化错误消息(消息“键”是“ field_ISERROR ”),添加到 FacesContext 中,然后返回结果字符串“ failure ”。这样到了失败页面就可以取出相应的经过本地化的错误消息。
2.         配置托管Bean和资源文件绑定
faces-config.xml 文件中把 RegistrationBean 配置成托管 Bean 。同时为了支持国际化,指定了错误消息文件和资源文件,它们是同一个文件,就是存放在应用的 org/qiujy/web/resources 目录下的 ApplicationMessages.properties 文件,稍后再来看这个文件的内容:
<faces-config>
<application>
        <message-bundle>
org.qiujy.web.resources.ApplicationMessages
</message-bundle>
        <locale-config>
                 <default-locale>zh_CN</default-locale>
                 <supported-locale>en</supported-locale>
                 <supported-locale>zh_TW</supported-locale>
        </locale-config>
       
        <resource-bundle>
                 <base-name>
org.qiujy.web.resources.ApplicationMessages
</base-name>
                 <var>bundle</var>
        </resource-bundle>
</application>
        
         <managed-bean>
                   <managed-bean-name>registrationBean</managed-bean-name>
                   <managed-bean-class>
                            org.qiujy.web.controller.RegistrationBean
                   </managed-bean-class>
                   <managed-bean-scope>request</managed-bean-scope>
         </managed-bean>
         ......
</faces-config>
3.         创建页面和本地化资源文件
用户登录页面: userlogin.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
 
<f:view>
         <html>
                   <head>
                            <title><h:outputText value="#{bundle.title_login}" /></title>
                   </head>
 
                   <body>
                            <h:form id="loginForm">
                                     <h:panelGrid columns="2">
                                               <h:graphicImage url="#{bundle.login_logo}"
                                                                                    width="220" height="160"/>
                                    
                                               <h:panelGrid columns="3">
                                                        <f:facet name="caption">
                                                                 <h:outputText value="#{bundle.title_login}" />
                                                        </f:facet>
        
                                                        <h:outputText value="#{bundle.login_userName}" />
                                                        <h:inputText id="textName"
                                                                 value="#{registrationBean.userName}"
                                                                 required="true">
                                                                          
                                                        </h:inputText>
                                                        <h:message for="textName" style="color:red" />
        
                                                        <h:outputText value="#{bundle.login_password}" />
                                                        <h:inputSecret id="textPwd"
                                                                 value="#{registrationBean.password}"
                                                                 required="true">
                                                                 <f:validateLength minimum="6" maximum="20"/>
                                                        </h:inputSecret>
                                                        <h:message for="textPwd" style="color:red" />
        
                                                        <f:facet name="footer">
                                                                 <h:panelGroup>
                                                                           <h:commandButton value="#{bundle.button_submit}"
                                                                                    action="#{registrationBean.validate}" />
                                                                           <h:outputText value=" "></h:outputText>
                                                                           <h:commandButton value="#{bundle.button_reset}"
                                                                                    type="reset" />
                                                                 </h:panelGroup>
                                                        </f:facet>
                                               </h:panelGrid>
                                     </h:panelGrid>
                            </h:form>
                   </body>
         </html>
</f:view>
在这个页面中,所有静态文本,错误消息都通过值表达式用资源文件的别名“ bundle ”来获取的。所有的资源消息“键”在本地化资源文件中都配置了相应的“值”,如下:
代码片段 7.15  缺省的资源文件 ApplicationMessages.properties
button_submit=Submit
button_reset=Reset
button_back=Back
 
title_login=User Login Page
login_userName=UserName:
login_password=Password:
login_logo=/images/jsf_i18n_en.gif
 
success_welcome=Welcome:
failure_error=Failure!
field_ISERROR= {0} is error.
         英文的资源文件 ApplicationMessages_en.properties 的内容跟这个相同。下面再来看简体中文的资源文件:
简体中文的资源文件 ApplicationMessages_zh_CN.properties
button_submit= 提交
button_reset= 重置
button_back= 后退
 
title_login= 用户登录页面
login_userName= 用户名 :
login_password= 密码 ::
login_logo=/images/jsf_i18n_zh_CN.gif
 
success_welcome= 欢迎 :
failure_error= 失败 !
field_ISERROR= {0} 不正确
         需要注意是,使用是别忘了进行 Uncodei 编码转换。至于繁体中文的资源文件也跟这个文件差不多,在此不再赘述。
         另外要对标准的错误消息进行国际化,可以把 SUN RI 实现中的错误消息全部复制到本地化资源文件中,对简体中文的资源进行汉化,由于内容较多,在这就不帖出代码了,具体可能看本例的源代码。
接下来看登录成功后的页面的代码: success.jsp
                
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<f:view>
         <html>
                   <head>
                            <title><h:outputText value="#{bundle.success_welcome}"/></title>
                   </head>
                   <body>
                            <h2>
                                     <h:outputText value="#{bundle.success_welcome}" />
                                     <h:outputText value="#{registrationBean.userName}" />
                            </h2>
                   <jsp:useBean id="currentDate" class="java.util.Date" scope="request"/>
                            <h:outputText value="#{currentDate}">
                                     <f:convertDateTime type="both"/>
                            </h:outputText>
                   </body>
         </html>
</f:view>
在这个页面中,为了演示日期时间的国际化,先创建了一个日期对象,然后用 Output 组件标签输出,并给这个标签注册了 DateTimeConverter ,这样就能实现日期时间的国际化了。
最后再来看登录失败页面的代码: failure.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
         <html>
                   <head>
                            <title><h:outputText value="#{bundle.failure_error}"/></title>
                   </head>
                   <body>
                            <h2><h:outputText value="#{bundle.failure_error}"/></h2>
                            <h:messages style="color:red"/><br/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值