<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">JSF 体系结构:</span>
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实现必须在这些阶段后处理这些事件。
一个jsf的实例
/JSFLoginDemo/WebRoot/WEB-INF/faces-config.xml
<?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.qdu.sun.jsf.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>/login.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/success.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>fail</from-outcome>
<to-view-id>/fail.jsp</to-view-id>
</navigation-case></navigation-rule>
<navigation-rule>
<from-view-id>/success.jsp</from-view-id>
</navigation-rule>
<navigation-rule>
<from-view-id>/fail.jsp</from-view-id>
</navigation-rule></faces-config>
前端页面
/JSFLoginDemo/WebRoot/login.jsp
<pre name="code" class="javascript"><%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSF '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>
后台代码
/JSFLoginDemo/src/com/qdu/sun/jsf/LoginBean.java
/**
*
*/
package com.qdu.sun.jsf;
/**
* @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("孙更新")) && (password.equals("123")))
return "success";
else
return "fail";
}
}