Thursday, January 5, 2012

JavaBeans

index

PersonBean.java
package beans;

/**
 * Class <code>PersonBean</code>.
 */
public class PersonBean implements java.io.Serializable {
private String name;
private boolean deceased;

/** No-arg constructor (takes no arguments). */
public PersonBean() { }
/**
* Property <code>name</code> (note capitalization) readable/writable.
*/
public String getName() {return this.name;}

/**
* Setter for property <code>name</code>.
* @param name
*/
public void setName(final String name) {
this.name = name;}

/**
* Getter for property "deceased"
* Different syntax for a boolean field (is vs. get)
*/
public boolean isDeceased() {return this.deceased;}

/**
* Setter for property <code>deceased</code>.
* @param deceased
*/
public void setDeceased(final boolean deceased) {
this.deceased = deceased;}
}
------------------------------------
TestPersonBean.java:
import beans.PersonBean;

/**
 * Class <code>TestPersonBean</code>.
 */
public class TestPersonBean {
/**
* Tester method <code>main</code> for class <code>PersonBean</code>.
* @param args
*/
public static void main(String[] args) {
PersonBean person = new PersonBean();
person.setName("Bob");
 person.setDeceased(false);

 // Output: "Bob [alive]"
 System.out.print(person.getName());
 System.out.println(person.isDeceased() ? " [deceased]" : " [alive]");
}//main
}//TestPersonBean
----------------------
testPersonBean.jsp;
<% // Use of PersonBean in a JSP. %>
<jsp:useBean id="person" class="beans.PersonBean" scope="page"/>
<jsp:setProperty name="person" property="*"/>

<html><body>
Name: <jsp:getProperty name="person" property="name"/><br/>
Deceased? <jsp:getProperty name="person" property="deceased"/><br/>
<br/>

<form name="beanTest" method="POST" action="testPersonBean.jsp">
Enter a name: <input type="text" name="name" size="50"><br/>
Choose an option:
<select name="deceased">
<option value="false">Alive</option>
<option value="true">Dead</option>
</select>
<input type="submit" value="Test the Bean">
</form>

</body></html>
----------------------------------
JSP Syntax
<jsp:setProperty>
Sets a property value or values in a Bean.

<jsp:setProperty name="beanInstanceName"
{property= "*" |
 property="propertyName" [ param="parameterName" ] |
 property="propertyName" value="{ string | <%= expression %> }"
}
/>

Examples
<jsp:setProperty name="mybean" property="*" />
<jsp:setProperty name="mybean" property="username" />
<jsp:setProperty name="mybean" property="username" value="Steve" />
--------------------
<jsp:useBean>
Locates or instantiates a Bean with a specific name and scope.

JSP Syntax
<jsp:useBean  id="beanInstanceName"
scope="page|request|session|application"
{class="package.class" |
type="package.class" |
class="package.class" type="package.class" |
beanName="{ package.class | <%= expression %> }" type="package.class"
}
{/> |
> other tags </jsp:useBean>
}

Examples
<jsp:useBean id="cart" scope="session" class="session.Carts" />
<jsp:setProperty name="cart" property="*" />
<jsp:useBean id="checking" scope="session" class="bank.Checking" >
<jsp:setProperty name="checking" property="balance" value="0.0" />
</jsp:useBean>
--------------------
<jsp:getProperty>
Gets the value of a Bean property so that you can display it in a result page.

JSP Syntax
<jsp:getProperty name="beanInstanceName"  property="propertyName" />

Examples
<jsp:useBean id="calendar" scope="page" class="employee.Calendar" />
<h2>
Calendar of <jsp:getProperty name="calendar" property="username" />
</h2>
-------------------

No comments:

Post a Comment