您的位置:首页 > Web前端 > JavaScript

jsf2入门demo

2016-07-28 18:50 453 查看
环境的搭建参考博客中分类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")
@SessionScoped
public 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配置文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: