Core JSTL[c] Mastering the JSP Standard Tag Library
This chapter has covered a lot of ground. We've discussed the JSTL general-purpose and conditional actions and shown how those actions can be useful for specific purposes such as retaining HTML element values and emulating if/else constructs and switch statements. Now let's see how to use those actions together to implement a simple Web application, shown in Figure 3-10, that lets you change background and foreground colors for all pages in the application. Figure 3-10. Setting Color Preferences
The top picture in Figure 3-10 shows the Web application's welcome page, which provides a link to a color preferences JSP page that lets you modify your color preferences. The middle page in Figure 3-10 shows the color preferences page, which contains a simple form with two HTML select elements that let you select background and foreground colors. When you click the submit button in the color preferences page, you return to the previous page, which adjusts its colors to the background and foreground colors that you specified, as shown in the bottom picture in Figure 3-10. The application shown in Figure 3-10 has a few special features. First, the button in the color preferences page displays the name of the previously displayed page, as you can see from the middle picture in Figure 3-10. Second, the application will not let you specify the same colors for the foreground and background because you won't be able to see anything if you do. If the same colors are specified for the foreground and background, the color preferences page is redisplayed with an error message, as shown in Figure 3-11. Third, the error message displayed in the color preferences page displays the name of the JSP page that was last displayed. Figure 3-11. Handling a Bad Color Combination
The application shown in Figure 3-10 consists of two JSP pages: welcome.jsp and set-colors.jsp . The former is listed in Listing 3.15. Listing 3.15 welcome.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <%-- The taglib declaration is in the <head> section because <c:out> is used by the <body> element below --%> <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c'%> <title>Welcome to core-jstl.com</title> </head> <%-- Specify a background color for the body that matches the bgcolor request parameter --%> <body bgcolor=' <c:out value="${param.bgcolor}"/> '> <%-- Store the name of this page in a session-scoped variable. That variable is used below to generate an error message and in set_colors.jsp to set the submit button's text --%> <c:set var='lastPage' scope='session' value='core-jstl.com'/> <%-- If request parameters for foreground and background colors exist... --%> <c:if test='${! empty param.fgcolor && ! empty param.bgcolor}'> <%-- ...create a color preferences bean and store it in session scope so that those color preferences can be used by other JSP pages in the application --%> <jsp:useBean id='bean' scope='session' class='beans.ColorPreferences'/> <%-- Set the bean's background property to the background color specified with the bgcolor request parameter and... --%> <c:set target='${bean}' property='background' value='${param.bgcolor}'/> <%-- ...set the bean's foreground property to the foreground color specified with the fgcolor --%> <c:set target='${bean}' property='foreground' value='${param.fgcolor}'/> <%-- If the bean's foreground and background colors are the same... --%> <c:if test='${bean.background == bean.foreground}'> <%-- ...store an error message in session scope and...--%> <c:set var='colorErrorMessage' scope='session'> Sorry, but you can't specify the same colors for the foreground and background because you won't be able to see anything at <c:out value='${sessionScope.lastPage}'/> </c:set> <%-- ...go back to set_colors.jsp --%> <jsp:forward page='set_colors.jsp'/> </c:if> </c:if> <%-- Set font characteristics --%> <font size='6' face='Arial,Helvetica' color='<c:out value="${bean.foreground}"/>'/> Welcome to core-jstl.com </font> <hr><p> <%-- Reset font size --%> <font color=' <c:out value="${bean.foreground}"/> '/> <i>Core JSTL</i> is an in-depth examination of the JavaServer Pages Standard Tag Library (JSTL), which provides a standard set of custom tags including: <ul> <li>General Purpose Actions <li>Conditional and Iteration Actions <li>URL Actions <li>Database Actions <li>Internationalization Actions <li>XML Actions </ul> <hr> Click <a href='set_colors.jsp'>here</a> to change your color preferences. </font> </body> </html> The first thing you should notice about the preceding JSP page (known hereafter as the welcome page) is that the taglib declaration for the JSTL core actions resides in the HTML head section, instead of the body section, as is the case for the other JSP pages discussed in this chapter. That's because the HTML <body> tag uses the <c:out> action to set the background color to the color specified with the bgcolor request parameter, so the taglib declaration must come before the <body> tag. The bgcolor parameter is set in the color preferences page, so if you access the welcome page directly, that request parameter will not exist and the <c:out> action will not produce any output. So the first time the welcome page is accessed, the background color will be the default color, as you can see from the top picture in Figure 3-10 on page 138. After the welcome page sets its background color, it uses <c:set> to store the name of the welcome page in a session-scoped variable named lastPage . That scoped variable is used to set the text of the submit button in the color preferences page and is also used later on in the welcome page to generate an error message if the foreground and background colors are the same. After setting the background color and storing the name of the welcome page in a scoped variable, the welcome page tests to see if the fgcolor and bgcolor request parameters exist; if so, the welcome page is being accessed from the color preferences page, so the welcome page creates a color preference bean and stores it in session scope. [18] Consequently, the welcome page stores the colors specified with the fgcolor and bgcolor request parameters in the bean's foreground and background properties, respectively, with the <c:set> action. [18] That bean is created only once per user (or per session) by the <jsp:useBean> action. That bean is an instance of ColorPreferences , which is listed in Listing 3.3 on page 108. Subsequently, the welcome page checks to see if the bean's foreground and background properties are the same; if so, the welcome page creates a session-scoped variable that references an error message. Notice that the error message contains the name of the welcome page, which was previously stored in the lastPage scoped variable. Finally, if the foreground and background colors are the same, the welcome page forwards to the color preferences page; otherwise , the welcome page displays its content, which includes a link to the color preferences page. The color preferences page ” set_colors.jsp ”is listed in Listing 3.16. Listing 3.16 set_colors.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Set Color Preferences</title> </head> <body> <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %> <c:choose> <%-- If there's an error message about a bad color combination... --%> <c:when test='${not empty sessionScope.colorErrorMessage}'> <%-- ...set the font size and color,... --%> <font size='4' color='red'> <%-- ...display the error message and... --%> <c:out value='${sessionScope.colorErrorMessage}'/> <p> </font> <%-- ...remove the error message from session scope. Note: it's not necessary to specify the scope attribute for <c:remove>, but it's good practice. --%> <c:remove var='colorErrorMessage' scope='session'/> </c:when> <%-- If there's not an error message about a bad color combination, ask the user to set colors --%> <c:otherwise> Please set your background and foreground colors: </c:otherwise> </c:choose> <%-- Store the preferred colors stored in the color preferences bean in page-scoped variables, for better readability in the <c:if> actions used for the HTML options below --%> <c:set var='prefBgColor' value='${sessionScope.bean.background}'/> <c:set var='prefFgColor' value='${sessionScope.bean.foreground}'/> <%-- The form --%> <p><form action='welcome.jsp'> <table> <tr> <td>Background Color:</td> <td> <%-- Create the HTML select element for background color --%> <select name='bgcolor'> <%-- Create option elements and select the previously selected background color --%> <option value='white' <c:if test='${prefBgColor == "white"}'> selected </c:if> >white</option> <option value='yellow' <c:if test='${prefBgColor == "yellow"}'> selected </c:if> >yellow</option> <option value='black' <c:if test='${prefBgColor == "black"}'> selected </c:if> >black</option> <option value='blue' <c:if test='${prefBgColor == "blue"}'> selected </c:if> >blue</option> </select> </td> </tr> <tr> <td>Foreground Color:</td> <td> <%-- Create the HTML select element for foreground color --%> <select name='fgcolor'> <%-- Create option elements and select the previously selected foreground color --%> <option value='white' <c:if test='${prefFgColor == "white"}'> selected </c:if> >white</option> <option value='yellow' <c:if test='${prefFgColor == "yellow"}'> selected </c:if> >yellow</option> <option value='black' <c:if test='${prefFgColor == "black"}'> selected </c:if> >black</option> <option value='blue' <c:if test='${prefFgColor == "blue"}'> selected </c:if> >blue</option> </select> </td> </tr> </table> <%-- Create the submit button text, using the name of the last page stored in session scope --%> <c:set var='submitButtonText'> Return to <c:out value='${sessionScope.lastPage}'/> </c:set> <%-- Create the submit button, using the value of the page-scoped variable created above as the button's text --%> <p><input type='submit' value=' <c:out value="${submitButtonText}"/> '/> </form> </body> </html> The color preferences page firsts checks to see if there's an error message in session scope named colorErrorMessage ; if so, the color preferences page changes the font color to red and displays the message. After the message is displayed, the color preferences page removes it from session scope with the <c:remove> action. After it deals with the error message, the color preferences page stores the color preference bean's background and foreground colors in page-scoped variables solely for better readability when those colors are accessed further down the page. Then the color preferences page creates its form, using the <c:if> action to retain the values displayed by its HTML select elements, as discussed in "Retaining Values for HTML Option Elements" on page 129. Finally, the color preferences page creates the submit button, which includes the name of the last page accessed, which is stored in the session-scoped variable named lastPage . |