階段
|
說明
|
恢復視圖
|
爲選定的視圖找到或創建組件樹。
一旦用戶單擊JSP頁面上的鏈接或按鈕,就會啓動此階段。JSF應用裏的JSP頁面被表示成一個組件樹。JSF實現會進一步將這些組件鏈接到事件處理程序和驗證程序,並將視圖保存在FacesContext對象中,以備後面的處理過程所用。FacesContext對象包含了JSF用來管理當前會話中當前請求的GUI組件狀態所需要的所有狀態信息。
|
應用請求值
|
使用請求中發送來的值來更新組件樹的組件值。因爲請求中發送來的值都是String類型的,所以在更新組件樹的組件值之前,必須將這些值轉換爲相應類型。這個過程也是解碼。若轉換有錯誤,這些錯誤將添加到FacesContext對象。
|
處理驗證
|
當每個組件的本地值被更新後,Lifecycle對象都會根據這些註冊組件的驗證規則來驗證這些值的合法性。
如果輸入的值不符合驗證規則,就會將驗證錯誤添加至FacesContext對象,並將組件標記爲無效。JSF將轉至呈現響應階段,並顯示帶有驗證錯誤消息的視圖。
如果沒有遇到驗證錯誤,JSF將進入下一階段。
|
更新模型值
|
更新與組件相關的後臺bean(也叫管理bean)或者模型對象的值。只有那些與組件值綁定在一起的Bean屬性纔會被更新。
|
調用應用程序
|
JSF控制器調用應用程序來處理應用程序級的事件,如提交一個表單。(此階段可執行業務邏輯)
|
呈現響應
|
使用當前的顯示技術(如JSP)顯示選定的視圖。
|
一個jsf的實例
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd">
<!-- 可指定多個faces-config.xml,多個xml文件用逗號隔開(/WEB-INF/faces.xml,/WEB-INF/other.xml) -->
<!-- <context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</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>1</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>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
faces-config.xml文件如果Web.xml上沒配置路徑,默認放在/WEB-INF下面
<?xml version='1.0' encoding='UTF-8'?>
<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_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>loginBean</managed-bean-name>
<managed-bean-class>
com.zeng.controller.LoginBean
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>username</property-name>
<property-class>java.lang.String</property-class>
<value></value>
</managed-property>
<managed-property>
<property-name>password</property-name>
<property-class>java.lang.String</property-class>
<value></value>
</managed-property>
</managed-bean>
<navigation-rule>
<from-view-id>/jsp/login.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/jsp/success.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>fail</from-outcome>
<to-view-id>/jsp/fail.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
JSP頁面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ include file="/common/common.jsp"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=webRoot%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
</head>
<body> <f:view> <br> <h:form> <h:panelGrid columns="3"> <h:outputLabel for="username" value="User Name:" /> <h:inputText id="username" value="#{loginBean.username}" required="true" /> <h:message for="username" /> <h:outputLabel for="password" value="Password:" /> <h:inputSecret id="password" value="#{loginBean.password}" required="true" /> <h:message for="password" /> </h:panelGrid> <h:panelGrid> <h:panelGroup> <h:commandButton value="Login" action="#{loginBean.login}" /> </h:panelGroup> </h:panelGrid> </h:form> </f:view> </body> </html>
對應javaBean
package com.zeng.controller;
/** * @author sun1 * */ public final class LoginBean extends Object {
/** * */ private String password; /** * */ private String username; /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /** * @return the username */ public String getUsername() { return username; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; }
public String login() {
if ((username == null) || (username.length() < 1))
return "fail";
if ((password == null) || (password.length() < 1))
return "fail";
if ((username.equals("zeng")) && (password.equals("123")))
return "success";
else
return "fail";
}
}
啓動服務http://localhost:8080/NativeJavaWeb/jsp/login.faces輸入用戶名密碼便可看到效果
參考http://www.doc88.com/p-184601543386.html
http://wenku.baidu.com/view/2c3e2cbec77da26925c5b0fe.html
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/common/common.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=webRoot%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<f:view>
<br>
<h:form>
<h:panelGrid columns="3">
<h:outputLabel for="username" value="User Name:" />
<h:inputText id="username" value="#{loginBean.username}"
required="true" />
<h:message for="username" />
<h:outputLabel for="password" value="Password:" />
<h:inputSecret id="password" value="#{loginBean.password}"
required="true" />
<h:message for="password" />
</h:panelGrid>
<h:panelGrid>
<h:panelGroup>
<h:commandButton value="Login" action="#{loginBean.login}" />
</h:panelGroup>
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>