一、web.xml
<display-name>Facelets Tutorial</display-name>
<description>Number Guess Game</description>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<!-- welcome file mapping -->
<welcome-file-list>
<welcome-file>faces/hello.xhtml</welcome-file>
</welcome-file-list>
二、添加jar包
三、页面
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view contentType="text/html"/>
<h:head>
<title>Hello World!</title>
</h:head>
<h:panelGrid columns="2" border="1">
<f:facet name="abc">
<h:outputText value="#{helloBean.name}"></h:outputText>
</f:facet>
<ui:repeat var="list" value="#{helloBean.lists }">
<h:outputText value="#{list.name}"></h:outputText>
</ui:repeat>
</h:panelGrid>
<h:body bgcolor="white">
<h2>My name is Duke. What is yours?</h2>
<h:form id="helloForm" >
<h:graphicImage id="waveImg" url="#{resource['wave.med.gif']}" />
<h:inputText id="username" value="#{helloBean.name}"/>
<h:commandButton id="submit" action="response" value="Submit"/>
</h:form>
</h:body>
</html>
四、helloBean
package helloworld;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {
private String name;
private List<HelloBean> lists;
public String getName() { return name;}
public void setName(String name) { this.name = name; }
public List<HelloBean> getLists() {
lists=new ArrayList<HelloBean>();
HelloBean h=new HelloBean();
h.setName("dong");
lists.add(h);
h.setName("dongdong");
lists.add(h);
return lists;
}
public void setLists(List<HelloBean> lists) {
this.lists = lists;
}
}