第一个Jsf程序
2012-05-04 14:41
190 查看
1.新建工程,并导入Jsf需要的jars:
2.配置web.xml,将请求交由FacesServlet处理。
在<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-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:
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入门三(第一个JSF程序)
- JSF系列教程(二) 动手编写第一个JSF DEMO程序
- JSF---->环境搭建及第一个程序
- JSF 第一个程序
- JSF技术研究——第一个JSF程序
- JSF---->环境搭建及第一个程序
- 第一个JSF程序
- Ch01:第一个JSF程序
- Spring -- 第一个简单的Spring程序
- Android NDK学习教程(一)--第一个NDK程序
- 使用Eclipse编写第一个Hibernate程序
- tornado之运行第一个tornado程序
- MFC-01-Chapter01:Hello,MFC---1.3 第一个MFC程序(04)
- python 2.7 : 第一个Python程序
- Arduino可穿戴教程之第一个程序——Blink(一)
- Hibernate入门02 - 第一个Hibernate程序
- delphi7第一个程序helloword
- 第一个delphi for php的小程序
- 第一个jsf小实例
- ARToolKit入门教程——安装调试第一个程序