1.3 A Simple JSTL Web Application

   

Now that you have the necessary software installed, let's implement a simple JSTL Web application, shown in Figure 1-7.

Figure 1-7. A Simple JSTL Web Application

graphics/01fig07.jpg

The preceding JSP page uses the JSTL <fmt:message> action to set the window's title and display a greeting. Before we discuss the code, let's take a look at the Web application's files, which are shown in Figure 1-8.

Figure 1-8. The Web Application's Files

graphics/01fig08.jpg

The Web application has a deployment descriptor, one JSP page, two JAR files, and a properties file. The JAR files are from the JSTL Reference Implementation distribution. Although the Reference Implementation comes with other JAR files, such as xalan.jar for the XML actions, most applications will only use jstl.jar and standard.jar . The former contains the classes and interfaces defined under the javax.servlet.jsp.jstl package, and the latter contains classes and interfaces specific to the JSTL reference implementation.

The properties file defines an English resource bundle that the <fmt:message> action uses to localize text. The deployment descriptor defines two JSTL configuration settings ”one that sets the locale and another that sets the base name of the resource bundle used by the application. [14]

[14] See "Configuration Settings" on page 230 for more information about JSTL configuration settings.

The JSP page is listed in Listing 1.1.

Listing 1.1 index.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>    <head>       <%@ taglib uri='http://java.sun.com/jstl/fmt' prefix='fmt' %>       <title><fmt:message key='index.window-title'/></title>    </head>    <body>       <h2><fmt:message key='index.greeting'/></h2>    </body> </html> 

The preceding JSP page uses only the JSTL formatting library, which is accessible through the taglib declaration. That declaration must come before the first use of any formatting action, so in this case, the taglib directive is in the HTML head section. Typically, taglib directives are placed at the top of the body section.

The JSP page uses the <fmt:message> action to localize text. That action retrieves from a resource bundle the values that correspond to the action's key attribute; for example, index.greeting corresponds to Welcome to JSTL . But how does the <fmt:message> action know which resource bundle to use? That can be specified in a number of ways; [15] this application specifies the resource bundle in the deployment descriptor, which is listed in Listing 1.2.

[15] See "Localization Context Lookup" on page 268 for more information about specifying resource bundles.

The preceding deployment descriptor specifies two JSTL configuration settings that define a locale and resource bundle base name, en-US and messages , respectively.

Because of those settings, the <fmt:message> action will retrieve localized messages from the messages_en.properties file located in WEB-INF/classes . That properties file is listed in Listing 1.3.

The preceding properties file defines two key/value pairs for the window title and greeting.

If you want to localize this application for another language, you can simply specify a different locale in the deployment descriptor and provide a properties file that defines a resource bundle for that locale. Even if you don't plan to support multiple locales, you should still use <fmt:message> to localize all the text that your application displays to users, for two reasons. First, placing all of the text in one location ”a properties file ”makes it easier to find and change that text. Second, if you later decide to support more locales, supporting multiple locales is a simple matter of creating new resource bundles and modifying the locale configuration setting. See "I18N Actions" on page 248 for more information about internationalization, and see "Formatting Actions" on page 308 for information about formatting and parsing numbers , currencies, percents, and dates.

Listing 1.2 WEB-INF/web.xml
 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app   PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"   "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"> <web-app>    <context-param>       <param-name>  javax.servlet.jsp.jstl.fmt.locale  </param-name>       <param-value>  en-US  </param-value>    </context-param>    <context-param>       <param-name>  javax.servlet.jsp.jstl.fmt.localizationContext  </param-name>       <param-value>  messages  </param-value>    </context-param>    <welcome-file-list>       <welcome-file>          index.jsp       </welcome-file>    </welcome-file-list> </web-app> 
Listing 1.3 WEB-INF/classes/messages_en.properties
 index.window-title=A JSTL Application index.greeting=Welcome to the JSTL 

When you deploy your application, you have two choices: Create a Web archive (WAR) file and place it in your servlet container's webapps directory, or modify your servlet container's configuration file. To create a WAR file, you can use the JAR command; for example, jar cvf $TOMCAT/webapps/simpleJSTL.war * executed in a Web application's top-level directory will create a WAR file in Tomcat's webapps directory. [16] The next time you start Tomcat, it will load your application.

[16] $TOMCAT represents the Tomcat top-level directory.

For Tomcat, you can add a Web application in the configuration file ” $TOMCAT/conf/server.xml ”like this:

 ...  <!-- Tomcat 4.1.3 context --> <Context path="/core-jstl/introduction"          docBase="C:/core-jstl/introduction/simple"/> ... 

Tomcat refers to Web applications as contexts, so you specify a URL path for the context and its corresponding directory, known as a document base. Resin, on the other hand, refers to Web applications as Web apps and refers to document bases as application directories; here's how you specify your Web application in the Resin configuration file:

 ...  <!-- Resin 2.1.2 web app --> <web-app id="/core-jstl/introduction"          app-dir="C:/core-jstl/introduction/simple"/> ... 

Regardless of whether you are using Tomcat or Resin, the URL for the preceding application is http://localhost/core-jstl/introduction, assuming your servlet container is running on your desktop on port 80.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

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