1 / 12

CS320 Web and Internet Programming Java Beans

CS320 Web and Internet Programming Java Beans. Chengyu Sun California State University, Los Angeles. Problems with Scripting Elements. Mixing presentation and processing hard to debug hard to maintain No clean and easy way to reuse code Solution – separate out Java code Bean

lilat
Download Presentation

CS320 Web and Internet Programming 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. CS320 Web and Internet ProgrammingJava Beans Chengyu Sun California State University, Los Angeles

  2. Problems with Scripting Elements • Mixing presentation and processing • hard to debug • hard to maintain • No clean and easy way to reuse code • Solution – separate out Java code • Bean • Custom taglib

  3. Java Beans • Initially designed for GUI builders • Beans support • Introspection • Customization • Events • Properties • Persistence

  4. Java Beans for Dummies • A zero-argument constructor • No public class variables • Property foo • read property foo: getFoo() or isFoo() • set property foo: setFoo()

  5. About Bean Properties • Property naming conventions • 1st letter is always in lower case • 1st letter must be capitalized in accessor (getter) and/or mutator (setter) • A property does not have to be a class variable • Property types • read-only property: only getter • write-only property: only setter • read/write property: both

  6. Bean Example • BGBean.java • package!

  7. jsp:useBean class id scope page (default) request session application jsp:getProperty name property jsp:setProperty name property value param Bean Tags and Attributes

  8. Bean in JSP Example • BGColor2.jsp <jsp:useBean id=“bg" class=“cs320.stu31.BGBean" /> <jsp:setProperty name=“bg” property=“r” value=“255” /> <jsp:setProperty name=“bg” property=“r” param=“r” /> <jsp:setProperty name=“bg” property=“r” /> <jsp:getProperty name=“bg” property=“r” />

  9. More About <jsp:setProperty> • Note the auto-conversion from parameter string to property type • e.g. “255”  255 • What if auto-conversion fails?? • What if a parameter is not specified??

  10. A Few More Ways to setProperty <%-- Use JSP expression for the value attribute --%> <jsp:setProperty name="fb" property="fname" value='<%= request.getParameter("fname") %>'/> <%-- Use <jsp:attribute> for value attribute --%> <jsp:setProperty name="fb" property="age"> <jsp:attribute name="value" trim="true"> <%= request.getParameter("age") %> </jsp:attribute> </jsp:setProperty> <%-- Set all properties --%> <jsp:setProperty name="fb" property="*"/>

  11. Example: Form Bean • Set all properties • And an easier way to “get property”: Expression Language First name: ${ fb.fname } Last name: ${ fb.lname }

  12. Scopes and Data Sharing • page scope – data is valid within current page • include • request scope – data is valid throughout the processing of the request • forward • session scope – data is valid throughout the session • redirect, multiple separate requests • application scope – data is valid throughout the life cycle of the web application

More Related