1、新建Web 项目jsfDemo---项目全部完成后的情况如下图;
2、加入jsf支持;
3、如步骤一, 建立好Action;
package com.yenange.action;
public class UserAction {
private String uname;
private String upass;
public String doLogin() {
if (uname.equals("leaf") && upass.equals("leaf")) {
return "success";
}
return "fail";
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpass() {
return upass;
}
public void setUpass(String upass) {
this.upass = upass;
}
}
4、建立三个页面文件,success.jsp和fail.jsp只分别有一个简单的welcome和fail, index.jsp的代码如下:
[%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
[%@ taglib uri="http://java.sun.com/jsf/core" prefix="c" %>
[%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
[html>
[head>
[title>登录页[/title>
[/head>
[c:view>
[body>
[h:form>
用户名:[h:inputText value="#{user.uname}" >[/h:inputText>[br/>
密 码:[h:inputSecret value="#{user.upass}" >[/h:inputSecret>[br/>
[h:commandButton value="登录" action="#{user.doLogin}" >[/h:commandButton>
[/h:form>
[/body>
[/c:view>
[/html>
5、配置faces-config.xml;
[?xml version="1.0" encoding="UTF-8"?>
[!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd" >
[faces-config>
[!-- managedBean就是托管Bean,或者称为 被jsf容器管理的bean -->
[managed-bean>
[managed-bean-name>user[/managed-bean-name>
[managed-bean-class>
com.yenange.action.UserAction
[/managed-bean-class>
[managed-bean-scope>session[/managed-bean-scope>
[/managed-bean>
[!-- 配置jsf的导航规则navigation rules -->
[navigation-rule>
[navigation-case>
[from-outcome>success[/from-outcome>
[to-view-id>/success.jsp[/to-view-id>
[/navigation-case>
[/navigation-rule>
[navigation-rule>
[navigation-case>
[from-outcome>fail[/from-outcome>
[to-view-id>/fail.jsp[/to-view-id>
[/navigation-case>
[/navigation-rule>
[/faces-config>
6、运行。
还算比较简单,只是不知道要真正做一个大项目,是不是也很容易。