如何用JavaServer Faces开发Web应用(faces,javaserver,web,编程语言)

时间:2024-05-03 07:44:59 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

    %E5%A6%82%E4%BD%95%E7%94%A8JavaServer+Faces%E5%BC%80%E5%8F%91Web%E5%BA%94%E7%94%A8

构建你自己的应用程序

XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

这一部分具体讲述如何一步一步地创建你自己的JavaServer Faces应用。我所使用的例子很简单,它要求用户输入他(她)的名字,然后点击Submit按钮,然后应用程序会向用户显示一个欢迎的信息。

创建如下目录结构:

c:tomcat4.1webapps

hello

src

web

WEB-INF

web.xml

lib

classes

这个目录结构的基本意思是说,我想创建一个叫做hello的新应用程序。在hello子目录下,有一个src子目录,里面放所有的Java 源文件;还有一个web子目录,该目录下有一个WEB-INF目录,里面包含web.xml文件及另外两个子目录,分别是lib和classes。

把c:jsf-ea3lib 目录下所有的jar文件拷贝到我们上面创建的lib子目录中。

创建web.xml 文件,用来配置我们的这个Web应用。在使用JavaServer Faces的时候,必须指定几个配置,诸如:(1) servlet context listener、(2) 用来处理JavaServer Faces 请求的servlet以及 (3) 上述servlet的servlet mapping。 下面的代码是这个应用程序的一个配置文件。

代码1 web.xml

<!DOCTYPE web-app PUBLIC

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd">

<!-- Used to initialize and destroy the application helper and register a Renderer to a RenderKit --&gt

BasicServletContextListener

<!-- Faces Servlet --&gt

Faces Servlet

javax.faces.webapp.FacesServlet

<!-- FaceServlet should be loaded when the application starts up --&gt

1

<!-- Faces Servlet Mapping. Map the path to the servlet when a request for

the servlet is received --&gt

Faces Servlet

/faces/*

使用JavaServer Faces 标记创建HTML页面。

首先我们写一个 index.html 页面,这是用户进入这个应用程序的第一个页面。这个页面里面有一个超级连接,点击它可以启动应用程序。代码如下:

代码2 index.html

index

Click JSP">here to start the application.

当用户点击了“here”,系统就会装载“index.jsp”,代码如下:

代码3 index.jsp

Hello

<%@ taglib="" uri="http://java.sun.com/jsf/html" prefix="h">

<%@ taglib="" uri="http://java.sun.com/jsf/core" prefix="f">

<h:input_text id="username"

modelReference="UserNameBean.userName"/>

这个JSP页面有几个值得注意的地方:

定制标记库。组件标记库不需要硬编码HTML来构成UI组件,从而使得组件可以复用,而且core tag library可以让向组件注册事件和其它行为更为简单。

form 标记用来表示一个输入窗体。Input_text和command_button用来表示该窗体中的组件,嵌套在form标记中。

input_text标记表示一个文本框,可以供用户输入一个字符串。这个标记有两个属性,分别为id和modelReference。id属性对应这个组件,是可选的。modelReference代表模型对象的属性,这个属性保存了我们输入文本框的值。

command_button 代表了一个提交按钮。

如有必要,编写模型对象(JavaBean组件)。

模型对象bean 就像其它JavaBean组件一样:它有一组访问方法。下面的代码段显示了我们这个应用中要使用的JavaBean组件。

代码4 UserNameBean.java

public class UserNameBean {

String userName = null;

public UserNameBean () {

}

public void setUserName(String user_name) {

userName = user_name;

}

public String getUserName() {

return userName;

}

}

本文:如何用JavaServer Faces开发Web应用的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:J2EE如何创建Enterprise Bean下一篇:

12 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18