Jsf國際化

weixin_33816946發表於2007-03-21

使用JSF做個國際化的登入介面:
1.建立App_zh_CN.properties,App_en_US.properties檔案;
2.建立UserBean:

ContractedBlock.gifExpandedBlockStart.gifUserBean
 1None.gifpackage com.kay.bean;
 2None.gif
 3ExpandedBlockStart.gifContractedBlock.gifpublic class UserBean dot.gif{
 4InBlock.gif
 5InBlock.gif    private String name;
 6InBlock.gif    private String pass;
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    public String getName() dot.gif{
 8InBlock.gif        return name;
 9ExpandedSubBlockEnd.gif    }

10ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setName(String name) dot.gif{
11InBlock.gif        this.name = name;
12ExpandedSubBlockEnd.gif    }

13ExpandedSubBlockStart.gifContractedSubBlock.gif    public String getPass() dot.gif{
14InBlock.gif        return pass;
15ExpandedSubBlockEnd.gif    }

16ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setPass(String pass) dot.gif{
17InBlock.gif        this.pass = pass;
18ExpandedSubBlockEnd.gif    }

19InBlock.gif    
20ExpandedSubBlockStart.gifContractedSubBlock.gif    public String login()dot.gif{
21InBlock.gif        return null;
22ExpandedSubBlockEnd.gif    }

23ExpandedBlockEnd.gif}

24None.gif
3.建立LocalBean:
 1None.gifpackage com.kay.bean;
 2None.gif
 3ExpandedBlockStart.gifContractedBlock.gifpublic class LocalBean dot.gif{
 4InBlock.gif
 5InBlock.gif    private String local;
 6InBlock.gif
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    public String getLocal() dot.gif{
 8InBlock.gif        return local;
 9ExpandedSubBlockEnd.gif    }

10InBlock.gif
11ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setLocal(String local) dot.gif{
12InBlock.gif        this.local = local;
13ExpandedSubBlockEnd.gif    }

14ExpandedBlockEnd.gif}
4.配置faces-config.xml:
ContractedBlock.gifExpandedBlockStart.giffaces-config.xml
 1None.gif<?xml version="1.0" encoding="UTF-8"?>
 2None.gif<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
 3None.gif
 4None.gif<faces-config >
 5None.gif    <navigation-rule>
 6None.gif        <from-view-id>/login.jsp</from-view-id>
 7None.gif    </navigation-rule>
 8None.gif    <!-- 管理UserBean -->
 9None.gif    <managed-bean>
10None.gif        <managed-bean-name>user</managed-bean-name>
11None.gif        <managed-bean-class>com.kay.bean.UserBean</managed-bean-class>
12None.gif        <managed-bean-scope>request</managed-bean-scope>
13None.gif    </managed-bean>
14None.gif    <!-- 管理LocalBean -->
15None.gif    <managed-bean>
16None.gif        <managed-bean-name>lang</managed-bean-name>
17None.gif        <managed-bean-class>com.kay.bean.LocalBean</managed-bean-class>
18None.gif        <managed-bean-scope>session</managed-bean-scope>
               <!-- 在session中設定local--zh_CN鍵值對-->
19None.gif        <managed-property>
20None.gif            <property-name>local</property-name>
21None.gif            <value>zh_CN</value>
22None.gif        </managed-property>
23None.gif    </managed-bean>
24None.gif    <!-- 設定支援的語言 -->
25None.gif    <application>
26None.gif        <locale-config>
27None.gif            <supported-locale>zh</supported-locale>
28None.gif            <supported-locale>en_US</supported-locale>
29None.gif        </locale-config>
30None.gif    </application>
31None.gif</faces-config>
5.login.jsp
ContractedBlock.gifExpandedBlockStart.giflogin.jsp
 1ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ page language="java" pageEncoding="UTF-8"%>
 2ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
 3ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
 4None.gif
 5ExpandedBlockStart.gifContractedBlock.gif<%dot.gif
 6InBlock.gifString path = request.getContextPath();
 7ExpandedBlockEnd.gifString basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 8None.gif
%>
 9None.gif
10None.gif<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
11None.gif<html>
12None.gif<head>
13None.gif    <base href="<%=basePath%>">
14None.gif    <title>Login頁面</title>
15None.gif</head>
16None.gif  
17None.gif<body>
18None.gif    <f:loadBundle basename="properties.App" var="z"/> //載入國際化檔案 sun推薦每個頁面載入一個資原始檔 
                                                                                                              本程式中資原始檔放在properties資料夾中

19None.gif    <f:view locale="#{lang.local}">   //在顯示內容中載入國際化地區
20None.gif        <h:form>
21None.gif            <h:outputText value="#{z.username}"/><h:inputText value="#{user.name}"/><br>
22None.gif            <h:outputText value="#{z.userpass}"/><h:inputText value="#{user.pass}"/><br>
23None.gif            <h:selectOneRadio value="#{lang.local}">  //選擇地區
None.gif                <f:selectItem itemLabel="中文" itemValue="zh_CN"/>
25None.gif                <f:selectItem itemLabel="英文" itemValue="en_US"/>
26None.gif            </h:selectOneRadio>
27None.gif            <h:commandButton value="#{z.login}" action="#{user.login}"></h:commandButton>
28None.gif        </h:form>
29None.gif    </f:view>
30None.gif</body>
31None.gif</html>

相關文章