一、What is a Bean?
A bean is simply a java class desinged according to the set of guidelines defined by the JavaBean specification.
1、some notice about bean:
a)、A bean class must have a no-argument constructor.This allow a tool to create any bean in a generic fashion knowing just the class name.
b)、The bean properties are accessed through getter and setter methods.
c)、the bean class should implement the java.io.Serializable or the java.io.Externalizable interface to allow tool to save and restore the bean's state.
2、the benifit about using a bean
the nice thing about using a bean is that it can encapsulate all information about the item it represents in one simple package.Another benifit of using a bean is that the bean can encapsulate all the rules about its properties.
二、declaring a Bean in a JSP page
A dose of Dilbert A dose of Dilbert
class="com.ora.jsp.beans.motd.CartoonBean" />
property="fileName" />">
is to declare the bean.The action creates an instance of the bean class specified by the class attribute and associated it with the name specified by the 'id' attribute.
三、Reading Bean properties
A bean's data is represented by it's property.There are two ways to insert a bean property value in a JSP page.
1、using the Action
Once you have created a bean and given it a name using action,you can get the bean's property values with another JSP standard action,named .This action obtains the current value of a bean property and insert it directly into the response body.
the code:
property="fileName" />"> looks a bit strange: an element() is used as the value of another element's attribute(the
tag's src attribute).
NOTE:this doesn't mean you can use an action element as the value of another JSP action element attribute.Using it to set an HTML element attribute works only because the HTML element isn't recognized as an element by the container.To set a JSP action attribute to the value produced by another action,you must use the standard action instead:
Here the 'value' attribute of the action is set using a action nested in a action element.If you need to define a body for the action element,in addition to attribute values set with , you must define the body with a element.
2、using the JSP Expression Language
A dose of Dilbert
A dose of Dilbert
class="com.ora.jsp.beans.motd.CartoonBean" />
![](images/${cartoon.fileName})
四、setting Bean properties
There two actions can use to set the value: or
1、Using the action
Messages of the Day
Messages of the Day
class="com.ora.jsp.beans.motd.MixedMessageBean" />
Deep Thoughts - by Jack Handey
value="thoughts" />
Quotes From the Famous and the Unknown
value="quotes" />
2、Using the JSTL action
The is more flexible than .so if you don't need to be JSP 1.1 compatible, is your first choice.
阅读(729) | 评论(0) | 转发(0) |