今天学了jsf,发现它入门真的很容易,而且写起来也很顺手,但是了解的不多,只是写了个小例子,所以也不好说太多,呵呵,总结一下吧。
第12行到18行指定了要使用的jsf的servlet类,20到23行指定了输入的url地址的后缀.faces,25到27行指定了开始页面。
第44行是连接数据库进行验证的代码,具体的就不写了。
第8行指定请求的url地址,当然可以是*,表示所有页面。
这里使用了JSF Expression Language,之前在faces-config.xml配置文件中指定了user代表UserBean类,所以在页面上可以直接写user.属性,来指定和显示值,在按钮的action中还指定了要调用的方法。
这里就很容易理解了,最后在地址栏里输入http://localhost:8080/jsf_test/pages/index.faces就可以了
先看到了一个文档,介绍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
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
- version="2.4">
- <description>
- JSF Demo
- </description>
- <display-name>JSF Demo</display-name>
- <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>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app>
2、编写javaBean,一个用户类:
- package com.cym.jsf;
- import com.cym.jsf.dao.*;
- 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 == null || "".equals(name)) {
- errMessage = "用户名不得为空";
- return "failure";
- }
- if (password == null || "".equals(password)) {
- errMessage = "密码不得为空";
- return "failure";
- }
- DbConnection dc = new DbConnection();
- boolean r = dc.login(name, password);
- if (r) {
- return "success";
- } else {
- errMessage = "用户名或密码错误";
- return "failure";
- }
- }
- }
3、在WEB-INF目录下建立一个faces-config.xml文件,内容是:
- <?xml version="1.0"?>
- <!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.cym.jsf.UserBean
- </managed-bean-class>
- <managed-bean-scope>session</managed-bean-scope>
- </managed-bean>
- </faces-config>
第10行和第11行表示如果提交的关键之是success则跳转到
/pages/welcome.jsp页面,第14行和15行也是类似。第21行到24行指定user代表UserBean类,其实就是类的映射。这样以后就可以使用user了,简单了很多,呵呵。第25行指定类的映射存在的生命周期,为session,还可以是application或none。
4、建立2个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=UTF-8"%>
- <html>
- <head>
- <title>第一个JSF程序</title>
- </head>
- <body>
- <f:view>
- <h:messages layout="table" style="color:red"/>
- <h:form>
- <h3>登录系统...</h3>
- <h:outputText value="#{user.errMessage}"/><p>
- 用户名: <h:inputText value="#{user.name}"/><p>
- 密 码:
- <h:inputSecret value="#{user.password}">
- </h:inputSecret><p>
- <h:commandButton value="登录" action="#{user.verify}"/>
- </h:form>
- </f:view>
- </body>
- </html>
5、简历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>