📄 lib0101.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Presentation Components</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0100.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0102.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch15"></a>
<div class="section">
<h2 class="first-section-title"><a name="485"></a><a name="ch15lev1sec1"></a>Presentation Components</h2><p class="first-para">Each component in the presentation layer has one of the following roles: page display, user input validation, data processing, navigation, or security. Guidelines for developing presentation layer components for each role are presented in the following sections.</p>
<div class="section">
<h3 class="sect3-title">
<a name="486"></a><a name="ch15lev2sec1"></a>Page Display</h3>
<p class="first-para">Most pages are either HTML for static content or JSPs for dynamic content. When using Struts, you should use JSPs only to create the page. JSPs are written with the assumption that the information they need to dynamically generate content has already been produced and associated with the <span class="fixed">HttpSession</span>. With little in the way of conditional logic, JSP pages are relatively simple and easy to debug.</p>
<p class="para">Consider an example from ProjectTrak. A JSP produces a page that allows users to view the information associated with project tasks (e.g., who's assigned to it, the percentage of the task completed, its name, etc.). <a class="internaljump" href="#ch15fig02">Figure 15.2</a> illustrates what the page looks like.</p>
<div class="figure">
<a name="487"></a><a name="ch15fig02"></a><span class="figuremediaobject"><a href="images/fig217%5F01%5F0%2Ejpg" NAME="IMG_43" target="_parent"><img src="images/fig217_01.jpg" height="169" width="350" alt="Click To expand" border="0"></a></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 15.2: </span>JSP Page Output</span>
</div>
<a name="488"></a><a name="IDX-202"></a>
<p class="para">The JSP assumes that information for a project task (the <span class="fixed">ProjectTaskVO</span> object) has been retrieved and is already on the session. All the JSP has to do is obtain the value object from the session and populate the appropriate controls on the page. This is simple logic. <a class="internaljump" href="#ch15list01">Listing 15.1</a> has a code extract from the JSP.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 15.1: </span>JSP to Produce the Page in Figure 15.1</span><a name="489"></a><a name="ch15list01"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
1:<HTML>
2:<!—
3: ProjectTrak Task Information Report
4:
5: Author: Derek C. Ashmore
6:—>
7:
8:<%@ page import="com.dvt.app.project.dto.*,
9: java.text.SimpleDateFormat;"
10:%>
11:
12:<jsp:useBean id=" projectName" scope=" session"
13: class=" java.lang.String" />
14:<jsp:useBean id=" startDate" scope=" session"
15: class=" java.lang.String" />
16:<jsp:useBean id=" endDate" scope=" session"
17: class=" java.lang.String" />
18:<%
19: ProjectTaskWithProjectionDTO[] task =
20: (ProjectTaskWithProjectionDTO[])
21: session.getAttribute("taskList");
22:%>
23:
24:<HEAD>
25:<TITLE><%= projectName %> Task Information</TITLE>
26:<META http-equiv=" Content-Type"
27: content=" text/html; charset=windows-1252">
28:</HEAD>
29:
30:<BODY>
31:<H1>ProjectTrak</H1>
32:<P>
33:<BR>Project Start Date: <%= startDate %>
34:<BR>Project Finish Date: <%= endDate %>
35:<P>
36:<H2>Assignments</H2>
37:<TABLE BORDER>
38: <TR BGCOLOR="#DFDFDF">
39: <TH NOWRAP>Task ID</TH><a name="490"></a><a name="IDX-203"></a>
40: <TH NOWRAP ALIGN=left>Task Name</TH>
41: <TH NOWRAP ALIGN=left>Resource Name</TH>
42: <TH NOWRAP>Work</TH>
43: <TH NOWRAP>Start</TH>
44: <TH NOWRAP>Finish</TH>
45: <TH NOWRAP>% Work Complete</TH>
46: </TR>
47:
48: <%
49: SimpleDateFormat format =
50: new SimpleDateFormat("MM/dd/yy");
51: for (int i = 0 ; i < task.length ; i++)
52: {
53: %>
54:
55: <TR BGCOLOR="#FFFFFF" ALIGN=right>
56: <TD ALIGN=center><%= task[i].getTaskId() %></TD>
57: <TD ALIGN=left><%= task[i].getTaskName() %></TD>
58: <TD ALIGN=left>
59:<%= task[i].getAssignedResource().getResourceName() %>
60: </TD>
61: <TD NOWRAP>
62: <%= task[i].getEstimateInHours() %> hrs
63: </TD>
64: <TD NOWRAP>
65: <%= format.format(task[i].getProjectedStartDate()) %>
66: </TD>
67: <TD NOWRAP>
68: <%= format.format(task[i].getProjectedEndDate()) %>
69: </TD>
70: <TD>0%</TD>
71: </TR>
72:
73: <%
74: }
75: %>
76:
77:</TABLE>
78:</BODY>
79:</HTML>
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">
<i class="emphasis">Source:</i> /jsp/TaskList.jsp</p>
<p class="para">A key to successful JSP development is using them for content display only. JSPs with navigation and business logic embedded in them can be complex and difficult to debug. Debugging complicated JSPs is a time-intensive effort, and without the tools required for interactive debugging, the developer is reduced to primitive tracing.</p>
<a name="491"></a><a name="IDX-204"></a>
</div>
<div class="section">
<h3 class="sect3-title">
<a name="492"></a><a name="ch15lev2sec2"></a>User Input Validation</h3>
<p class="first-para">User input validation (e.g., ensuring the user entered all required fields) can be performed on the client with Javascript or on the server. In a Struts world, server-side user validation is delegated to <span class="fixed">ActionForm</span> classes. Client-side input validation is often faster, especially if the user has a slow Internet connection. However, because support for Javascript support varies from browser to browser, Javascript can be a maintenance nightmare. These maintenance issues and the proliferation of high-speed Internet connections support validating user input on the server side.</p>
<p class="para">
<span class="fixed">ActionForm</span> objects aren't required for Struts unless you're doing serverside input validation. If you've nothing to validate for a page, don't bother creating a form for that page.</p>
<p class="para">Struts offers two ways to manage input validation. The first is to use the Struts Validator plug-in, which allows you to essentially program the validation rules in XML documents instead of Java. Although some developers see this as being easier, I don't. The set-up overhead and complexity make this an unattractive option in my view. For more detail on this point, see the Struts documentation.</p>
<p class="para">Another alternative is to code the validation rules in Java within the form. Validation rules are classes that extend <span class="fixed">org.apache.struts.action.ActionForm</span> and are coded in an override to method <span class="fixed">validate()</span>. The <span class="fixed">validate()</span> method returns object <span class="fixed">ActionErrors</span>, which contains a description of all errors found. Struts manages navigating users to the pages they came from, and the JSP takes care of displaying validation errors, should they be present. <a class="internaljump" href="#ch15list02">Listing 15.2</a> illustrates the <span class="fixed">validate()</span> override in a form. This option sounds straightforward enough, but there's more.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 15.2: </span>Sample Form Containing Validation Rules</span><a name="493"></a><a name="ch15list02"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
1: public ActionErrors validate( ActionMapping mapping,
2: HttpServletRequest request)
3: {
4: ActionErrors errors = null;
5:
6: if (_proj == null)
7: {
8: errors = new ActionErrors();
9: errors.add("proj",
10: new ActionError("Null project not allowed."));
11: }
12: else if (_proj.equals(""))<a name="494"></a><a name="IDX-205"></a>
13: {
14: errors = new ActionErrors();
15: errors.add("proj",
16: new ActionError("Blank project not allowed."));
17: }
18:
19: if (errors != null)
20: {
21: request.getSession().setAttribute("errors",
22: errors);
23: }
24: return errors;
25: }
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">It turns out that <span class="fixed">ActionError</span> objects contain a key that is used to look up message text in a properties file, which is managed as a <span class="fixed">ResourceBundle</span>. Using <span class="fixed">ResourceBundle</span> objects, you can support user messages in multiple languages. This powerful feature is a necessary complexity for multinational applications. However, managing and coordinating these keys between your properties files and your code is annoying and painful, so if your business applications aren't written to support multiple languages, don't add the unnecessary complexity.</p>
<p class="para">Struts provides a tag library to assist JSPs in displaying validation errors should they occur. These libraries can be used with either of the two validation methods previously described.</p>
<p class="para">Notice that <a class="internaljump" href="#ch15list02">listing 15.2</a> doesn't define the <span class="fixed">ActionError</span> messages with property keys but inserts the message text instead. It also stores the error messages on the session, which isn't typically required when overriding <span class="fixed">validate()</span>. This is an inelegant shortcut I often use with Struts.</p>
<p class="para">In JSPs that require input validation, you can insert a one-line include statement like the following:</p>
<div class="informalexample">
<pre class="literallayout">
<jsp:include page="/jsp/ShowErrors.jsp" flush="true"/>
</pre>
</div>
<p class="para">With the JSP include statement, you can retrieve error messages from the session and format them for the user. Because it doesn't use the <span class="fixed">ActionError</span> objects as they we're intended to be used, this shortcut is crude. However, it does have the effect of eliminating the overhead of managing any property files, thus saving you development time. <a class="internaljump" href="#ch15list03">Listing 15.3</a> illustrates <span class="fixed">ShowErrors.jsp</span>. As applications vary widely in look and feel, you'll want to customize the error display to fit the look and feel of each application.</p>
<div class="example">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -