JSF开篇之Login案例
开发环境:Myeclipse+JDK5+MyEclipse Tomcat+jsf2.2.8
JSF看起来和STRUTS还是有些像的,刚开始还是遇到一点问题:资源包的存放路径及文件访问路径。
开发Login案例过程如下:
1、下载开发JSF所需的jar包,google:jsf download
2、在MyEclipse中创建J2EE工程,然后引入jar包。
3、配置web.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <!-- 设置配置文件路径 默认就是web-inf下 --> 9 <context-param> 10 <param-name>javax.faces.application.CONFIG_FILES</param-name> 11 <param-value>/WEB-INF/faces-config.xml</param-value> 12 </context-param> 13 14 <!-- 设置总控制器,接管所有请求 --> 15 <servlet> 16 <servlet-name>Faces Servlet</servlet-name> 17 <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 18 <load-on-startup>1</load-on-startup> 19 </servlet> 20 21 <servlet-mapping> 22 <servlet-name>Faces Servlet</servlet-name> 23 <!-- 以后从web访问文件路径都是 localhost:80/jsflogin/faces/jsps/login.jsp这样的格式,不然报错--> 24 <url-pattern>/faces/*</url-pattern> 25 </servlet-mapping> 26 27 <welcome-file-list> 28 <welcome-file>index.jsp</welcome-file> 29 </welcome-file-list> 30 </web-app>
4、编写受管bean LoginBean.java
1 package jsf.bean; 2 3 import javax.faces.application.FacesMessage; 4 import javax.faces.context.FacesContext; 5 6 public class LoginBean { 7 8 String username; 9 String password; 10 //构造方法一定是public 11 public LoginBean() { 12 super(); 13 // TODO Auto-generated constructor stub 14 } 15 /** 16 * @return the username 17 */ 18 public String getUsername() { 19 return username; 20 } 21 /** 22 * @param username the username to set 23 */ 24 public void setUsername(String username) { 25 this.username = username; 26 } 27 /** 28 * @return the password 29 */ 30 public String getPassword() { 31 return password; 32 } 33 /** 34 * @param password the password to set 35 */ 36 public void setPassword(String password) { 37 this.password = password; 38 } 39 40 public String login(){ 41 if(username.equals("admin")&&password.equals("admin")){ 42 43 return "success"; 44 }else{ 45 FacesContext ctx=FacesContext.getCurrentInstance(); 46 FacesMessage msg=new FacesMessage("Invalid Username and/or password,try again!"); 47 ctx.addMessage("loginForm", msg); 48 return "failure"; 49 } 50 } 51 52 }
5、编写配置文件faces-config.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE faces-config PUBLIC 3 "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN" 4 "http://java.sun.com/dtd/web-facesconfig_1_0.dtd"> 5 <faces-config> 6 <!-- 配置受管bean --> 7 <managed-bean> 8 <display-name>Login Bean Holder</display-name> 9 <managed-bean-name>LoginBean</managed-bean-name> 10 <managed-bean-class>jsf.bean.LoginBean</managed-bean-class> 11 <managed-bean-scope>session</managed-bean-scope> 12 </managed-bean> 13 <!-- 配置导航规则 --> 14 <navigation-rule> 15 <from-view-id>/jsps/login.jsp</from-view-id> 16 <navigation-case> 17 <from-outcome>success</from-outcome> 18 <to-view-id>/jsps/greeting.jsp</to-view-id> 19 </navigation-case> 20 <navigation-case> 21 <from-outcome>failure</from-outcome> 22 <to-view-id>/jsps/login.jsp</to-view-id> 23 </navigation-case> 24 </navigation-rule> 25 </faces-config>
6、编写资源离线包,国际化/本地化配置Message.properties,一定要放在src目录下(放在其它地方不知怎么配置)
1 greeting_text=Welcome 2 username_label=Username 3 password_label=Password 4 login_button=Login
7、编写页面文件 login.jsp,greeting.jsp
login.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!-- 引入jsf的标签 --> 3 <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 4 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> 5 <!-- 加载资源文件 --> 6 <f:loadBundle basename="bundle.Messages" var="Message"/> 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <title>系统登录</title> 11 </head> 12 <body> 13 <f:view> 14 <h:form id="loginForm"> 15 <h:message for="loginForm" /><br/> 16 <h:outputText value="#{Message.username_label}" /> 17 <h:inputText id="username" value="#{LoginBean.username}" required="true"> 18 <f:validateLength maximum="15" minimum="5" /> 19 </h:inputText> 20 <h:message for="username" /><br/> 21 <h:outputText value="#{Message.password_label}" /> 22 <h:inputText id="password" value="#{LoginBean.password}" required="true"> 23 <f:validateLength maximum="15" minimum="5" /> 24 </h:inputText> 25 <h:message for="password" /><br/> 26 <h:commandButton id="submit" value="#{Message.login_button}" action="#{LoginBean.login}"/> 27 </h:form> 28 </f:view> 29 </body> 30 </html>
greeting.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 3 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> 4 <f:loadBundle basename="bundle.Messages" var="Message"/> 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 6 <html> 7 <head> 8 <title>登录成功</title> 9 </head> 10 <body> 11 <f:view> 12 <h:outputText value="#{Message.greeting_text}" />, 13 <h:outputText value="#{LoginBean.username}" />! 14 </f:view> 15 </body> 16 </html>
8、开启Tomcat部署项目,访问http://localhost:80/jsflogin/faces/jsps/login.jsp
运行结果如下:
我的项目截图: