DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Liu has posted 1 posts at DZone. View Full User Profile

Hello

05.03.2010
| 867 views |
  • submit to reddit
        // 这是一个JSF Hello 项目.
1、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



2、faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>

<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>/hello.jsp</from-view-id>
    <navigation-case>
      <from-outcome>newFriend</from-outcome>
      <to-view-id>/howDoYouDo.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
      <from-outcome>oldFriend</from-outcome>
      <to-view-id>/howAreYou.jsp</to-view-id>
    </navigation-case>
  </navigation-rule>

  <managed-bean>
    <managed-bean-name>hello</managed-bean-name>
    <managed-bean-class>com.jsfabc.jsh.view.bean.HelloBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
  </managed-bean>
    
</faces-config>


3、hello.jsp
<%@page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
<head>
<title>Hello Application</title>
</head>
<body>
<f:view>
  <h:form id="helloForm">
    <h3>Please enter your name </h3>
    <table>
      <tr>
        <td>Name:</td>
        <td>
          <h:inputText id="userName" 
                       value="#{hello.userName}" required="true"/>
          <h:message for="userName" />
        </td>
      </tr>      
    </table>
    <p>
    <h:commandButton id="helloCommand" 
                     type="submit" 
                     value="Submit" 
                     action="#{hello.sayHelloAction}"/>
  </h:form>
  <h:messages globalOnly="true">
  </h:messages>
</f:view>
</body>
</html>

4、howAreYou.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 
<html>
    <f:view>
      <head>
          <title>A Simple JavaServer Faces Application</title>
       </head>
       <body>
          <h3>
             How are you,
             <h:outputText value="#{hello.userName}"/>!
          </h3>
       </body>
    </f:view>
</html>

5、howDoYouDo.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 
<html>
    <f:view>
      <head>
          <title>A Simple JavaServer Faces Application</title>
       </head>
       <body>
          <h3>
             How do you do,
             <h:outputText binding="#{hello.htmlOutputText}"/>
          </h3>
       </body>
    </f:view>
</html>

6、index.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<jsp:forward page="/hello.faces"/>

7、HelloBean.java

package com.jsfabc.jsh.view.bean;

import javax.el.ValueExpression;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;

public class HelloBean {
    //用于登录的用户名	
    private String userName;
    //用于显示New Friend的文本输出组件
    private HtmlOutputText htmlOutputText;

    //构造函数
    public HelloBean() {
    }

    // 用户名的取得与设置
    public String getUserName() {
    	 return userName; 
    }

    public void setUserName(String newValue) {
         this.userName = newValue; 
    }
    
    //输出组件的取得与设置
    public HtmlOutputText getHtmlOutputText(){
    	return htmlOutputText;
    }
    
    public void setHtmlOutputText(HtmlOutputText newValue){
    	this.htmlOutputText=newValue;
    }    

    public String sayHelloAction(){
    	if(userName.equalsIgnoreCase("stranger")){
    	   //获得应用程序实例,以便创建HtmlOutputText组件
    	   FacesContext context = FacesContext.getCurrentInstance();
    	   //声明一个输出文本组件
      	   HtmlOutputText output = new HtmlOutputText(); 
      	   //获得一个值表达式
           ValueExpression value = context.getApplication().getExpressionFactory()
                 .createValueExpression(context.getELContext(), 
                 "#{hello.htmlOutputText}", String.class);
           output.setValueExpression("value", value);
 
      	   //为HtmlOutputText组件设置值
      	   output.setValue("New Friend!");
      	   //为HtmlOutputText组件设置CSS风格
      	   output.setStyle("color:red");
      	   //将创建的HtmlOutputText组件实例赋给Bean的属性
      	   this.htmlOutputText=output;
      	   //返回陌生人访问时的导航逻辑字符串
      	   return "newFriend";
    	}
    	else{
    	   //返回熟人访问时的导航逻辑字符串	
    	   return "oldFriend";
    	}
    }
}