JSF的开发,核心是事件驱动,组件和标签的封装程度非常高,很多典型应用已经不需要开发者去处理http。页面操作会被自动映射到对应的java bean中,后台逻辑只需要同java bean发生交互。整个过程是通过“依赖注入(DI)”来实现的,其设计思想与spring相似。
下面实例所用的东东包括:Eclipse3.1+MyEclipse4.1, tomcat5.0, jstl 1.1, jsf 。
SayHello 类
package org.cssseek.jsf.model.hello;
public class SayHello {
public String say(String name)
{
return "你好,"+name;
}
}
HelloDelegater 类
package org.cssseek.jsf.control.hello;
import org.cssseek.jsf.model.hello.*;
public class HelloDelegater {
//------属性---------
private String name;//表单中的文本框数据会传到这里
private String result;//web页会从这里取得运行结果
private SayHello sayHello;//model层的对象,并不事例化,由系统注入
//-----set/get--------
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SayHello getSayHello() {
return sayHello;
}
public void setSayHello(SayHello sayHello) {
this.sayHello = sayHello;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
//-----逻辑方法---------
public String say()
{
this.setResult(sayHello.say(this.getName()));
return "ok";
}
}
index.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<f:view>
<h:form>
<h:panelGrid columns="3">
<h:outputLabel for="name" value="姓名:"/>
<h:inputText id="name" value="#{HelloDelegater.name}" required="true"/>
<h:message for="name"/>
<h:outputLabel value="#{HelloDelegater.result}"/>
</h:panelGrid>
<h:panelGroup>
<h:commandButton action="#{HelloDelegater.say}" value="提交"/>
</h:panelGroup>
</h:form>
</f:view>
</body>
</html>
faces-config.xml文件
<?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>
<managed-bean>
<managed-bean-name>SayHello</managed-bean-name>
<managed-bean-class>
org.cssseek.jsf.model.hello.SayHello
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>HelloDelegater</managed-bean-name>
<managed-bean-class>
org.cssseek.jsf.control.hello.HelloDelegater
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>sayHello</property-name>
<value>#{SayHello}</value>
</managed-property>
</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>
因为我们在url pattern中选择了*.faces,所以我们应该访问如下地址来查看程序:
http://localhost:8080/hellojsfdemo/index.faces
这个实例就到此结束啦。具体操作这里就没有详细写出来,读者自己看看吧。
posted on 2006-03-21 17:50
cssseek 阅读(1748)
评论(1) 编辑 收藏