The JSP 2.0 expression language does not, in itself, provide a rich conditional evaluation facility. That capability is provided by the c:if and c:choose tags of the JSP Standard Tag Library (JSTL) or by another custom tag library. (JSTL and the creation of custom tag libraries are covered in Volume 2 of this book.) However, the expression language supports the rudimentary ?: operator as in the Java, C, and C++ languages. For example, if test evaluates to true , ${ test ? expression1 : expression2 } returns the value of expression1 ; otherwise , it returns the value of expression2 . Just remember that the main purpose of the expression language is to simplify presentation logic; avoid using this technique for business logic. An ExampleThe servlet of Listing 16.12 creates two SalesBean objects (Listing 16.13) and forwards the request to a JSP page (Listing 16.14) for presentation (Figure 16-7). However, if the total sales number is negative, the JSP page wants to use a table cell with a red background. If it is negative, the page wants to use a white background. Implementing this behavior is a presentation task, so the ?: operator is appropriate. Listing 16.12 Conditionals.javapackage coreservlets; /** Servlet that creates scoped variables that will be used * to illustrate the EL conditional operator (xxx ? xxx : xxx). */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Conditionals extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { SalesBean apples = new SalesBean(150.25, -75.25, 22.25, -33.57); SalesBean oranges = new SalesBean(-220.25, -49.57, 138.25, 12.25); request.setAttribute("apples", apples); request.setAttribute("oranges", oranges); RequestDispatcher dispatcher = request.getRequestDispatcher("/el/conditionals.jsp"); dispatcher.forward(request, response); } } Listing 16.13 SalesBean.javapackage coreservlets; public class SalesBean { private double q1, q2, q3, q4; public SalesBean(double q1Sales, double q2Sales, double q3Sales, double q4Sales) { q1 = q1Sales; q2 = q2Sales; q3 = q3Sales; q4 = q4Sales; } public double getQ1 () { return(q1); } public double getQ2 () { return(q2); } public double getQ3 () { return(q3); } public double getQ4 () { return(q4); } public double getTotal () { return(q1 + q2 + q3 + q4); } } Listing 16.14 conditionals.jsp<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD><TITLE>Conditional Evaluation</TITLE> <LINK REL=STYLESHEET HREF="/el/JSP-Styles.css" TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> Conditional Evaluation </TABLE> <P> <TABLE BORDER=1 ALIGN="CENTER"> <TR><TH> <TH CLASS="COLORED">Apples <TH CLASS="COLORED">Oranges <TR><TH CLASS="COLORED">First Quarter <TD ALIGN="RIGHT">${apples.q1} <TD ALIGN="RIGHT">${oranges.q1} <TR><TH CLASS="COLORED">Second Quarter <TD ALIGN="RIGHT">${apples.q2} <TD ALIGN="RIGHT">${oranges.q2} <TR><TH CLASS="COLORED">Third Quarter <TD ALIGN="RIGHT">${apples.q3} <TD ALIGN="RIGHT">${oranges.q3} <TR><TH CLASS="COLORED">Fourth Quarter <TD ALIGN="RIGHT">${apples.q4} <TD ALIGN="RIGHT">${oranges.q4} <TR><TH CLASS="COLORED">Total <TD ALIGN="RIGHT" BGCOLOR=" ${(apples.total < 0) ? "RED" : "WHITE" } "> ${apples.total} <TD ALIGN="RIGHT" BGCOLOR=" ${(oranges.total < 0) ? "RED" : "WHITE" } "> ${oranges.total} </TABLE> </BODY></HTML> Figure 16-7. You can use the C-style ?: operator to conditionally output different elements. However, the JSP Standard Tag Library (JSTL) is often a better alternative for this kind of conditional operation.
|