使用JSF做個國際化的登入介面:
1.建立App_zh_CN.properties,App_en_US.properties檔案;
2.建立UserBean:
UserBean
1package com.kay.bean;
2
3public class UserBean {
4
5 private String name;
6 private String pass;
7 public String getName() {
8 return name;
9 }
10 public void setName(String name) {
11 this.name = name;
12 }
13 public String getPass() {
14 return pass;
15 }
16 public void setPass(String pass) {
17 this.pass = pass;
18 }
19
20 public String login(){
21 return null;
22 }
23}
24
3.建立LocalBean:1package com.kay.bean;
2
3public class UserBean {
4
5 private String name;
6 private String pass;
7 public String getName() {
8 return name;
9 }
10 public void setName(String name) {
11 this.name = name;
12 }
13 public String getPass() {
14 return pass;
15 }
16 public void setPass(String pass) {
17 this.pass = pass;
18 }
19
20 public String login(){
21 return null;
22 }
23}
24
1package com.kay.bean;
2
3public class LocalBean {
4
5 private String local;
6
7 public String getLocal() {
8 return local;
9 }
10
11 public void setLocal(String local) {
12 this.local = local;
13 }
14}
4.配置faces-config.xml:2
3public class LocalBean {
4
5 private String local;
6
7 public String getLocal() {
8 return local;
9 }
10
11 public void setLocal(String local) {
12 this.local = local;
13 }
14}
faces-config.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
3
4<faces-config >
5 <navigation-rule>
6 <from-view-id>/login.jsp</from-view-id>
7 </navigation-rule>
8 <!-- 管理UserBean -->
9 <managed-bean>
10 <managed-bean-name>user</managed-bean-name>
11 <managed-bean-class>com.kay.bean.UserBean</managed-bean-class>
12 <managed-bean-scope>request</managed-bean-scope>
13 </managed-bean>
14 <!-- 管理LocalBean -->
15 <managed-bean>
16 <managed-bean-name>lang</managed-bean-name>
17 <managed-bean-class>com.kay.bean.LocalBean</managed-bean-class>
18 <managed-bean-scope>session</managed-bean-scope>
<!-- 在session中設定local--zh_CN鍵值對-->
19 <managed-property>
20 <property-name>local</property-name>
21 <value>zh_CN</value>
22 </managed-property>
23 </managed-bean>
24 <!-- 設定支援的語言 -->
25 <application>
26 <locale-config>
27 <supported-locale>zh</supported-locale>
28 <supported-locale>en_US</supported-locale>
29 </locale-config>
30 </application>
31</faces-config>
5.login.jsp1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
3
4<faces-config >
5 <navigation-rule>
6 <from-view-id>/login.jsp</from-view-id>
7 </navigation-rule>
8 <!-- 管理UserBean -->
9 <managed-bean>
10 <managed-bean-name>user</managed-bean-name>
11 <managed-bean-class>com.kay.bean.UserBean</managed-bean-class>
12 <managed-bean-scope>request</managed-bean-scope>
13 </managed-bean>
14 <!-- 管理LocalBean -->
15 <managed-bean>
16 <managed-bean-name>lang</managed-bean-name>
17 <managed-bean-class>com.kay.bean.LocalBean</managed-bean-class>
18 <managed-bean-scope>session</managed-bean-scope>
<!-- 在session中設定local--zh_CN鍵值對-->
19 <managed-property>
20 <property-name>local</property-name>
21 <value>zh_CN</value>
22 </managed-property>
23 </managed-bean>
24 <!-- 設定支援的語言 -->
25 <application>
26 <locale-config>
27 <supported-locale>zh</supported-locale>
28 <supported-locale>en_US</supported-locale>
29 </locale-config>
30 </application>
31</faces-config>
login.jsp
1<%@ page language="java" pageEncoding="UTF-8"%>
2<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
3<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
4
5<%
6String path = request.getContextPath();
7String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
8%>
9
10<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
11<html>
12<head>
13 <base href="<%=basePath%>">
14 <title>Login頁面</title>
15</head>
16
17<body>
18 <f:loadBundle basename="properties.App" var="z"/> //載入國際化檔案 sun推薦每個頁面載入一個資原始檔
本程式中資原始檔放在properties資料夾中
19 <f:view locale="#{lang.local}"> //在顯示內容中載入國際化地區
20 <h:form>
21 <h:outputText value="#{z.username}"/><h:inputText value="#{user.name}"/><br>
22 <h:outputText value="#{z.userpass}"/><h:inputText value="#{user.pass}"/><br>
23 <h:selectOneRadio value="#{lang.local}"> //選擇地區
<f:selectItem itemLabel="中文" itemValue="zh_CN"/>
25 <f:selectItem itemLabel="英文" itemValue="en_US"/>
26 </h:selectOneRadio>
27 <h:commandButton value="#{z.login}" action="#{user.login}"></h:commandButton>
28 </h:form>
29 </f:view>
30</body>
31</html>
1<%@ page language="java" pageEncoding="UTF-8"%>
2<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
3<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
4
5<%
6String path = request.getContextPath();
7String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
8%>
9
10<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
11<html>
12<head>
13 <base href="<%=basePath%>">
14 <title>Login頁面</title>
15</head>
16
17<body>
18 <f:loadBundle basename="properties.App" var="z"/> //載入國際化檔案 sun推薦每個頁面載入一個資原始檔
本程式中資原始檔放在properties資料夾中
19 <f:view locale="#{lang.local}"> //在顯示內容中載入國際化地區
20 <h:form>
21 <h:outputText value="#{z.username}"/><h:inputText value="#{user.name}"/><br>
22 <h:outputText value="#{z.userpass}"/><h:inputText value="#{user.pass}"/><br>
23 <h:selectOneRadio value="#{lang.local}"> //選擇地區
<f:selectItem itemLabel="中文" itemValue="zh_CN"/>
25 <f:selectItem itemLabel="英文" itemValue="en_US"/>
26 </h:selectOneRadio>
27 <h:commandButton value="#{z.login}" action="#{user.login}"></h:commandButton>
28 </h:form>
29 </f:view>
30</body>
31</html>