JavaSercer Pages (Hosting tomcat) 8.3 Using Custom Actions You might

JavaSercer Pages 8.3 Using Custom Actions You might be wondering if we are stretching the bean model too far in the previous example. Perhaps. The CounterBean does more than hold information; it also has a non-conforming method for incrementing the counter. If we stray away from the purely bean model and use methods with arguments, this may force us to use scriptlets instead of the standard actions. That’s not necessarily bad, but in this case we can do better using a custom action instead of a bean and the standard actions. A custom action is just like the standard actions we’ve used so far. It has a start tag, which may contain attributes, and an end tag. It can also have a body. Here’s what a custom action looks like: The JSP specification defines how the standard set of actions can be extended with custom actions developed by Java programmers in the team or by a third party. A custom action is used in a JSP page in exactly the same way as the standard JSP actions you have seen in previous examples, such as . This makes them easier to use than beans with methods that must be invoked with scripting code, since you don’t have to worry about missing braces and semicolons and other syntax details. A custom action can do pretty much anything: it has access to all information about the request and can add content to the response body as well as set response headers. If you’re a programmer, you should know that a custom action is basically a JavaBeans class, with property setter methods corresponding to the action’s attributes, plus a few extra methods used by the JSP container to invoke the action. You can read all about how to develop your own custom actions in Chapter 16. As is often the case in software development, it’s hard to say exactly whether a bean or a custom action is the preferred component type. My rule of thumb is that a bean is a great carrier of information, and a custom action is great for processing information. Custom actions can use beans as input and output. For instance, an action can be used to save the properties of a bean in a database, or to get information from a database and make it available to the page as a bean. If you’re a page author, you don’t have to worry about the implementation details. All you need to know right now is how to use the custom actions you have available. You’ll find many custom actions in this java blog that you can use, and more are available from open source projects and commercial companies listed in Appendix E. Custom actions are grouped together in a tag library. Consequently, you often see custom actions referred to as custom tags, even though that is not strictly correct. A tag library consists of a Tag Library Descriptor (TLD) and the Java classes used to implement the custom actions. The TLD contains information about the action names and attributes. It’s used by the JSP container during the translation phase to verify that all actions are used correctly in the page, for instance that all mandatory attributes are specified. Typically, the TLD and all classes are packaged in a Java Archive (JAR) file. You install such a library by placing the JAR file in the WEB-INF/lib subdirectory for the application in which it’s used. If you look at the files in your Tomcat installation for the ora application (containing all the book examples), you see the JAR file in WEBINF/ lib/orataglib_1_0.jar and the TLD in WEB-INF/tlds/orataglib_1_0.tld. When you use custom actions in a JSP page, you must identify the library using the taglib directive: <%@ taglib uri="/orataglib" prefix="ora" %> The uri attribute value identifies the library. Depending on how the library is installed, different types of values are used: a symbolic name, the path to the JAR file, or the path to the TLD file. My recommendation is to use a symbolic name, as shown in the example. The symbolic name must then be mapped to the location of the library in the WEB-INF/web.xml file for the application: /orataglib /WEB-INF/tlds/orataglib_1_0.tld page 98

Hint: If you are looking for high quality and reliable webspace provider to host and run your jsp hosting application check Sandzak jsp web hosting provider

Comments are closed.