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
Hello
// 这是一个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";
}
}
}





