Java server face (JSF) – trang đa ngôn ngữ


OBJECTIVES

  1. -Create jsf web page support multi language interface
  2. -Using Face component Event
  3. -Setting current Locale and Global Locale

Hints:

  1. Create JSF web site
  2. Create 03 jsp page and configure navigation rule for them from properties file support two language Vietnam and English
  3. Setting current Locale and Global Locale at runtime and design time

Create JSF website

  • Create 03 jsp file: index.jsp, add.jsp, list.jsp
  • Open Face-config.xml, design navigation rule as figure 01 and insert new code as code 01

page-flow

<application> 
 <view-handler>com.sun.facelets.FaceletViewHandler</view-handler> 
 <resource-bundle> 
 <var>bundle</var> 
 <base-name>code.guiMessage</base-name> 
 </resource-bundle> 
 <locale-config> 
 <default-locale>vi</default-locale> 
 </locale-config> 
</application>

Code 01: Register bundle file

Create properties file

  • Name: guiMessage in side package code
  • Add two Locale vi_VN and en_US
  • Rename file as figure 02

bundle

Right click o guiMessage file select open and insert some key as figure 03

properties

Create manage bean

  • Language in code package
  • Modified code as
package code;

import java.util.Locale;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

public class language {

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }
    String lang = &amp;amp;quot;vi&amp;amp;quot;;

    /**
     * Creates a new instance of language
     */
    public language() {
    }

    public void change(ValueChangeEvent event) {
        lang = event.getNewValue().toString();
        FacesContext.getCurrentInstance().getApplication().setDefaultLocale(new Locale(lang));
        FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(lang));
    }
}

This code allow we setting Local and Global Locale of websie

Open index.jsp

Modified as figure 03 (using EL language: after bundle. Using Ctrl+Space bar for virtual code)

index

Index.jsp with two language

index1

Add Add.jsp page

add

<%@page contentType="text/html"%> 
<%@page 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> 
 <h:form> 
 <table> 
 <tr> 
 <td align="center" colspan="2"> 
 <h1><h:outputText value="#{bundle.register}"/></h1> 
 <h4><h:commandLink value="#{bundle.home}" action="home"/></h4> 
 <h:messages layout="table"/> 
 </td> 
 </tr> 
 <tr> 
 <td> 
 <h:outputText> 
 <f:attribute name="value" value="#{bundle.id}"/> 
 </h:outputText> 
 </td> 
 <td> 
 <h:inputText value="#{Customer.customerID}"> 
 
 </h:inputText> 
 </td> 
 </tr> 
 <tr> 
 <td><h:outputText value="#{bundle.CompanyName}"/></td> 
 <td><h:inputText value="#{Customer.companyName}"/></td> 
 </tr> 
 <tr> 
 <td><h:outputText value="#{bundle.Address}"/></td> 
 <td><h:inputText value="#{Customer.address}"/></td> 
 </tr> 
 <tr> 
 <td></td> <td><h:commandButton value="#{bundle.register}" action="list" 
actionListener="#{Customer.AddNew}"/></td> 
 </tr> 
 </table> 
 </h:form> 
</f:view> 
Advertisements

2 thoughts on “Java server face (JSF) – trang đa ngôn ngữ

Phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s