Thursday, October 31, 2013

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


[sourcecode language="xml"]
<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>
[/sourcecode]

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



[sourcecode language="java"]
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));
}
}
[/sourcecode]

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

[sourcecode language="html"]
<%@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>
[/sourcecode]

2 comments:

  1. Chào bạn, bạn có thẻ share mình code bài trên được ko?
    email truongtungduong9x@gmail.com
    cmar ơn nhiều

    ReplyDelete
  2. Mã nguồn trên bài viết đó bạn, đây là bài viết tôi cho sinh viên làm bài tập nên kg có lưu lại trên máy cá nhân.

    ReplyDelete

Translate