1
下面粘貼的代碼是一個簡單的JSF程序,帶有一個命令按鈕(在JSP中是一個JSF組件)的概念,點擊時應該顯示寫在託管bean中的消息,然後返回並在JSP中顯示消息。一個簡單的JSF執行
**perfectJSP.jsp**
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Page</title>
</head>
<body>
<f:view>
<h:form>
<h:commandButton value="Click" actionListener="#{Test.clicked}"></h:commandButton>
</h:form>
</f:view>
<h3> This brings us to the end of the program </h3>
</body>
</html>
**Test** (managed bean)
import javax.faces.event.*;
public class Test {
public void clicked(ActionEvent ae)
{
System.out.println("This is from the bean class");
}
}
**web.xml**
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>
org.apache.myfaces.webapp.MyFacesServlet</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>
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
**faces-config.xml**
<faces-config>
<managed-bean>
<managed-bean-name>Test</managed-bean-name>
<managed-bean-class>Test</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
JBoss是使用的應用程序服務器。無法理解爲什麼不生成輸出。