我参照的书是 javaserver faces in action 中文
本书入个门还行吧.
开发环境是myeclipse 5.5
全部代码如下.
bean:
public class HelloBean {
private int numControls;
private HtmlPanelGrid controlPanel;
public int getNumControls()
{
return numControls;
}
public void setNumControls(int numControls)
{
this.numControls = numControls;
}
public HtmlPanelGrid getControlPanel()
{
return controlPanel;
}
public void setControlPanel(HtmlPanelGrid controlPanel)
{
this.controlPanel = controlPanel;
}
public void addControls(ActionEvent actionEvent)
{
Application application = FacesContext.getCurrentInstance().getApplication();
List children = controlPanel.getChildren();
children.clear();
for (int count = 0; count < numControls; count++)
{
HtmlOutputText output = (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE);
output.setValue(" " + count + " ");
output.setStyle("color: blue");
children.add(output);
}
}
public String goodbye()
{
return "success";
}
}
页面 hello.jsp
<jsp:directive.page import="java.util.Random"/><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>
JSF in Action - Hello, world!
<% Random r = new Random();
int a = r.nextInt(20);
out.println(a);
%>
</title>
</head>
<f:view>
<body>
<h:form id="welcomeForm">
<h:outputText id="welcomeOutput"
value="Welcome to JavaServer Faces!"
style="font-family: Arial, sans-serif; font-size: 24;
color: green;"/>
<p>
<h:message id="errors" for="helloInput" style="color: red"/>
</p>
<p>
<h:outputLabel for="helloInput">
<h:outputText id="helloInputLabel" value="Enter number of controls to display:"/>
</h:outputLabel>
<h:inputText id="helloInput" value="#{helloBean.numControls}" required="true">
<f:validateLongRange minimum="1" maximum="500"/>
</h:inputText>
</p>
<p>
<h:panelGrid id="controlPanel"
binding="#{helloBean.controlPanel}"
columns="20" border="1" cellspacing="0"/>
</p>
<h:commandButton id="redisplayCommand" type="submit" value="Redisplay" actionListener="#{helloBean.addControls}"/>
<h:commandButton id="goodbyeCommand" type="submit" value="Goodbye" action="#{helloBean.goodbye}" immediate="true"/>
</h:form>
<blockquote>
<f:verbatim>
<c:import url="WEB-INF/web.xml"/>
</f:verbatim>
</blockquote>
</body>
</f:view>
</html>
goodbye.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<html>
<head>
<title>
JSF in Action - Hello, world!
</title>
</head>
<body>
<h:form id="goodbyeForm">
<p>
<h:outputText id="welcomeOutput" value="Goodbye!"
style="font-family: Arial, sans-serif; font-size: 24;
font-style: bold; color: green;"/>
</p>
<p>
<h:outputText id="helloBeanOutputLabel" value="Number of controls displayed:"/>
<h:outputText id="helloBeanOutput" value="#{helloBean.numControls}"/>
</p>
</h:form>
</body>
</html>
</f:view>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<managed-bean>
<description>The one and only HelloBean.</description>
<managed-bean-name>helloBean</managed-bean-name>
<managed-bean-class>org.jia.hello.HelloBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<description>Navigation from the hello page.</description>
<from-view-id>/hello.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/goodbye.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
web.xm
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
-->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
调试通过.