Using JSP Pages

JSP pages are a tested technology with which many developers are familiar. Because JSP pages are compiled into Java classes, and because they can contain Java code, developers may be too tempted to move parts of the business logic into the JSP pages. Needless to say, that is a very bad thing: not only does it violate the MVC type 2 architecture, it also makes the application very difficult to maintain.

There is no way to stop the developers from using Java scriptlets in the JSP pages, but you should not need to use them. If you ever find yourself needing a scriptlet, or if you find yourself performing much logic using the standard JSTL tags, consider writing a custom tag.

Spring offers a list of custom tags that simplify access to Spring features in your JSP pages, as shown in Table 18-1.

Table 18-1: Spring Custom Tags

Custom Tag

Description

htmlEscape

Sets a value indicating whether the output of other Spring tags should be escaped. If true, the HTML formatting strings (such as <, />, &, etc.) are replaced by their HTML visual codes.

message

Displays a message retrieved from the Spring messageSource beans; the message is identified by its code.

theme

Retrieves a value for an element defined in the current theme.

hasBindErrors

Evaluates a nested body if the page has validation errors.

nestedPath

Sets a nested path that is then used by the bind tag.

bind

Binds to an object and provides an object that allows you to access the bound value and any error messages.

transform

Transforms a variable to a string using the currently registered PropertyEditor. You can only use this with the bind tag.

Let's take a closer look at using the Spring custom tags. We begin with the message tag.

Using the message Tag

Let's create a messageSource bean definition in the application context file and an appropriate message.properties file. Listing 18-1 shows the definition of the messageSource bean.

Listing 18-1: messageSource Bean Definition

image from book
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ¿ "http://www.springframework.org/dtd/spring-beans.dtd">      <beans>     <bean           >         <property name="basename"><value>messages</value></property>     </bean>     <!-- other beans omitted --> </beans>
image from book

If you want to learn more about messageSource beans in web applications, go back to Listing 18-2 shows the message.properties file in English.

Listing 18-2: message.properties File

image from book
greeting=Hello <b>Spring</b> Framework required=This field is required and cannot be empty
image from book

Now, we create a default.jsp page that uses the Spring JSTL library to display the greeting message inside an H1 element. Listing 18-3 shows the code for this page.

Listing 18-3: Code for default.jsp Page

image from book
 <%@taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%>      <html> <head> <title>Pro Spring</title> </head> <body> <h1><spring:message code="greeting"/></h1> </body> </html> 
image from book

The usage of the message tag in this code is quite simple: we simply use it to output text that is looked up in the messageSource bean. This is obviously the simplest use of this tag, which you can modify by setting its attributes as shown in Table 18-2.

Table 18-2: message Tag Attributes

Attribute

Description

code

The code you use to look up the message text in the messageSource bean.

arguments

A comma-separated list of arguments passed to the getMessage() method of the messageSource bean.

text

Text that is displayed if the entry code is not found in the messageSource. Internally, this is used as an argument in the MessageSource.getMessage() call.

var

Specifies an object that is set to the value of the message.

scope

Specifies a scope to which the object specified in the var attribute is going to be inserted.

htmlEscape

Indicates whether the message tag should escape the HTML text. If not specified, the global value defined by the htmlEscape tag is used.

Using the theme Tag

The theme tag allows you to create themed pages. It uses the themeResolver bean to load all theme element definitions. For a more detailed explanation of the themeResolver bean, refer to Listing 18-4.

Listing 18-4: themeResolver Bean Definition

image from book
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ¿ "http://www.springframework.org/dtd/spring-beans.dtd">      <beans>     <bean           >         <property name="defaultThemeName"><value>cool</value></property>     </bean>     <!-- other beans omitted --> </beans>
image from book

The code fragment of the default.jsp page in Listing 18-5 displays the typical usage of the theme bean.

Listing 18-5: theme Tag Usage

image from book
<link rel="stylesheet" href="<spring:theme code="css"/>"> 
image from book

The theme tag has similar attributes to the message tag, which allows you to further control the values it generates. In fact, the theme tag is a subclass of the message tag, the only difference is that the codes are looked up in a themeResolver rather than messageSource bean. The attributes you can use are summarized in Table 18-3.

Table 18-3: theme Tag Attributes

Attribute

Description

code

The code used to look up the theme element text in the themeResolver bean.

arguments

A comma-separated list of arguments that is passed to the getMessage() method of the messageSource bean.

text

Text that is displayed if the entry code is not found in the messageSource. Internally, this is used as an argument in the MessageSource.getMessage() call.

var

Specifies an object that is set to the value of the theme resource.

scope

Specifies a scope to which the object specified in the var attribute is going to be inserted.

htmlEscape

Indicates whether the message tag should escape the HTML text. If not specified, the global value defined by the htmlEscape tag is used.

Using the hasBindErrors Tag

This tag evaluates its body if the bean specified in its name attribute has validation errors. If there are no errors, the body of the tag is skipped. Listing 18-7 demonstrates how to use this tag in a JSP page.

Listing 18-6: hasBindErrors Tag

image from book
<spring:hasBindErrors name="command">     There were validation errors: <c:out value="${errors}"/> </spring:hasBindErrors>
image from book

As with other beans, Table 18-4 lists attributes you can use to fine-tune the behavior of this tag.

Table 18-4: hasBindErrors Attributes

Attribute

Description

name

Name of the bean for which errors will be looked up.

htmlEscape

Indicates whether the message tag should escape the HTML text. If not specified, the global value defined by the htmlEscape tag is used.

Using the nestedPath Tag

This tag is used to include another object in the path resolution operation performed by the bind tag. The nestedPath tag also copies objects in an existing nestedPath to the nestedPath it creates.

The only attribute of this tag is path, which specifies a path to the object that is to be included in the nestedPath. The code in Listing 18-7 shows how to use the nestedPath tag to add a user attribute to the nestedPath, thus giving the bind tag access to properties exposed by the user bean.

Listing 18-7: nestedPath Tag Usage

image from book
<spring:nestedPath name="user"/>
image from book

Using the bind Tag

The bind tag is the most important tag exposed by the Spring tag library. It greatly simplifies data entry and validation. In its simplest form, it looks up an object's property, identified by the path attribute, and exposes a new variable named status that holds the object's value and validation errors. This is the most common usage scenario and it is shown in code fragment in Listing 18-8.

Listing 18-8: The bind Tag

image from book
<spring:bind path="command.name">     <input name="name" value="<c:out value="${status.value}"/>">     <span ><c:out value="${status.errorMessage}"/></span> </spring:bind>
image from book

This code assumes a command object is in the current scope, and that it exposes the name property. The bind tag then creates a status object and sets its status property to an instance of the BindStatus object for the property identified by the path attribute. If there are any validation errors, the errors property contains an instance of Errors; the PropertyEditor used to parse the value is in the propertyEditor property.

You can further control this tag with the attributes listed in Table 18-5.

Table 18-5: bind Tag Attributes

Attribute

Description

path

Identifies the object and its property that are used to create the status, errors, and propertyEditor properties.

ignoreNestedPath

If set to true, the bind tag ignores any available nested paths as set by the nestedPath tag.

htmlEscape

Indicates whether the message tag should escape the HTML text. If not specified, the global value defined by the htmlEscape tag is used.

Using the transform Tag

The transform tag looks up the appropriate PropertyEditor for the object specified in the value attribute and uses it to output the String representation. You can use this attribute instead of the Format JSTL tags to keep the formatting rules in Spring. The tag's usage is shown in Listing 18-9.

Listing 18-9: transform Tag Usage

image from book
<spring:transform value="command.expirationDate"/>
image from book

If the expirationDate property of the command object is of the Date class, and if there is a PropertyEditor registered for Date.class with specified formatting rules, the transform tag outputs the appropriately formatted date string. All attributes of the transform tag are listed in Table 18-6.

Table 18-6: transform Tag Attributes

Attribute

Description

value

Identifies the value to be formatted.

var

If specified, this attribute sets the output to the object specified in the value attribute.

scope

Specifies the scope to which the object specified in the var attribute is going to be inserted.

htmlEscape

Indicates whether the message tag should escape the HTML text. If this is not specified, the global value defined by the htmlEscape tag is used.

JSP Best Practices

As we have stated before, JSP pages offer the largest set of features you can use to generate HTML output. Because the JSP pages are compiled into Java classes, the applications using JSPs can suffer from low performance, especially when the pages are still being compiled. Another consideration is that if the content of the page is too big, the JSP will not compile into a Java class because Java methods cannot be more than 64KB long.

From an architectural point of view, there is a danger that developers will use Java code in the pages to perform business operations. This is clearly a violation of the MVC architecture and it will cause problems in the future development of the application.

If you keep all these potential limitations in mind, you will no doubt find that JSP pages are an excellent technology to use, especially when combined with tag libraries. However, there are other view technologies that may not be as feature-rich as JSP, but offer other benefits; the first in line is Velocity.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net