JSTL Tips o' the Day
Here's a couple of tips to help you in your JSP development with JSTL:
- The <c:out> tag has a default attribute you can use to provide a default value when the value in your the variable you're trying to write is null. This is much easier than using if statements. Example: <c:out value="${user.firstName}" default="None Given" />.
- You can populate dynamic values in a key from your ResourceBundle using the <fmt:param> tag. For instance, if you have a message with a link in ApplicationResources.properties - and you want to populate it with a struts-forward, it's easy with JSTL:
ApplicationResources.properties:
mainMenu.link=Click <a href="{0}">here</a> to return to the Main Menu.
In your JSP:
<fmt:message key="mainMenu.link"> <fmt:param><html:rewrite forward="mainMenu"/></fmt:param> </fmt:message>
Usually I try not to put any HTML in ApplicationResources.properties, but I don't think a minimal amount hurts anything. Of course, you could put all the HTML in your JSP, so mainMenu.link changes to "Click {0} to return to the Main Menu." and your JSP changes to:
<fmt:message key="mainMenu.link"> <fmt:param> <html:link forward="mainMenu">here</html:link> </fmt:param> </fmt:message>
Enjoy!
Posted by dsuspense on December 01, 2003 at 06:41 PM MST #