140 likes | 365 Views
Java Beans. Celsina Bignoli bignolic@smccd.net. Java Beans. Used in JSP as a containers for dynamic content A bean encapsulates data about one entity Java classes that follow certain coding conventions must be in a package, ex com.petstore.bean must have a no-argument constructor
E N D
Java Beans Celsina Bignoli bignolic@smccd.net
Java Beans • Used in JSP as a containers for dynamic content • A bean encapsulates data about one entity • Java classes that follow certain coding conventions • must be in a package, ex com.petstore.bean • must have a no-argument constructor • data in the bean is referred to as properties • bean properties are accessed through getter and setter methods • a property is eitherread-only,write-onlyorread-write • a property is case-sensitive and starts with a lower-case letter
Java Beans Accessor Methods • get (set) + name of the property with first character of the name capitalized • Ex: if propety name is fileName • setter method is setFileName() • getter method is getFileName() • a getter method has no arguments • a setter method returns void • a read-only property has a getter method • a write-only property has a setter method • a read-write property has a setter and a getter method
Java Beans -Example Pet properties id: int (read/write) name: String (read/write) species: String (read/write) owner: String (read/write) methods int getId() void setId(int) String getName() void setName(String) String getSpecies() void setSpecies(String) boolean getBirthDate() String getOwner() void setOwner(String)
Declaring a Bean in a JSP Page • To use beans in JSP you first must declare it using the JSP <jsp:useBean> standard action: <jsp:useBean id=“pet” class=“com.petstore.bean.PetBean” /> • creates an instance of the bean specified by the class attribute • associates it with the name specified by the id attribute • the name must be unique in the page, be a valid Java variable name • must start with a letter • have no special characters in it
Deploying a JavaBean • the javaBean class file must be stored under WEB-INF/classes of your application. • Example: PetBean.class should go in: WEB-INF classes com petstore bean PetBean.class • Restart Tomcat after deploying the bean class and after making changes to it!
Reading Bean Properties • to read a bean property use the JSP <jsp:getProperty> standard action <table> <tr> <th>Name</th> <th>Species</th> … </tr> <tr> <td><jsp:getProperty name=“pet” property=“name” /></td> <td><jsp:getProperty name=“pet” property=“species” /></td> … </tr> </table>
Reading Bean Properties with EL • to read a bean property you can also use JSP expression language <%@ taglib prefix=“c” uri=http://java.sun.com/jsp/jstl/core %> … <table> <tr> <th>Name</th> <th>Species</th> … </tr> <tr> <td><c:out value=“${pet.name}” /></td> <td><c:out value=“${pet.species}” /></td> … </tr> </table>
Setting Bean Properties with JSP actions • to set bean properties use the JSP <jsp:setProperty> standard action <jsp:setProperty name=“pet” property=“name” value=“Fluffy” /> <jsp:setProperty name=“pet” property=“species” value=“cat” />
Setting Bean Properties with EL amd JSTL • to set bean properties use the JSP <jsp:setProperty> standard action <c:set target=“${pet}” property=“name” value=“Fluffy” /> <c:set target=“${pet}” property=“species” value=“cat” />
Automatic Type Conversions • Values returned by <jsp:getProperty> <c:out> or an EL expression are always converted to String. • Bean properties can be of any Java type • When using <jsp:setProperty> or <c:set>, the container takes care automatically of the conversion from text to the appropriate type for certain types
Loading JSP Parameters into a Bean • Create a HTML Form where the input items have the same name as properties of the Bean • in the JSP use the following action tag: <jsp:useBean id=“pet" class=“com.petstore.bean.PetBean"> <jsp:setProperty name=“pet" property="*" /> </jsp:useBean>
Inserting Data from a Bean into a Database • add a property called save with boolean value to the bean • code the setSave() setter method to perform an insert into the database when the value is set to true. • set the value of the property to true