开发环境:Myeclipse+JDK5+MyEclipse Tomcat+jsf2.2.8

JSF看起来和STRUTS还是有些像的,刚开始还是遇到一点问题:资源包的存放路径及文件访问路径。

开发Login案例过程如下:

1、下载开发JSF所需的jar包,google:jsf download

2、在MyEclipse中创建J2EE工程,然后引入jar包。

3、配置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"> <!-- 设置配置文件路径 默认就是web-inf下 -->
<context-param>
<param-name>javax.faces.application.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>
<!-- 以后从web访问文件路径都是 localhost:80/jsflogin/faces/jsps/login.jsp这样的格式,不然报错-->
<url-pattern>/faces/*</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

4、编写受管bean LoginBean.java

 package jsf.bean;

 import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext; public class LoginBean { String username;
String password;
//构造方法一定是public
public LoginBean() {
super();
// TODO Auto-generated constructor stub
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
} public String login(){
if(username.equals("admin")&&password.equals("admin")){ return "success";
}else{
FacesContext ctx=FacesContext.getCurrentInstance();
FacesMessage msg=new FacesMessage("Invalid Username and/or password,try again!");
ctx.addMessage("loginForm", msg);
return "failure";
}
} }

5、编写配置文件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>
<!-- 配置受管bean -->
<managed-bean>
<display-name>Login Bean Holder</display-name>
<managed-bean-name>LoginBean</managed-bean-name>
<managed-bean-class>jsf.bean.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<!-- 配置导航规则 -->
<navigation-rule>
<from-view-id>/jsps/login.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/jsps/greeting.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/jsps/login.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>

6、编写资源离线包,国际化/本地化配置Message.properties,一定要放在src目录下(放在其它地方不知怎么配置)

 greeting_text=Welcome
username_label=Username
password_label=Password
login_button=Login

7、编写页面文件 login.jsp,greeting.jsp

login.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 引入jsf的标签 -->
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<!-- 加载资源文件 -->
<f:loadBundle basename="bundle.Messages" var="Message"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>系统登录</title>
</head>
<body>
<f:view>
<h:form id="loginForm">
<h:message for="loginForm" /><br/>
<h:outputText value="#{Message.username_label}" />
<h:inputText id="username" value="#{LoginBean.username}" required="true">
<f:validateLength maximum="15" minimum="5" />
</h:inputText>
<h:message for="username" /><br/>
<h:outputText value="#{Message.password_label}" />
<h:inputText id="password" value="#{LoginBean.password}" required="true">
<f:validateLength maximum="15" minimum="5" />
</h:inputText>
<h:message for="password" /><br/>
<h:commandButton id="submit" value="#{Message.login_button}" action="#{LoginBean.login}"/>
</h:form>
</f:view>
</body>
</html>

greeting.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="bundle.Messages" var="Message"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录成功</title>
</head>
<body>
<f:view>
<h:outputText value="#{Message.greeting_text}" />,
<h:outputText value="#{LoginBean.username}" />!
</f:view>
</body>
</html>

8、开启Tomcat部署项目,访问http://localhost:80/jsflogin/faces/jsps/login.jsp

运行结果如下:

我的项目截图: