JSF 入门教程

                eclipse3.2+JSF2.0第一课:初级编程

  第一步准备如下图的jar包:

 

其中选中jsf+spring.jar和spring.jar是用不上的!!大家可以到http://download.youkuaiyun.com/detail/corey_jk/5054869下载,解压之后就有这些包了。

第二步:WEB-INF/web.xml

<?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>Myjsf5</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>

第三步:WEB-INF/faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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>/faces/login.jsp</from-view-id>
		<navigation-case>
			<from-outcome>success</from-outcome>
			<to-view-id>/faces/success.jsp</to-view-id>
		</navigation-case>
		<navigation-case>
			<from-outcome>fail</from-outcome>
			<to-view-id>/faces/error.jsp</to-view-id>
		</navigation-case>
		
	</navigation-rule>
	<managed-bean>
		<managed-bean-name>user</managed-bean-name>
		<managed-bean-class>com.jsf.UserBean</managed-bean-class>
		<managed-bean-scope>session</managed-bean-scope>
	</managed-bean>
</faces-config>
第四步:实体UserBean.java
package com.jsf;

public class UserBean {

	public UserBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	public UserBean(String name, String passWorld) {
		super();
		this.name = name;
		this.passWorld = passWorld;
	}

	private String name;
	private String passWorld;

	public String getName() {
		return name;
	}

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

	public String getPassWorld() {
		return passWorld;
	}

	public void setPassWorld(String passWorld) {
		this.passWorld = passWorld;
	}

	public String login() {
		if ("core".equals(this.name) && "1111".equals(this.passWorld)) {
			return "success";
		} else {
			return "fail";
		}
	}
}
第五步:书写好jsp页面
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
    <%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
 <f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>登录页面</title>
</head>
<body>
	<h:form>
		userName:<h:inputText value="#{user.name }"/>
		passworld:<h:inputSecret value="#{user.passWorld }"/>
		<h:commandButton value="提交" type="submit" action="#{user.login }"/>
	</h:form>
</body>
</html>
</f:view>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>成功</title>
</head>
<body>
欢迎<h1><h:outputText value="#{user.name }"/></h1>
</body>
</html>
</f:view>
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
    <%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
    <f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>错误</title>
</head>
<body>
 您输入错误<h1><h:outputText value="#{user.name }"/></h1>
</body>
</html>
</f:view>
 
 
 

接着就发布项目,我用的是tomcat6.0
 
 
有图有真相:
 
很简单是吧?
如果还有不懂的请留言!



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值