jsf之登陆

一环境搭建:
1,创建WebProject
2,添加jsf jar包:jsf-api.jar  jsf-impl.jar
3,配置web.xml文件  即可


知识点:
1,xhtml中#表示延迟取值:什么时候用什么时候计算值
   jsp中用$表示及时取值:立马计算并赋值


二实例登陆
1,web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
       <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
       <param-value>.xhtml</param-value>
  </context-param>
  <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>*.jsf</url-pattern>
  </servlet-mapping>
</web-app>


2,登陆login.xhtml,注意该页面不能直接请求(只能从别的jsp也跳转)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core" xml:lang="en" lang="en">
	<h:head>
	   <title>Login</title>
	</h:head>
	<h:body>
	   <h:form>
	      <h3>Please enter your username and password.</h3>
	      <table>
               <tr>
                   <td>userName:</td>
                   <td><h:inputText value="#{user.name}"/></td>
               </tr>
               <tr>
               <td>Password:</td>
                   <td><h:inputText value="#{user.password}"/></td>
               </tr>	      
	      </table>
	      <p><h:commandButton value="Login" action="#{user.login}"/></p>
	   </h:form>
	</h:body>
</html>


3,与前台绑定的类(包括绑定的属性和方法) UserBean .java
package com.jsf.bean;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.inject.Named;
@Named("user")
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable {
    private String name;
    private String password;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String login()
	{
		if("chenchaoyang".equals(getName()) && "chenchaoyang".equals(getPassword())){
			return "success";
		}
		return "fail";
	}
}


4,登陆成功页面 success.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core" xml:lang="en" lang="en">
	<h:head>
	   <title>Welcome</title>
	</h:head>
	<h:body>
	     ${user.name} 登陆成功<br/>
	     下面的信息是从国际化资源中获得的:<br/>
	     <h:outputFormat value="#{msgs.success}">
	         <f:param value="#{user.name}"></f:param>
	     </h:outputFormat>
	</h:body>
</html>


5,登陆失败页面fail.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core" xml:lang="en" lang="en">
	<h:head>
	   <title>Welcome</title>
	</h:head>
	<h:body>
	     ${user.name} 登陆失败<br/>
	     下面的信息是从国际化资源中取出来的:
	     <h:outputFormat value="#{msgs.fail}">
	         <f:param value="#{user.name}"></f:param>
	     </h:outputFormat>
	</h:body>
</html>




你可能感兴趣的:(JSF)