风人园

弱水三千,只取一瓢,便能解渴;佛法无边,奉行一法,便能得益。
随笔 - 99, 文章 - 181, 评论 - 56, 引用 - 0
数据加载中……

JSF-- 入门 First Example

一、JSF开发环境
   使用myeclipse的功能,添加相关lib到classpath。 或者从sun的官方网站下载相关lib。
http://java.sun.com/javaee/javaserverfaces/download.html
二、JSP页面
      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>
            
<h3> Welcome to JavaServer Faces,
                
<h:outputText value="#{user.name}" /> !
            
</h3>
        
</body>
    
</f:view>
</html>


三、Bean
      UserBean .java
   package com.test.jsf.bean;

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;
 }
 
 public String check() {
  name = "wxf";
  return "success";
  //return SUCCESS;
 }
}

四、faces-config
<?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 >
 <navigation-rule>
 
    <from-view-id>/login/index.jsp</from-view-id>
    <navigation-case>
       <from-outcome>login</from-outcome>
       <to-view-id>/login/welcome.jsp</to-view-id>
    </navigation-case>
 </navigation-rule>
 
 <managed-bean>
    <managed-bean-name>user</managed-bean-name>
    <managed-bean-class>
       com.test.jsf.bean.UserBean
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>

</faces-config>
五、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <context-param>
  <param-name>javax.faces.CONFIG_FILES</param-name>
  <param-value>/WEB-INF/faces-config.xml</param-value>
 </context-param>

 <servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>0</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.faces</url-pattern>
 </servlet-mapping>
</web-app>


六、测试

   http://localhost:8080/jsf/login/index.faces
注意,调用jsp页面时一定要使用faces,结尾,否则jsf的标签将无法识别

posted on 2007-04-26 12:56 风人园 阅读(599) 评论(0)  编辑  收藏 所属分类: JSF


只有注册用户登录后才能发表评论。


网站导航: