分类 标签 存档 ME! 黑客派 订阅 搜索

JSF 2.0 初学 Demo

135 浏览

目录结构:


Root


--Web Pages


        --login.xhtml


        --index.html


        --error.html


        --WEB-INF


                  --web.xml


--sources Packages


        --org


                  --b3log


                            --application


                                      --Login.java


                            --entity


                                      --User.java


pom.xml


 



  • 去除 XML 配置



          JSF2.0 省去了大量的陪值,主要原因是纳入了 Facelets。省去了faces-config.xml 中的大量配置。




    <managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>
com.corejsf.UserBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean> 




  • 导航配置的简化



不需要


<navigation-rule>
<navigation-case>
<from-view-id>/pages/login.xhtml</from-view-id>
<outcome>places</outcome>
<to-view-id>/pages/places.xhtml</to-view-id>
</navigation-case>
</navigation-rule>















这样复杂的导航规则,只需要通过约定进行绝对路径来进行导航便可。







例如







<h:form>
<h:commandLink action="login" value="Back"/>
</h:form>















就可以直接跳转到同一级目录下的login.xhtml页面。







也可以根据java 应用代码中返回的String来进行页面的跳转。







例如







public String login(final String password) {

if (password.equals("Vanessa")) {

return "index";

}

return "error";

}










  • EL 表达式的加强



以往 JSF 的表达式不支持参数的传递,现在我们可以通过配置相关的配置文件来实现


1. 通过 Maven 获取需要的依赖


    <dependencies>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>2.1.2-b05</version>
</dependency>
<dependency>
<groupId>org.jboss.el</groupId>
<artifactId>jboss-el</artifactId>
<version>2.0.1.GA</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>sun</id>
<url>http://download.java.net/maven/2/;
</repository>
<repository>
<id>java.net</id>
<url>http://download.java.net/maven/2;
</repository>
<repository>
<id>jboss.org</id>
<url>http://repository.jboss.org/maven2;
</repository>
</repositories>



 


2. 在 web.xml 中配置一个 expressionFactory




   <context-param>

<param-name>com.sun.faces.expressionFactory</param-name>

<param-value>org.jboss.el.ExpressionFactoryImpl</param-value>

</context-param>










3. 就可以使用 EL 表达式传入一个对象


eg:



 


 


应用解说


用户输入用户名和密码,进行登录


    1. 登录成功 -- 密码为 “Vanessa”,进入 index.xhtml 页面,显示:Welcome,#{user.userName}!


    2. 登录失败 -- 密码不为 “Vanessa”,进入 error.xhtml 页面,提示错误原因,点击 Back 后返回 index.xhtml


 


 


部分代码


        --login.xhtml


<?xml version='1.0' encoding='UTF-8' ?>
<!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">;
<head>
<title>Welcome Login</title>
</head>
<body>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel for="userName" value="User Name"/>
<h:inputText id="userName" value="#{user.userName}"/>
<h:outputLabel for="password" value="Password"/>
<h:inputSecret id="password" value="#{user.password}"/>
<h:outputLabel value=""/>
<h:commandButton value="Login" action="#{login.login(user.password)}"/>
</h:panelGrid>
</h:form>
</body>
</html>



        --index.html


<!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">;
<head>
<title>Index</title>
</head>
<body>
Welcome, #{user.userName}!
</body>
</html>



        --error.html


<!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">;
<head>
<title>Error</title>
</head>
<body>
The Password is Error!
<h:form>
<h:commandLink action="login" value="Back"/>
</h:form>
</body>
</html>



        --Login.java


/
  To change this template, choose Tools | Templates
and open the template in the editor. /
package org.b3log.application.jsf;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.b3log.entity.User;
/
@author Vanessa
*/
@ManagedBean(name ="login")
@SessionScoped
public class Login {
public String login(final String password) {
if (password.equals("Vanessa")) {
return "index";
}
return "error";
}
}



        --User.java


package org.b3log.entity;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/

This class depicts a user of B3log. This class only describes user's information basically, such as user name, email address, etc. The security
information(i.e. user password, user roles, etc) is depicted in class <code>UserAccount</code>.
@author <a href="mailto:lly219@gmail.com" mce_href="mailto:lly219@gmail.com">Liyuan Li</a>
@version 1.0.0.0, Aug 4, 2009 @see UserAccount
*/
@ManagedBean(name = "user")
@SessionScoped
public class User implements Serializable {
private String userName;
private String email;
private String password;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}



 


 


欢迎注册黑客派社区,开启你的博客之旅。让学习和分享成为一种习惯!

评论  
留下你的脚步
推荐阅读