Mutually Exclusive Conditionals statement in JSTL

When using a mutually exclusive conditional action, only one of the number of possible alternative actions gets its body content evaluated. This is the familiar if/else or if/then/else programming structure.

The JSTL actions , , and are used to construct mutually exclusive conditional statements. Note that the and actions are different. A action always processes its body content if its test condition evaluates to true. Only the ?rst action whose test condition evaluates to true will have its body content processed. The action has no attributes and primarily sets the context for a mutually exclusive conditional. The action can contain the and nested subtags.

When using these actions, at most one of the nested actions will be processed. The test conditional of the that evaluates to true will have its body content evaluated and written to the current JspWriter. There can be as many actions as desired. The action must be the last action nested within the . It isn’t required to use a , but if it is used, there can only be one. The body content of the is only evaluated if none of the tes tconditions for the actions evaluated to true. For example, the sample code below shows how the text rendered depends on a user’s buying habits. Using the EL in the test conditions we can direct our sales strategy to the appropriate purchasing habit of the user :

<c:choose> 
	<c:when test="${user.lastPurchaseAmount > 100}"> 
		Welcome big spender, check out all the new titles you can buy!
	</c:when> 
	<c:when test="${user.lastPurchaseAmount > 30}"> 
		Welcome, we’ve got some new titles that you might be interested in! 
	</c:when> 
	<c:when test="${user.lastPurchaseAmount > 1}">
		Welcome, let us help you find some great books! 
	</c:when> 
	<c:otherwise> 
		Come on, there has to be something that interests you! 
	</c:otherwise> 
</c:choose>

 

JSP-SERVLET JSTL