JSP and JSTL Tutorials - Tutorial Addendum - Using JavaBean Classes
| |
This affiliate explains:
- How to amount a JavaBean into JSP pages and dispense its properties.
- Compilation issues of using JavaBean classes in bearding packages.
- Setting and accepting JavaBeans properties.
- Using JavaBeans as Java altar in scripting elements.
- Using Java altar as JavaBeans.
- How to brace the JavaBean altar loaded in memory.
The "jsp:useBean" Activity Elements
jsp:useBean: A JSP activity aspect that endless a JavaBean item into the JSP page.
<jsp:useBean id="object_name">
where "object_name" is the name of the item to be created, and "class_name" is the
class name of the JavaBean chic from which the item will be instantiated.
Once a bean item is loaded into the page, you can use two additional activity elements to
manipulate it.
<jsp:setProperty name="obj" property="prop_name" value="prop_value"/>
<jsp:getProperty name="obj" property="prop_name"/>
The "setProperty" activity will set a new amount to the defined acreage of the specified
bean object. The "getProperty" activity will get the accepted amount of the defined property
of the defined bean object. This amount will be adapted into a string
Once a bean item is loaded into the page, it can be acclimated in additional
scripting elements in the aforementioned JSP page.
Here is my first JavaBean, CacheBean.java:
/**
* CacheBean.java
* Absorb (c) 2002 by Dr. Yang. All rights reserved.
*/
public chic CacheBean {
clandestine Cord argument = "null";
accessible Cord getText() {
acknowledgment text;
}
accessible abandoned setText(String text) {
this.text = text;
}
accessible Cord getInfo() {
acknowledgment "My JavaBean - Adaptation 1.00"
}
}
Here is a simple JSP page to appearance you how to use JavaBean, UseBean.jsp:
<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- UseBean.jsp
Absorb (c) 2002 by Dr. Yang
-->
<html><body>
<jsp:directive.page import="CacheBean"/>
<jsp:useBean id="b">
<jsp:setProperty name="b" property="text" value="Hello world!"/>
Property from my Bean:
<jsp:getProperty name="b" property="text"/>
<br/>
Info from my Bean:
<jsp:expression>b.getInfo()</jsp:expression>
</body></html>
</jsp:root>
Then I aggregate CacheBean.java with JDK 1.3.1, and affected CacheBean.class
to localjakarta-tomcat-4.1.18webapps
ootweb-infclasses.
Here is what I got by requesting UseBean.jsp from IE:
Property from my Bean: Accost world!
Info from my Bean: My JavaBean - Adaptation 1.00
Note that CacheBean chic needs to be alien into the JSP page. The chic file
needs to be affected to the .web-infclasses directory.
(Continued on next part...)
|
javabean, object, usebean, property, cachebean, value, action, class, public, string, version, classes, specified, getproperty, elements, setproperty, objects, javabeans, loaded, , < jsp, jsp page, usebean jsp, cachebean java, javabean classes, jsp usebean, bean object, obj property prop, property prop name, jsp getproperty name, jsp setproperty name, notes using javabean, tutorials tutorial notes, tutorial notes using, jstl tutorials tutorial, |
Also see ...
i(Continued from antecedent part...)/iIt s actual absorbing to see the Servlet chic translated from UseBean.jsp: package org.apache.jsp;import javax.servlet.*;import javax.
i(Continued from antecedent part...)/iIn this section, let s attending at some of the basal rules about ambience and gettingJavaBean properties:The setProperty aspect haveto be accura
i(Continued from antecedent part...)/iNote that:Property names are case sensitive. Acreage name "Author" can not be mapped to "getAuthor" method. Two set method
i(Continued from antecedent part...)/iUsing Java Altar as JavaBeansNow we understand that a JavaBean is just a accustomed Java item with some distinctively namedp