- -Create jsf web page support multi language interface
- -Using Face component Event
- -Setting current Locale and Global Locale
Hints:
- Create JSF web site
- Create 03 jsp page and configure navigation rule for them from properties file support two language Vietnam and English
- 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
[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
Right click o guiMessage file select open and insert some key as figure 03
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;quot;vi&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.jsp with two language
Add Add.jsp page
[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]
Chào bạn, bạn có thẻ share mình code bài trên được ko?
ReplyDeleteemail truongtungduong9x@gmail.com
cmar ơn nhiều
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