بیشترلیست موضوعات • Index • ExamplesApache Jakarta and Beyond: A Java Programmers Introduction By
Larne Pekowsky Publisher : Addison Wesley Professional Pub Date : December 30, 2004 ISBN : 0-321-23771-4 Pages : 608
توضیحاتافزودن یادداشت جدید
18.4. Displaying Expressions
The ability to use a bean to control a tag is certainly powerful, but often such values must be shown to the user rather than used by a tag. There is a standard tag called c:out that renders values to the page, and its use is quite straightforward. Listing 18.7 displays various values from Bean1.
Listing 18.7. The out tag
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <jsp:useBean id="bean1" /> <p>Here is some data that came from bean1:</p> <ul> <li>The name of this bean is: <c:out value="${bean1.name}"/> <li>The 7th prime number is: <c:out value="${bean1.seventhPrimeNumber}"/> <li>The current time is: <c:out value="${bean1.currentTime}"/> </ul>
Although the c:out tag is being used to display only a simple value, a great deal of power is offered due to the fact that what is being shown is the result of a script instead of just a simple property.The Expression Language allows page developers to manipulate properties in many ways. For example, it is possible to write an expression that will add two numbers right in the page without needing to rely on any external mechanism. Listing 18.8 shows the first page of a simple calculator application.
Listing 18.9 shows the bean that will be used by this application.
Listing 18.9. A simple calculator bean
package com.awl.toolbook.chapter18; public class CalcBean implements java.io.Serializable { private int value1; public int getValue1() {return value1;} public void setValue1(int value1) { this.value1 = value1; } private int value2; public int getValue2() {return value2;} public void setValue2(int value2) { this.value2 = value2; } public int getSum() { return value1 + value2; } }
Next, Listing 18.10 shows how a JSP can populate this bean and perform the calculation directly using the expression language.
Listing 18.10. Addition in the expression language
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <jsp:useBean id="calc" /> <jsp:setProperty name="calc" property="*"/> The sum is: <c:out value="${calc.value1 + calc.value2}"/>
It is now possible to easily extend this to do more complex calculations, such as finding the average of the two numbers or raising one to the power of the other, and so on.Note that although this is very powerful, it also breaks the model/view/controller paradigm because the model is now being manipulated directly from the view. There are times when this is worth doing, but as a general rule of thumb it is better to leave such calculations in the bean.Another possibility offered by the c:out is that it can display things other than beans. Every JSP has access to a number of implicit objects, objects that the system provides without the developer needing to explicitly load or name them. One of these is the pageContext object, which contains a great deal of information about the action currently being performed. Among this information is the name of the page being generated, the name of the computer from which the request came, and so on. Listing 18.11 uses the pageContext object to display some of the available information.
Listing 18.11. The request object
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <ul> <li>Your computer is called <c:out value="${pageContext.request.remoteHost}"/> <li>This page came from server <c:out value="${pageContext.request.serverName}"/> <li>This page came from port <c:out value="${pageContext.request.serverPort}"/> </ul>
This example illustrates expressions with multiple dots. This follows directly from the use of BeanUtils. And compound expression handled by BeanUtils, including indexed and mapped properties, is valid in the JSP expression language.Another important implicit variable is called param, and it holds all the values that have been sent to a page by a form. This acts like a special bean in that it does not have a predefined set of properties; instead, it has a property for every value in the form.
<input type="text" name="color">
The user's response could be displayed on a page using
<awl:maybeShow show="${param.shouldShow}"> The time is: <awl:date format="hh:mm:ss MM/dd/yy"/> </awl:maybeShow>
Likewise, the calculator could do without its bean, reducing the page to
The sum is: <c:out value="${param.value1 + param.value2}"/>
This example may look familiar; it was used to create a testable page for use with HTTPUnit in Chapter 5.
Errors to Watch For
Trying to reference a property that a bean does not possess will result in an error. In addition, trying to reference a bean that does not exist, such as c:out value= "${someBean.someProperty}" if someBean has not been loaded, will not result in an error but simply in nothing being displayed. This can cause problems that may be hard to fixfor example, if the name of a bean is simply misspelled.
Because c:out can obtain values from a bean, it is not surprising that there is also a tag that sets values called c:set. Unlike the jsp:setProperty tag, the c:set tag cannot set all properties in a bean at once by using the special property "*." However, each of the parameters to c:set may be a script that allows properties to be set with dynamic values.
<c:set target="bean " property="property name " value="property value "/>
This sets the property called property name in the bean identified as bean name to value.