JSF框架簡介與實例

JSF 體系結構:
JSF 的主要優勢之一就是它既是 Java Web 應用程序的用戶界面標準又是嚴格遵循模型-視圖-控制器(MVC) 設計模式的框架。用戶界面代碼(視圖)與應用程序數據和邏輯(模型)的清晰分離使 JSF 應用程序更易於管理。爲了準備提供頁面對應用程序數據訪問的 JSF 上下文和防止對頁面未授權或不正確的訪問,所有與應用程序的用戶交互均由一個前端FacesServlet(控制器)來處理。
JSF 生命週期:
FacesServlet 充當用戶和 JSF 應用程序之間的紐帶。它在明確限定的 JSF 生命週期(規定了用戶請求之間的整個事件流)的範圍內工作。
1.   當JSF頁面上的一個事件發生時(比如:用戶單擊了一個按鈕),事件通知通過HTTP發往服務器。服務器端使用FacesServet這個特殊的Servlet處理該通知。
2.   FacesServlet一接收到用戶的請求就創建一個FacesContext對象(JSF上下文,它存放了應用程序的所有數據)。在處理過程中,主要修改的就是這個FaceContext對象。
3.   接着就是處理過程,處理器是一個叫作Lifecycle的對象。FacesServet把控制權轉交給Lifecycle對象。該對象分6個階段來處理FacesContext對象以生成響應,最後將響應發回客戶端。
Lifecycle對象處理JSP請求所需要的一系列動作稱爲請求處理生命週期。過程狀態圖如下:
由於請求處理生命週期裏的應用請求值、處理驗證、更新模型值和調用應用程序等階段都可以在當前的請求對應的FacesContext實例中添加事件,因此,JSF實現必須在這些階段後處理這些事件。
階段
說明
恢復視圖
爲選定的視圖找到或創建組件樹。
一旦用戶單擊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>


 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章