合成视图 (Composite View)。 用于演示 JSP 页面中信息的设计模式。该设计模式从组件视图中创建一个组合视图。组件视图可能包括页面的动态部分和模块部分。当您从多个子视图中创建视图时,合成视图设计模式与 web 应用程序设计模式有关。合成的 web 页面通常来自各种资源的内容组成。页面的布局由其子视图的内容独立管理。例如,一个视图的子视图可能为 Navigation、Search、Feature Story 和 Headline。
当创建合成视图时,您既可以包含静态的内容,也可以包含动态的内容。静态内容由 HTML 文件组成。动态内容是 JSP 页面的一部分。可以在 JSP 安装时或运行时决定具体的内容。
前端控制器 (Front controller)。 负责路由传入请求并在 web 应用程序中实施导航的组件。有关使用前端控制器设计模式的详细信息,请参阅构建 Web 组件 编程系列书中的“前端控制器”部分,该书位于 Sun™ ONE Studio Developer Resources 网站的文档页面中。
JSP 页 (JSP Page)。 在 web 应用程序中用来向用户提供信息并能够使数据从终端用户流回服务器的文件。JSP pages 必须放置在 web 应用程序中以便可以在 IDE 中执行 JSP pages。
Servlet。 在 servlet 容器内执行的 Java 类,通常在 web 服务器内运行。Servlet 用于执行以下操作:
生成动态内容。
扩展 web 服务器和支持 web 的应用程序的功能。
使用请求-响应范例与 web 客户端(通常是浏览器应用程序,如 Netscape 或 Internet Explorer)进行交互。
该应用程序使用 JavaServer Pages Standard Tag Library (JSTL) 获取该应用程序中使用的动态数据以及国际化 JSP 页面。
当您构建 Midnight Cookie Company 应用程序时,本教程将带您浏览在 IDE 中的整个 web 开发过程。
Java Standard Development Kit (JDK™) version 1.4.2(下载)或 5.0(下载)
也可以下载并使用 Sun Java System (SJS) Application Server(下载)、JBoss 或 WebLogic。但是,与 IDE 绑定在一起的 Tomcat Web Server 可以提供两层 Web 应用程序(如本快速入门指南中描述的)所需的全部支持。仅当您要开发企业应用程序时才需要应用服务器(如 SJS Application Server、Jboss 或 WebLogic)。
在 IDE 中注册服务器
绑定的 Tomcat Web Server 会自动在 IDE 中注册。但是,在部署到 SJS Application Server、Jboss 或 WebLogic 之前,必须在 IDE 中注册一个本地实例。如果安装了 NetBeans IDE 5.0/SJS Application Server 包,则自动注册 SJS Application Server 的本地实例。否则,请执行以下步骤:
将提示您让 IDE 删除您的类文件,因为当构建项目时,它们将自动编译到您项目的 build 文件夹。单击 Delete 以允许 IDE 这样做。
IDE 在 IDE 中导入源、删除类文件并创建 Midnight 项目。可以在 Projects 窗口中查看其逻辑结构,在 Files 窗口中查看其文件结构。
使用 servlet 创建前端控制器
前端控制器负责路由传入用户请求并在 web 应用程序中实施导航。前端控制器提供一个入口点,通过该入口点在 web 应用程序通道中请求几个资源。前端控制器可以减少在 JSP 页面中复制代码,尤其是在几个资源都需要进行相同的处理时更是如此。有关前端控制器的详细信息,请参阅构建 Web 组件 编程系列书中的“前端控制器”部分,该书位于 Sun™ ONE Studio Developer Resources 网站的文档页面中。
展开 Midnight 项目节点和 Source Packages 节点。
右键单击 com.midnightcookies 包节点并选择 New > Servlet。
在 Class Name 文本框中输入 FrontController,并确保选中 Package 下拉列表中的 com.midnightcookies 包。单击 Next。
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getRequestURI();
id = id.substring(request.getContextPath().length());
getServletContext().log
("Midnight Cookies FrontController received a request for " + id);
// The page variable holds the path to the page that will be included
// in the content area of the template page /WEB-INF/docs/main.jsp.
// It is passed to the template in a request attribute.
String page;
// If no request is received or if the request received is
// for the root, serve /WEB-INF/docs/index.jsp
if (id == null || id.trim().equals("") || id.equals("/")){
page = "/WEB-INF/docs/index.jsp";
}
// If a request received is for a file that does not end in
// .jsp or .html, there was an error. Include the
// error page (nonesuch.jsp) and set the nonesuch attribute with
// the path requested.
else if (!id.endsWith(".jsp") && !id.endsWith(".html")) {
page = "/WEB-INF/docs/nonesuch.jsp";
request.setAttribute("nonesuch", id);
}
else {
page = "/WEB-INF".concat(id);
if (id.equals("/docs/cookies/CookieMake.jsp")) {
Cookie cookie = createCookie(request);
if (cookie == null) {
page = "/WEB-INF/docs/cookies/CookieCutter.jsp";
}
else {
response.addCookie(cookie);
request.setAttribute("cookie", cookie);
}
}
}
request.setAttribute("page", page);
try {
request.getRequestDispatcher("/WEB-INF/docs/main.jsp").forward(request, response);
}
catch(Throwable t) {
getServletContext().log(t.getMessage());
}
}
注意: 在 Source Editor 中输入代码(或者通过输入、复制和粘贴),将光标放在 Source Editor 中并按 Ctrl-Shift-F 可自动重新格式化该代码。要显示行号,请右键单击右边空白处并从上下文菜单中选择 Show Line Numbers。
在 Name and Location 面板中,在 Folder Name 文本框中键入 parameters,然后单击 Finish。在 Projects 窗口的 docs 节点下面将出现新的参数文件夹。
右键单击参数节点并选择 New > JSP。在 JSP File Name 文本字段中键入 Input 并确保选中 Folder 文本框中的 WEB-INF/docs/parameters。单击 Finish。
Input.jsp 将在 Source Editor 中打开。
在 Source Editor 中,用以下代码替换 Input.jsp 文件的内容:
<%@page contentType="text/html;charset=UTF-8"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@taglib prefix="midnight" uri="/WEB-INF/tlds/midnight.tld"%>
<fmt:setBundle basename="com.midnightcookies.Midnight" var="bundle"
scope="page"/>
<h3><fmt:message key="provide_input" bundle="${bundle}"/></h3>
<form method="POST" action="Output.jsp">
<table>
<tr>
<td><fmt:message key="type_input" bundle="${bundle}"/>:<td>
<%-- The value of the Input field will be placed in a
request parameter named "input" and it will be
passed to Output.jsp --%>
<td><input type="text" size="20" name="input" value=""></td>
</tr>
<tr>
<td><fmt:message key="submit" bundle="${bundle}"
var="buttonLabel" scope="page"/>
<midnight:button/>
</td>
<td></td>
</tr>
</table>
</form>
<%@page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<fmt:setBundle basename="com.midnightcookies.Midnight" var="bundle" scope="page"/>
<h3><fmt:message key="display_input" bundle="${bundle}"/></h3>
<fmt:message key="datareceived" bundle="${bundle}"/>:
<!--The following line gets the value of the request parameter named
"input". This is a demo application. For security reasons, one should
never echo user input without parsing first. -->
<c:out value="${param.input}"/>
在 cookies 文件夹中创建一个 Tray.jsp 文件,方法与创建其他 JSP 文件相同。确保将其放在 cookies 文件夹中。
在 Source Editor 中,用以下代码替换 Tray.jsp 文件的内容:
<%@page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<fmt:setBundle basename="com.midnightcookies.Midnight" var="bundle" scope="page"/>
<h3><fmt:message key="cookies" bundle="${bundle}"/></h3>
<table border="1">
<tr>
<th halign="center">#</th>
<th align="left">
<fmt:message key="name" bundle="${bundle}"/>
</th>
<th align="left">
<fmt:message key="value" bundle="${bundle}"/>
</th>
<tr>
<%-- We have to use expression to get at an array.
If we use ${cookie} then we get a map and
the entries are not automatically cast --%>
<% request.setAttribute("cookies", request.getCookies()); %>
<c:set var="i" value="0"/>
<c:forEach var="ck" items="${cookies}">
<c:set var="i" value="${i+1}"/>
<tr>
<td><c:out value="${i}"/></td>
<td><c:out value="${ck.name}"/></td>
<td><c:out value="${ck.value}"/></td>
</tr>
</c:forEach>
</table>
按 Ctrl-S 保存更改。
结束语
完成了上述的开发阶段之后,Projects 窗口和 Files 窗口中的视图将如下所示:
注意: 将上面演示的应用程序部署到 Tomcat Web Server。如果您注册并选择了不同的目标服务器,则您将不会有 META-INF/context.xml 文件。例如,如果您的服务器是 Sun Java System Application Server,则您的部署描述符文件为 WEB-INF/sun-web.xml 文件。
web 应用程序使用资源包属性文件帮助转换该 web 应用程序中使用的一些页面。因此,转换和资源包属性文件的内容一样广泛。
执行问题的故障排除
如果在执行 Midnight Cookie Company 应用程序时收到一个错误,下面是有关这些问题的一些可能解决方案的提示:
当 IDE 试图启动您指定的浏览器时收到一个异常
检查您指定的要与 IDE 一起使用的浏览器的路径是否正确配置:
从 IDE 的主窗口中选择 Tools > Options。
单击 Advanced Options。
展开 IDE Configuration 节点,然后展开 Server and External Tool Settings。
展开 Web 浏览器。
检查您的浏览器可执行文件的路径是否正确设置。如果不正确,请单击浏览器按钮或键入正确的路径。
当试图显示 JSP 页面时收到 File Not Found 错误。
如果将您的 web 浏览器设置为使用代理服务器,则您的 web 浏览器将试图路由您想通过代理服务器查看的所有文件,包括本地文件。您需要向 web 浏览器的设置中的忽略主机列表中添加 localhost 和您的机器名。有关详细信息,请参阅 IDE 帮助系统中标题为“Accessing Local Files Through a Proxy”的在线帮助主题。(选择 > Help Contents,展开“Servers and Databases node”,然后展开“Web Browsers”节点。)
指定 web 应用程序时收到 File not found 错误。
单击在本教程中创建的 JSP 文件的名称。确保在 New File 向导中输入对象的名称时,不要输入 .jsp 文件扩展名。如有必要,重新命名文件。
用于演示 JSP 页面中信息的设计模式。该设计模式从组件视图中创建一个组合视图。组件视图可能包括页面的动态部分和模块部分。当您从多个子视图中创建视图时,合成视图设计模式与 web 应用程序设计模式有关。合成的 web 页面通常来自各种资源的内容组成。页面的布局由其子视图的内容独立管理。例如,一个视图的子视图可能为 Navigation、Search、Feature Story 和 Headline。
当创建合成视图时,您既可以包含静态的内容,也可以包含动态的内容。静态内容由 HTML 文件组成。动态内容是 JSP 页面的一部分。可以在 JSP 安装时或运行时决定具体的内容。