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

一个简单的jsf例子------JSF2学习笔记1

1.jar包? 如附件

2.Web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>jsf</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
	<context-param> 
		<param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
		<param-value>client</param-value> 
	</context-param>
	
	<context-param> 
		<param-name>javax.faces.PROJECT_STAGE</param-name> 
		<param-value>Development</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>

?

3.faces-config.xml

<faces-config xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"> 

	<navigation-rule>
		<from-view-id>/index.jsp</from-view-id>
		<navigation-case>
			<from-outcome>login</from-outcome>
			<to-view-id>/welcome.jsp</to-view-id>
		</navigation-case>
	</navigation-rule>
	
	<!-- 使用了注解, 所以这里没有ManagedBean -->
	
</faces-config>

?

4. Bean文件? UserBean.java

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="user")
@SessionScoped
public class UserBean {
	private String name;
	private String password;

	public String getName() {
		return name;
	}

	public void setName(String newValue) {
		name = newValue;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String newValue) {
		password = newValue;
	}
}

?

?

5. 测试jsp? index.jsp?? 访问时 是index.faces

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">

   <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
   <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
   <f:view>
      <head>                  
         <title>Index </title>
      </head>
      <body>
         <h:form>
            <h3>Please enter your name and password.</h3>
            <table>
               <tr>
                  <td>Name:</td>
                  <td>
                     <h:inputText value="#{user.name}"/>

                  </td>
               </tr>             
               <tr>
                  <td>Password:</td>
                  <td>
                     <h:inputSecret value="#{user.password}"/>
                  </td>
               </tr>
            </table>
            <p>
               <h:commandButton value="Login" action="login"/>
            </p>
         </h:form>
      </body>
   </f:view>
</html&