论坛首页 Java企业应用论坛

一个简单的JSF入门Demo

浏览 4497 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (12) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-05-24  

应一个网友要求,需要发一个简单的JSF入门Demo给他,但是公司的网络管制非常严,不敢直接用邮件发送代码,所以放在这里,需要的可以随便取用。

首先,按如下结构创建一个dynamic web project,导入所需要的jar。

 

配置一下web.xml和faces-config.xml。

 

 

<!-- web.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>JSFDemo</display-name>
	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.faces</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>

 

 

<!-- faces-config.xml -->
<?xml version="1.0"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
	version="1.2">
	<navigation-rule>
		<from-view-id>/index.jsp</from-view-id>
		<navigation-case>
			<from-outcome>login</from-outcome>
			<to-view-id>/welcome.jsp</to-view-id>
		</navigation-case>
	</navigation-rule>

	<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>
</faces-config>
 

 可以看到,在上面的faces-config.xml里注册了一个manage bean:user。下面就是这个bean:

 

 

//com.corejsf.UserBean.java
package com.corejsf;

public class UserBean {
	private String name;
	private String password;

	// PROPERTY: name
	public String getName() {
		return name;
	}

	public void setName(String newValue) {
		name = newValue;
	}

	// PROPERTY: password
	public String getPassword() {
		return password;
	}

	public void setPassword(String newValue) {
		password = newValue;
	}
}
 

 几个简单的测试页面也是必须的。

 

<!-- index.html -->
<html>
   <head>
      <meta http-equiv="Refresh" content= "0; URL=index.faces"/>
      <title>Start Web Application</title>
   </head>
   <body>
      <p>Please wait for the web application to start.</p>
   </body>
</html>

 

 

 

<!-- index.jsp -->
<html>
   <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
   <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
   <f:view>
      <head>                  
         <title>A Simple JavaServer Faces Application</title>
      </head>
      <body>
         <h:form>
            <h3>Please enter your name and password.</h3>
            <table>
               <tr>
                  <td>Name:</td>
                  <td>
                     <h:inputText value="#{user.name}"/>
                  </td>
               </tr>             
               <tr>
                  <td>Password:</td>
                  <td>
                     <h:inputSecret value="#{user.password}"/>
                  </td>
               </tr>
            </table>
            <p>
               <h:commandButton value="Login" action="login"/>
            </p>
         </h:form>
      </body>
   </f:view>
</html>

 

 

<!-- welcome.jsp -->
<html>
   <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
   <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

   <f:view>
      <head>               
         <title>A Simple JavaServer Faces Application</title>
      </head>
      <body>
         <h:form>
            <h3>
               Welcome to JavaServer Faces, 
               <h:outputText value="#{user.name}"/>!
            </h3>
         </h:form>
      </body>      
   </f:view>
</html>
 

 上面说得应该已经很明白了,JSF的基础知识就不需要我在这里普及了,很多地方能找到足够的资料。

 

  • 大小: 15.6 KB
   发表时间:2011-08-22  
这就是《Core JavaServer Faces》这本书的代码
0 请登录后投票
   发表时间:2011-08-25  
minfirefox 写道
这就是《Core JavaServer Faces》这本书的代码



是呀,有些朋友可能没看过这书,发上来分享一下。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics