While working on JSP pages, I often include conditional display logic by leveraging the JSTL <c:choose> tag. Trouble is, it's pretty verbose, with at least a couple of nested tags to write each time I use it.
To reduce my typing, I created a JDeveloper Code Template that lets me type ch followed by the [Ctrl]-[Enter] key combination to expand those two letters into this whole block of tags, ready to be used:
<c:choose> <c:when test="${}"> </c:when> <c:otherwise> </c:otherwise> </c:choose>
It even puts the insert-point cursor right between the ${ and } of the EL expression so I can press [Ctrl]-[Space] and get code-insight to help me fill out the EL expression I want in no time.
To define your own code template just pick the Tools | Preferences... | Code Editor > Code Templates to get to the preference panel for code templates. You'll see that there are a number of Java-related, predefined code templates in the list there. Click the (Add) button and in the Shortcut column enter the few-letter shortcut you want to type to be expanded when you press [Ctrl]-[Enter] in any code editor. You can also enter a description if you like. Down on the Code tab, type in the tags above, with the indentation that you want them to have when they get expanded into your JSP page code.
The last step is pretty subtle, but important. Click the mouse cursor into your just-pasted tags so that the i-beam cursor is just between the ${ and the } of the test attribute value on the <c:when> tag as shown in the example above. Then, press (OK) to save the preferences. The place where you click the i-beam cursor in the code template text when you click (OK) is where it will be placed automatically whenever you expand that particular code template.
Finally, create a new JSP page and try out the template by typing ch followed by a [Ctrl]-[Enter] while in the JSP page source view. One caveat is that if it's the first time you're using a tag from the JSTL Core tag library, make sure to add the <@taglib%> directive at the top of your JSP page for that tag library. It needs to look like this:
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
For Java-related code templates, while defining your code template you can click over to the Imports tab of the preference panel and type in a list of Java classes that your code template depends on. When that code template is expanded in a Java class' source code file, JDeveloper will make sure those classes are imported, and if they are not it will add them automatically. You list the class names to import, one per line, without semicolons at the end.
11:40:55 AM
|