1 / 14

Java Beans

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

faolan
Télécharger la présentation

Java Beans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Beans Celsina Bignoli bignolic@smccd.net

  2. 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

  3. 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

  4. 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)

  5. 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

  6. 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!

  7. 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>

  8. 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>

  9. 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” />

  10. 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” />

  11. 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

  12. Automatic Type Conversions

  13. 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>

  14. 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

More Related