JSF>环境搭建及第一个程序 小强斋

一、 环境搭建

新建web项目

右键项目-----MyEclipse---Add JSF Capabilities..

点击Finish后面,项目增加了/WEB-INF/faces-config.xml文件,并且web.xml如下。

Web应用程序的web.xml如下所示,使用JSF时,所有的请求都透过FacesServlet来处理:

<?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.CONFIG_FILES</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>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>

在上面的定义中,我们将所有.faces的请求交由FaceServlet来处理,FaceServlet会唤起相对的.jsp网页,例如请求是/index.faces的话,则实际上会唤起/index.jsp网页,完成以上的配置,您就可以开始使用JSF了。

说明:(如果自己拷贝jar包到WEB-INF/lib,有时候发布时候会出现错误,com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED! com.sun.faces.lifecycle.LifecycleFactoryImpl cannot be cast to javax.faces.lifecycle.LifecycleFactory是因为jar包重复,Java EE 5 Libraries已经包含jsf-impl.jar jsf-api.jar,jstl.jar了,删除项目中重复的jar包即可。)

二 第一个JSF程序

程序开发人员撰写一个简单的JavaBean:

package wsz.ncepu;

public class UserBean {
 private String name;
 private String password;
 private String errMessage;

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

 public String getName() {
  return name;
 }

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

 public String getPassword() {
  return password;
 }

 public void setErrMessage(String errMessage) {
  this.errMessage = errMessage;
 }

 public String getErrMessage() {
  return errMessage;
 }

 public String verify() {
  if (!name.equals("justin") || !password.equals("123456")) {
   errMessage = "名称或密码错误";
   return "failure";
  } else {
   return "success";
  }
 }
}

接下来设计页面流程,我们将先显示一个登入网页/pages/index.jsp,使用者填入名称并送出窗体,之后在/pages/welcome.jsp中显示Bean中的使用者名称与欢迎讯息。为了让JSF知道我们所设计的Bean以及页面流程,我们定义一个/WEB-INF/faces-config.xml:在<navigation-rule>中,我们定义了页面流程,:

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/javaeehttp://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
	version="1.2">

	<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>
			wsz.ncepu.UserBean
		</managed-bean-class>
		<managed-bean-scope>session</managed-bean-scope>
	</managed-bean>

</faces-config>

</faces-config>在<managed-bean>中我们可以统一管理我们的Bean,我们设定Bean对象的存活范围是session,也就是使用者开启浏览器与程序互动过程中都存活。接下来要告诉网页设计人员的信息是,他们可以使用的Bean名称,即<managed-bean-name>中设定的名称,以及上面所定义的页面流程。

网页设计人员

首先网页设计人员撰写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=utf-8"%>
 <html>
 <head>
 <title>第一个JSF程序</title>
 </head>
 <body>
    <f:view>
        <h:form>
            <h3>请输入您的名称</h3>
            <h:outputText value="#{user.errMessage}"/><p>
           名称: <h:inputText value="#{user.name}"/><p>
           密码: <h:inputSecret value="#{user.password}"/><p>
            <h:commandButton value="送出" action="#{user.verify}"/>
        </h:form>
    </f:view>
 </body>
 </html>

我们使用了JSF的core与html标签库,core是有关于UI组件的处理,而html则是有关于HTML的进阶标签。<f:view>与<html>有类似的作用,当您要开始使用JSF组件时,这些组件一定要在<f: view>与</f:view>之间,就如同使用HTML时,所有的标签一定要在<html>与< /html>之间。

  html卷标库中几乎都是与HTML卷标相关的进阶卷标,<h:form>会产生一个窗体,我们使用<h: inputText>来显示user这个Bean对象的name属性,而<h:commandButton>会产生一个提交按钮,我们在action属性中指定将根据之前定义的login页面流程中前往welcome.jsp页面。

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=utf-8"%>
 <html>
 <head>
 <title>第一个JSF程序</title>
 </head>
 <body>
    <f:view>
        <h:outputText value="#{user.name}"/> 您好!
        <h3>欢迎使用 JavaServer Faces!</h3>
    </f:view>
 </body>
 </html>

发布后访问http://localhost:8080/jsf-first/pages/index.faces,运行结果如下


 

 

原文地址:https://www.cnblogs.com/xiaoqiangzhaitai/p/5429569.html