附件
清单
web-inf/lib中
commons-beanutils.jar
commons-digester.jar
commons-logging.jar
jsf-api.jar
jsf-ri.jar
jstl.jar
standard.jar
html_basic.tld
web-facesconfig_1_0.dtd
faces-config.xml
<?xml version="1.0"?>
<faces-config>
<navigation-rule>
<from-tree-id>/counter.jsp</from-tree-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-tree-id>/counter.jsp</to-tree-id>
</navigation-case>
</navigation-rule>
</faces-config>
web.xml
<?xml version="1.0"?>
<web-app>
<description>
JavaServer Faces Guess Number Sample Application
</description>
<display-name>JavaServer Faces Guess Number Sample Application</display-name>
<context-param>
<param-name>saveStateInClient</param-name>
<param-value>false</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
src中
com.jspdev.ch19下有两java
CounterActionListener.java
package com.jspdev.ch19;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.event.PhaseId;
import javax.faces.tree.Tree;
import java.util.Iterator;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
public class CounterActionListener implements ActionListener {
public PhaseId getPhaseId() {
debugOut("调用了:getPhaseId");
return PhaseId.APPLY_REQUEST_VALUES;
}
/**
*处理动作
*/
public void processAction(ActionEvent event) {
debugOut("调用了: processAction ");
// 激发动作事件的组件
UIComponent component = event.getComponent();
debugOut
("激发动作事件的组件id: " + component.getComponentId());
// 动作命令
String actionCommand = event.getActionCommand();
debugOut
("动作命令: " + actionCommand);
FacesContext facesContext =
FacesContext.getCurrentInstance();
Tree tree = facesContext.getTree();
UIComponent root = tree.getRoot();
debugOut
("----------- 组件树 -------------");
navigateComponentTree(root, 0);
debugOut
("----------------------------------------");
}
/**
*遍历组件树
*/
private void navigateComponentTree
(UIComponent component, int l) {
Iterator children = component.getChildren();
// indent
for (int i=0; i<l; i++)
System.out.print(" ");
// 打印组件的ID
debugOut(component.getComponentId());
// 遍历组件的Child。
while (children.hasNext()) {
UIComponent child =
(UIComponent) children.next();
navigateComponentTree(child, l + 1);
}
}
private void debugOut(String msg)
{
System.out.println("from CounterActionListener:"+msg);
}
}
CounterCharacterBean.java
package com.jspdev.ch19;
/**
*用于计算字符串长度的JavaBean
*/
public class CounterCharacterBean
{
private String target=null;
private Integer counter=null;
public CounterCharacterBean()
{
System.out.println("in CounterCharacterBean:");
}
public void setTarget(String target)
{
this.target=target;
}
public String getTarget()
{
return this.target;
}
/**
*获得计算的结果
*/
public Integer getCounter()
{
return new Integer(target.length());
}
}
counter.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=GB2312"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>jsf 统计字符串中字符数量的统计</title>
</head>
<body>
<jsp:useBean id="counterBean" class="com.jspdev.ch19.CounterCharacterBean"
scope="session" />
<f:use_faces><br />
<h:form id="counterForm" formName="counterForm" ><br />
输入字符:<br />
<h:input_text id="target" valueRef="counterBean.target" /><br />
这个字符的长度:
<h:output_number id="output" valueRef="counterBean.counter"/><br>
<h:command_button id="submitButton"
label="submit" commandName="submit" action="success">
<f:action_listener
type="com.jspdev.ch19.CounterActionListener"/>
</h:command_button>
</h:form>
</f:use_faces>
</body>
</html>