Using Beans or Custom Actions
The examples used in this
chapter show that a custom action can provide the same functionality
as a bean. In Chapter 6, we created a
MixedMessageBean, set its
category attribute, and retrieved the value of its
message property to the page:
<jsp:useBean id="msg" class="com.ora.jsp.beans.motd.MixedMessageBean" /> ... <jsp:setProperty name="msg" property="category" value="thoughts" /> ... <jsp:getProperty name="msg" property="message" />
In this chapter, we use a custom action to accomplish exactly the same thing:
<ora:motd category="thoughts" />
This raises the question of when it’s better to use one or the other of these two component types. As is often the case in software development, there’s no rule applicable to all cases; in other words, we are left with “it depends.” 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 save the properties of a bean in a database, or get information from a database and make it available to the page as a bean. In Chapter 8, I will show how a bean can also capture and validate user input in a very powerful way.
Some beans do more than carry information; they encapsulate functionality intended for use in many different environments, such as in applets, servlets, and JSP pages. In a case like this, a custom action can internally use the bean, providing a JSP-specific adapter ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access