JSF

1.导入jar文件 从MyEclipse->lib文件夹中
JSF
自动生成web-info下生成配置文件myface-config.xml 并配置
<?xml version='1.0' encoding='UTF-8'?>

<faces-config 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-facesconfig_1_2.xsd"
	version="1.2">

	<managed-bean>
		<managed-bean-name>userBean</managed-bean-name>
		<managed-bean-class>com.test.backingbean.UserBean</managed-bean-class>
		<managed-bean-scope>request</managed-bean-scope>
	</managed-bean>

	<navigation-rule>
		<from-view-id>/login.jsp</from-view-id>

		<navigation-case>
			<from-outcome>success</from-outcome>
			<to-view-id>/success.jsp</to-view-id>
		</navigation-case>

		<navigation-case>
			<from-outcome>failure</from-outcome>
			<to-view-id>/login.jsp</to-view-id>
		</navigation-case>

	</navigation-rule>

</faces-config>

web.xml的自动配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	version="2.5"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>0</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.jsp</welcome-file>
	</welcome-file-list>
	
</web-app>

2.创建login.jsp 选择 default jsf template 模板
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
  
<body>
	<f:view>
		
		<h1>Login Page</h1>
		
		<b><h:outputText value="#{userBean.message}"></h:outputText></b>
		
		<h:form>
		
			<h:outputText value="username: "></h:outputText>
			<h:inputText value="#{userBean.username}"></h:inputText> <br>
			
			<h:outputText value="password: "></h:outputText>
			<h:inputSecret value="#{userBean.password}"></h:inputSecret> <br>
			
			<h:commandButton value="Login" action="#{userBean.validateUser}"></h:commandButton>
		
		</h:form>
		
	</f:view>
</body>
</html>


success.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
  
<body>
	<f:view>
		
		<h:outputText value="username: "></h:outputText>
		<h:outputText value="#{userBean.username}"></h:outputText> <br>
		
		<h:outputText value="password: "></h:outputText>
		<h:outputText value="#{userBean.password}"></h:outputText>
		
	</f:view>
</body>
</html>

3.在src 创建包com.test.backingbean 及UserBean.java(backingbean包的后缀名规范)
package com.test.backingbean;

public class UserBean
{
	private String username;
	
	private String password;
	
	private String message;

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

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

	public String getMessage()
	{
		return message;
	}

	public void setMessage(String message)
	{
		this.message = message;
	}
	
	public String validateUser()
	{
		if("hello".equals(username) && "world".equals(password))
		{
			return "success";
		}
		
		this.setMessage("username or password error!");
		
		return "failure";
	}
}

启动tomcat服务器 http://localhost:8080/test/login.faces 不是login.jsp (login.faces显示页面标签)

你可能感兴趣的:(java,jsp,xml,MyEclipse,JSF)