第一个Jsf程序

1.新建工程,并导入Jsf需要的jars:

* jsf-impl.jar 
* jsf-api.jar
* commons-digester.jar 
* commons-collections.jar 
* commons-beanutils.jar 
* jstl.jar 
* standard.jar

2.配置web.xml,将请求交由FacesServlet处理。

<?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>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <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>
在上面的定义中,我们将所有.faces的请求交由FaceServlet来处理,FaceServlet会唤起相对的.jsp网页,例如请求是/index.faces的话,则实际上会唤起/index.jsp网页,完成以上的配置,您就可以开始使用JSF了。
 
 
3.新建一个Bean,存储用户信息(name和password)
UserBean.java:
<!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:dc="http://purl.org/dc/elements/1.1/"
         xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"><rdf:description rdf:about="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=2628" dc:identifier="http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=2628" dc:title="简介JSF" trackback:ping="http://www.javaworld.com.tw/confluence/rpc/trackback/2628">--&gt;<!--
    Root decorator: all decisions about how a page is to be decorated via the
                    inline decoration begins here.
--><!--
    Switch based upon the context. However, for now, just delegate to a decorator
    identified directly by the context.
-->
package com.jsf.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;
import javax.faces.model.SelectItem;
import javax.swing.JOptionPane;

public class UserBean {
	private String name;
	private String password;
	private String errMsg;
	
	public UserBean() {
	}

	public String getPassword() {
		return this.password;
	}

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

	public String getErrMsg() {
		return this.errMsg;
	}

	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 *校验用户名和密码的方法。
	 */
	public String verify() {
		System.out.println("name:" + name + ",password:" + password);
		if (name.equals("admin") && password.equals("123456")) {
			return "success";
		} else {
			errMsg = "用户名或密码错误!";
			return "failure";
		}
	}
}
 
 
4.接下来设计页面流程,用户在index.jsp中输入用户名和密码,单击登陆按钮,通过UserBean的verify()方法检验,校验通过(返回success),则跳转到欢迎页面(welcome.jsp)
index.jsp:
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
 	<%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
 <%@page contentType="text/html;charset=gb18030"%>
<f:view>
	<html>
	<body>
	<h:outputText value=”#{user.errMsg}/>
	<h:form>
	<h3>请输入用户名和密码</h3>
	用户名:<h:inputText value=”#{user.name}”/><br>
	密码:<h:inputSecret value=”#{user.password}/><br>
	<h:commandButton value=”登陆” action=”#{user.verify}”/>
</h:form>
</body>
</html>
</f:view>
welcome.jsp:
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
 	<%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
 <%@page contentType="text/html;charset=gb18030"%>
<f:view>
<html>
<body>
欢迎<h:outputText value=”#{user.name}/>!
</body>
</html>
</f:view>
5.为了让JSF知道我们所设计的Bean以及页面流程,我们定义一个/WEB-INF/faces-config.xml:
 
 
 
 
 <!DOCTYPE faces-config PUBLIC
 "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
 "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

 <faces-config>
    <navigation-rule>
        <from-view-id>/pages/index.jsp</from-view-id>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/pages/welcome.jsp</to-view-id>
        </navigation-case>
         <navigation-case>
            <from-outcome>failure</from-outcome>
            <to-view-id>/pages/index.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

    <managed-bean>
        <managed-bean-name>user</managed-bean-name>
         <managed-bean-class>
             com.jsf.test.UserBean
         </managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
 </faces-config>
 在<navigation-rule>中,我们定义了页面流程,当请求来自<from-view- id>中指定的页面,并且指定了<navigation-case>中的<from-outcome>为success(即校验通过)时,则会将请求导向至<to-view-id>所指定的页面welcome.jsp,当<navigation-case>中的<from-outcome>为failure(即校验失败)时,则跳转回index.jsp。

在<managed-bean>中我们可以统一管理我们的Bean,我们设定Bean对象的存活范围是session。

 

6.接下来,将应用部署到tomcat,启动tomcat,并访问http://localhost:8080/工程名/pages/index.faces就可以了。


 

你可能感兴趣的:(JSF)