jsf运行源代码 (MyEclipse6.5环境)

jsf的简单演示
摘自百度文库 搜 jsf入门 就能找到原文 但愿一直都在.上面有详细解释...
时间匆忙,我先记下我运行起来的jsf...

1 加载相关架包.这里我是通过MyEclipse自带的jsf加载的.

2 修改web.xml文件 最好不要全部覆盖,将web-app里面的部分拷入替换原来内容.
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
	<display-name>hello world</display-name>
	<description>Welcome to JavaServer Faces.</description>
	<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>faces/hello.jsp</welcome-file>
	</welcome-file-list>
</web-app>


3 facse-config.xml与web.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">
	<managed-bean>
		<description>The one and only HelloBean.</description>
		<managed-bean-name>helloBean</managed-bean-name>
		<managed-bean-class>org.jia.hello.HelloBean</managed-bean-class>
		<managed-bean-scope>session</managed-bean-scope>
	</managed-bean>
	<navigation-rule>
		<description>Navigation from the hello page.</description>
		<from-view-id>/hello.jsp</from-view-id>
		<navigation-case>
			<from-outcome>success</from-outcome>
			<to-view-id>/goodbye.jsp</to-view-id>
		</navigation-case>
	</navigation-rule>
</faces-config>


4 后台的java文件如下:
package org.jia.hello;

import javax.faces.application.Application;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import java.util.List;

public class HelloBean {
	private int numControls;
	private HtmlPanelGrid controlPanel;

	public int getNumControls() {
		return numControls;
	}

	public void setNumControls(int numControls) {
		this.numControls = numControls;
	}

	public HtmlPanelGrid getControlPanel() {
		return controlPanel;
	}

	public void setControlPanel(HtmlPanelGrid controlPanel) {
		this.controlPanel = controlPanel;
	}

	public void addControls(ActionEvent actionEvent) {
		Application application = FacesContext.getCurrentInstance().getApplication();
		List children = controlPanel.getChildren();
		System.out.println("children==="+children);
		children.clear();
		for (int count = 1; count <= numControls; count++) {
			HtmlOutputText output = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
			output.setValue(" " + count + " ");
			output.setStyle("color: red");
			children.add(output);
		}
	}

	public String goodbye() {
		return "success";
	}
}


5 两个jsp文件放在和index.jsp同一目录.
hello.jsp的内容:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
	<html>
		<head>
			<title>JSF in Action - Hello, world!</title>
		</head>
		<body>
			<h:form id="welcomeForm">
				<h:outputText id="welcomeOutput"
					value="Welcome to JavaServer Faces!"
					style="font-family: Arial, sans-serif; font-size: 24;      color: green;" />
				<p>
					<h:message id="errors" for="helloInput" style="color: red" />
				</p>
				<p>
					<h:outputLabel for="helloInput">
						<h:outputText id="helloInputLabel"
							value="Enter number of controls to display:" />
					</h:outputLabel>
					<h:inputText id="helloInput" value="#{helloBean.numControls}"
						required="true">
						<f:validateLongRange minimum="1" maximum="500" />
					</h:inputText>
				</p>
				<p>
					<h:panelGrid id="controlPanel" binding="#{helloBean.controlPanel}"
						columns="20" border="1" cellspacing="0" />
				</p>
				<h:commandButton id="redisplayCommand" type="submit"
					value="Redisplay" actionListener="#{helloBean.addControls}" />
				<h:commandButton id="goodbyeCommand" type="submit" value="Goodbye"
					action="#{helloBean.goodbye}" immediate="true" />
			</h:form>
		</body>
	</html>
</f:view>


goodbye.jsp内容:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
	<html>
		<head>
			<title>JSF in Action - Hello, world!</title>
		</head>
		<body>
			<h:form id="goodbyeForm">
				<p>
					<h:outputText id="welcomeOutput" value="Goodbye!"
						style="font-family: Arial, sans-serif; font-size: 24;     font-style: bold; color: green;" />
				</p>
				<p>
					<h:outputText id="helloBeanOutputLabel"
						value="Number of controls displayed:" />
					<h:outputText id="helloBeanOutput" value="#{helloBean.numControls}" />
				</p>
			</h:form>
		</body>
	</html>
</f:view>


全部完成后网址运行地址:
http://localhost:8080/{项目名称}

你可能感兴趣的:(java jsf)