JSF案例

阶段
说明
恢复视图
为选定的视图找到或创建组件树。
一旦用户单击JSP页面上的链接或按钮,就会启动此阶段。JSF应用里的JSP页面被表示成一个组件树。JSF实现会进一步将这些组件链接到事件处理程序和验证程序,并将视图保存在FacesContext对象中,以备后面的处理过程所用。FacesContext对象包含了JSF用来管理当前会话中当前请求的GUI组件状态所需要的所有状态信息。
应用请求值
使用请求中发送来的值来更新组件树的组件值。因为请求中发送来的值都是String类型的,所以在更新组件树的组件值之前,必须将这些值转换为相应类型。这个过程也是解码。若转换有错误,这些错误将添加到FacesContext对象。
处理验证
当每个组件的本地值被更新后,Lifecycle对象都会根据这些注册组件的验证规则来验证这些值的合法性。
如果输入的值不符合验证规则,就会将验证错误添加至FacesContext对象,并将组件标记为无效。JSF将转至呈现响应阶段,并显示带有验证错误消息的视图。
如果没有遇到验证错误,JSF将进入下一阶段。
更新模型值
更新与组件相关的后台bean(也叫管理bean)或者模型对象的值。只有那些与组件值绑定在一起的Bean属性才会被更新。
调用应用程序
JSF控制器调用应用程序来处理应用程序级的事件,如提交一个表单。(此阶段可执行业务逻辑)
呈现响应
使用当前的显示技术(如JSP)显示选定的视图。

 

JSF是一种模拟事件驱动的处理方式

 

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.jim.login.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.faces</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>fail</from-outcome>
<to-view-id>/fail.jsp</to-view-id>
</navigation-case>
</navigation-rule>

</faces-config>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>0</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>
</web-app>


login.jsp:

<%@ 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>

loginBean.java:

public  class LoginBean  {

private String password;

private String username;


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


public String getUsername() {
return username;
}


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("admin")) && (password.equals("admin")))
return "success";
else
return "fail";
}
}


success.jsp:

<%@ page language="java" import="java.util.*" 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 JSP 'success.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>

<h:outputText value="#{loginBean.username}"></h:outputText><br/>
<h:outputText value="#{loginBean.password}"></h:outputText>
</f:view>
</body>
</html>




 

posted @ 2012-03-19 14:57  一直在等  阅读(930)  评论(0编辑  收藏  举报