jsf2入门demo

来源:互联网 发布:犀牛软件调整模型尺寸 编辑:程序博客网 时间:2024/05/18 02:36

环境的搭建参考博客中分类RichFaces下的“创建RichFaces工程”
参考:《JavaServer Faces核心编程(第3版)》
项目目录结构
这里写图片描述

index.xhtml

<!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:h="http://java.sun.com/jsf/html"      xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"      xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich"><h:head>        <title>Welcome</title></h:head><h:body>    <h:form>        <h3>please enter your name and password</h3>        <table>            <tr>                <td>Name:</td>                <td><h:inputText value="#{user.name}"></h:inputText></td>            </tr>            <tr>                <td>Password:</td>                <td><h:inputSecret value="#{user.password}"></h:inputSecret></td>            </tr>        </table>        <p><h:commandButton value="Login" action="welcome"></h:commandButton></p>    </h:form></h:body></html>

welcome.xhtml

<!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:h="http://java.sun.com/jsf/html"      xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"      xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich"><h:head>    <title>Welcome</title></h:head><h:body>    <h3>Welcome to JavaServer Faces.#{user.name}</h3></h:body></html>

User

import javax.faces.bean.ManagedBean;@ManagedBean(name="user")或者@Named("user")@SessionScopedpublic class User {    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;    }}

这里写图片描述
这里写图片描述

JSF2.0之前需要在WEB-INF/faces-config.xml文件中生命bean,对于一个简单应用程序其实不需要faces-config.xml配置文件

0 0
原创粉丝点击