我的第一个jsf的Sample

今天学了jsf,发现它入门真的很容易,而且写起来也很顺手,但是了解的不多,只是写了个小例子,所以也不好说太多,呵呵,总结一下吧。
先看到了一个文档,介绍jsf的,写的非常好。随后按照文档上的提示开始编代码。首先在官方网站上下载相关的jar包,可是没有jstl.jar和standard.jar,不知道为什么,也许是我下载的不对吧,后来在csdn上找到了(现在我也提供了这个资源,免费的)。
我手头上有eclipse3.0和tomcat5.0。新建了一个tomcat项目,导入相关的jar包,新建jsp页面,写入代码
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
 <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
保存是提示出错,很无奈,后来在百度上搜索,终于找到了高手的解释,原来需要tomcat6才可以,相应的也要换eclipse。无奈下,下载tomcat6,eclipse3.3,还有tomcat的插件。这回可好了,一切正常。下面是代码,关于登录的。
1、首先需要改写web.xml
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2.  <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  6.     version="2.4">
  7.     <description>
  8.         JSF Demo
  9.     </description>
  10.     <display-name>JSF Demo</display-name>
  11.     <servlet>
  12.         <servlet-name>Faces Servlet</servlet-name>
  13.         <servlet-class>
  14.             javax.faces.webapp.FacesServlet
  15.         </servlet-class>
  16.         <load-on-startup>1</load-on-startup>
  17.     </servlet>
  18.     <servlet-mapping>
  19.         <servlet-name>Faces Servlet</servlet-name>
  20.         <url-pattern>*.faces</url-pattern>
  21.     </servlet-mapping>
  22.     <welcome-file-list>
  23.         <welcome-file>index.html</welcome-file>
  24.     </welcome-file-list>
  25.  </web-app>
    第12行到18行指定了要使用的jsf的servlet类,20到23行指定了输入的url地址的后缀.faces,25到27行指定了开始页面。
2、编写javaBean,一个用户类:
  1. package com.cym.jsf;
  2. import com.cym.jsf.dao.*;
  3. public class UserBean {
  4.     private String name;
  5.     private String password;
  6.     private String errMessage;
  7.     public void setName(String name) {
  8.         this.name = name;
  9.     }
  10.     public String getName() {
  11.         return name;
  12.     }
  13.     public void setPassword(String password) {
  14.         this.password = password;
  15.     }
  16.     public String getPassword() {
  17.         return password;
  18.     }
  19.     public void setErrMessage(String errMessage) {
  20.         this.errMessage = errMessage;
  21.     }
  22.     public String getErrMessage() {
  23.         return errMessage;
  24.     }
  25.     public String verify() {
  26.         if (name == null || "".equals(name)) {
  27.             errMessage = "用户名不得为空";
  28.             return "failure";
  29.         }
  30.         if (password == null || "".equals(password)) {
  31.             errMessage = "密码不得为空";
  32.             return "failure";
  33.         }
  34.         DbConnection dc = new DbConnection();
  35.         boolean r = dc.login(name, password);
  36.         if (r) {
  37.             return "success";
  38.         } else {
  39.             errMessage = "用户名或密码错误";
  40.             return "failure";
  41.         }
  42.     }
  43. }
第44行是连接数据库进行验证的代码,具体的就不写了。
3、在WEB-INF目录下建立一个faces-config.xml文件,内容是:
  1. <?xml version="1.0"?>
  2.  <!DOCTYPE faces-config PUBLIC
  3.  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
  4.  "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
  5.  <faces-config>
  6.     <navigation-rule>
  7.         <from-view-id>/pages/index.jsp</from-view-id>
  8.         <navigation-case>
  9.             <from-outcome>success</from-outcome>
  10.             <to-view-id>/pages/welcome.jsp</to-view-id>
  11.         </navigation-case>
  12.         <navigation-case>
  13.             <from-outcome>failure</from-outcome>
  14.             <to-view-id>/pages/index.jsp</to-view-id>
  15.         </navigation-case>
  16.     </navigation-rule>
  17.     <managed-bean>
  18.         <managed-bean-name>user</managed-bean-name>
  19.          <managed-bean-class>
  20.              com.cym.jsf.UserBean
  21.          </managed-bean-class>
  22.         <managed-bean-scope>session</managed-bean-scope>
  23.     </managed-bean>
  24.  </faces-config>
第8行指定请求的url地址,当然可以是*,表示所有页面。
第10行和第11行表示如果提交的关键之是success则跳转到 /pages/welcome.jsp页面,第14行和15行也是类似。第21行到24行指定user代表UserBean类,其实就是类的映射。这样以后就可以使用user了,简单了很多,呵呵。第25行指定类的映射存在的生命周期,为session,还可以是application或none。
4、建立2个jsp文件
index.jsp:
  1.  <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
  2.  <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
  3.  <%@page contentType="text/html;charset=UTF-8"%>
  4.  <html>
  5.  <head>
  6.  <title>第一个JSF程序</title>
  7.  </head>
  8.  <body>
  9.     <f:view>
  10.     <h:messages layout="table" style="color:red"/>
  11.     
  12.         <h:form>
  13.             <h3>登录系统...</h3>
  14.             <h:outputText value="#{user.errMessage}"/><p>
  15.            用户名: <h:inputText value="#{user.name}"/><p>
  16.            密    码:
  17.     <h:inputSecret value="#{user.password}">
  18.     </h:inputSecret><p>
  19.             <h:commandButton value="登录" action="#{user.verify}"/>
  20.         </h:form>
  21.     </f:view>
  22.  </body>
  23.  </html>
这里使用了JSF Expression Language,之前在faces-config.xml配置文件中指定了user代表UserBean类,所以在页面上可以直接写user.属性,来指定和显示值,在按钮的action中还指定了要调用的方法。
5、简历welcome.jsp
  1. <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
  2.  <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
  3.  <%@page contentType="text/html;charset=UTF-8"%>
  4.  <html>
  5.  <head>
  6.  <title>第一个JSF程序</title>
  7.  </head>
  8.  <body>
  9.     <f:view>
  10.         <h:outputText value="#{user.name}"/> 您好!
  11.         <h3>欢迎使用 JavaServer Faces!</h3>
  12.     </f:view>
  13.  </body>
  14.  </html>
这里就很容易理解了,最后在地址栏里输入http://localhost:8080/jsf_test/pages/index.faces就可以了














  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值