JSF入门例子

首先去 http://java.sun.com/javaee/javaserverfaces/download.html 下载jsf的标准类库,这是sun公司发布的一套类库!当然还有apache的myfaces等,暂时先用标准的类库去操作!

用myeclipse6.0去搭建一个web的工程

然后开始编写一个登录的例子

 

首先编写web.xml文件

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>JSF</display-name>
    <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>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
</web-app>
 

 

 

再编写jsf的配置文件 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>/index.jsp</from-view-id>
        <!-- 返回值控制跳转页面 -->
        <navigation-case>
             <from-outcome>success</from-outcome>
            <to-view-id>/welcome.jsp</to-view-id>
		</navigation-case>
        <navigation-case>
             <from-outcome>fail</from-outcome>
            <to-view-id>/index.jsp</to-view-id>
		</navigation-case>
    </navigation-rule>

    <managed-bean>
        <managed-bean-name>user</managed-bean-name>
         <managed-bean-class>
             com.vincent.jsf.demo.UserBean
         </managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
 </faces-config>

 

 

编写一个 UserBean 的类

 

package com.vincent.jsf.demo;

import javax.faces.event.ActionEvent;

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

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

    public String getName() {
        return name;
    }

	public String getPassword() {
		return password;
	}

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

	
	public String getOutCome() {
		return outCome;
	}

	public void setOutCome(String outCome) {
		this.outCome = outCome;
	}

	public String getErrMessage() {
		return errMessage;
	}

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

	public void loginIn(ActionEvent e){
		System.out.println(name);
		System.out.println(password);
		if(name != null && name.equals("vincent")){
			outCome = "success";
		}else{
			errMessage = "用户名错误";
			outCome = "fail";
		}
	}
}

 

 

做两个jsp文件 index.jsp 和 welcome.jsp

 

<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
 <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
 <html>
 <head>
 <title>第一个JSF程序</title>
 </head>
 <body>
    <f:view>
        <h:form>
            <h3>登录</h3>
            名称:<h:inputText value="#{user.name}"/><p>
            密码:<h:inputSecret value="#{user.password}"></h:inputSecret><p/>
            <h:commandButton value="送出" actionListener="#{user.loginIn}" action="#{user.getOutCome}"/>
            <font color="red"><h:outputText value="#{user.errMessage}"/></font><p>
        </h:form>
    </f:view>
 </body>
 </html>

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

 

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
 <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
 <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/index.jsf

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值