日期:2014-05-16  浏览次数:20403 次

JSF小示例《Core JavaServer Faces 3rd Edition》代码
《Core JavaServer Faces 3rd Edition》中第四章的代码:
LocaleChanger.java
package com.corejsf;

import java.io.Serializable;
import java.util.Locale;
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class LocaleChanger implements Serializable {
   public String germanAction() {
      FacesContext context = FacesContext.getCurrentInstance();
      context.getViewRoot().setLocale(Locale.GERMAN);
      return null;
   }
   
   public String englishAction() {
      FacesContext context = FacesContext.getCurrentInstance();
      context.getViewRoot().setLocale(Locale.ENGLISH);
      return null;
   }   
   
   public String chineseAction() {
      FacesContext context = FacesContext.getCurrentInstance();
      context.getViewRoot().setLocale(Locale.CHINESE);
      return null;
   }
}


UserBean.java
package com.corejsf;

import java.io.Serializable;
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable {
   private String name;
   private String password;
   private String aboutYourself;
   
   public String getName() { return name; } 
   public void setName(String newValue) { name = newValue; }
   
   public String getPassword() { return password; }
   public void setPassword(String newValue) { password = newValue; }
   
   public String getAboutYourself() { return aboutYourself; }
   public void setAboutYourself(String newValue) { aboutYourself = newValue; }
}


index.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">
	<h:head>
		<title>#{msgs.indexWindowTitle}</title>
	</h:head>
	<h:body>

		<h:form>
			<h:commandLink action="#{localeChanger.germanAction}">
				<h:graphicImage library="images" name="de_flag.gif"
					style="border: 0px; margin-right: 1em;" />
			</h:commandLink>
			<h:commandLink action="#{localeChanger.englishAction}">
				<h:graphicImage library="images" name="en_flag.gif"
					style="border: 0px; margin-right: 1em;" />
			</h:commandLink>
			<h:commandLink action="#{localeChanger.chineseAction}">
				<h:graphicImage library="images" name="cn_flag.gif"
					style="border: 0px; margin-right: 1em;" />
			</h:commandLink>
			<p>
				<h:outputText value="#{msgs.indexPageTitle}"
					style="font-style: italic; font-size: 1.3em" />
			</p>
			<h:panelGrid columns="2">
            #{msgs.namePrompt}
            <h:inputText value="#{user.name}" />         
            #{msgs.passwordPrompt}
            <h:inputSecret value="#{user.password}" />         
            #{msgs.tellUsPrompt}
            <h:inputTextarea value="#{user.aboutYourself}" rows="5"
					cols="35" />
			</h:panelGrid>
			<h:commandButton value="#{msgs.submitPrompt}" action="success" />
		</h:form>
	</h:body>
</html>


thankYou.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
   <h:head>
      <title>#{msgs.thankYouWindowTitle}</title>
   </h:head>
   &