13 JavaBeans
13 JavaBeans
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Taught by the author of Core Servlets and JSP, More Servlets and JSP, and this tutorial. Available at public venues, or customized versions can be held on-site at your Customized Java EE Training: http://courses.coreservlets.com/ Java, JSF 2, PrimeFaces, Servlets, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. organization. Contact [email protected] for details. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Agenda
Understanding the benefits of beans
We will use standalone beans here. Later sections will cover beans with MVC and the JSP expression language.
Creating beans Installing bean classes on your server Accessing bean properties Explicitly setting bean properties Automatically setting bean properties from request parameters Sharing beans among multiple servlets and JSP pages
5
Persistent values should be accessed through methods called getXxx and setXxx
If class has method getTitle that returns a String, class is said to have a String property named title Boolean properties may use isXxx instead of getXxx It is the name of the method, not instance var that matters!
executive
executive
ZIP
Note 1: property name does not exist anywhere in your code. It is just a shortcut for the method name. Note 2: property name is derived only from method name. Instance variable name is irrelevant. 9
Right
private double speed; // Var need not match method name public double getSpeed() { return(speed); } public void setSpeed(double speed) { this.speed = speed; Note: in Eclipse, after you create instance variable, if } you R-click and choose Source, it gives you option to
generate getters and setters for you.
OOP design
10
If users of your class accessed the fields directly, then they would each be responsible for checking constraints.
11
12
If users of your class accessed the fields directly, then they would each be responsible for executing side effects. Too much work and runs huge risk of having display inconsistent from actual values.
13
Bottom Line
It is no onerous requirement to be a bean
You are probably following most of the conventions already anyhow
Zero arg constructor (not required in MVC!) No public instance variables Use getBlah/setBlah or isBlah/setBlah naming conventions
jsp:setProperty
This element modifies a bean property (i.e., calls a setBlah method). It is normally used as follows:
<jsp:setProperty name="beanName" property="propertyName" value="propertyValue" />
jsp:getProperty
This element reads and outputs the value of a bean property. It is used as follows:
<jsp:getProperty name="beanName" property="propertyName" />
15
JSP Page
JSP page instantiates a bean
<jsp:useBean id="myBean" class=""/>
Purpose
Allow instantiation of Java classes without explicit Java programming (XML-compatible syntax)
Notes
Simple interpretation: <jsp:useBean id="book1" class="coreservlets.Book" /> can be thought of as equivalent to the scriptlet <% coreservlets.Book book1 = new coreservlets.Book(); %>
Purpose
Allow setting of bean properties (i.e., calls to setXxx methods) without explicit Java programming
Notes
<jsp:setProperty name="book1" property="title" value="Core Servlets and JavaServer Pages" /> <% book1.setTitle("Core Servlets and JavaServer Pages"); %>
18
Purpose
Allow access to bean properties (i.e., calls to getXxx methods) without explicit Java programming
Notes
<jsp:getProperty name="book1" property="title" /> is equivalent to the following JSP expression <%= book1.getTitle() %>
19
Example: StringBean
package coreservlets; public class StringBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; }
22
23
Bad: violates rule of not having scripting in pages that use jsp:useBean, jsp:getProperty, and jsp:setProperty.
24
Even worse than previous slide: not only has scripting, but has long ugly sections of Java code.
26
27
29
This is extremely convenient for making form beans -- objects whose properties are filled in from a form submission.
You can even divide the process up across multiple forms, where each submission fills in part of the object.
30
Sharing Beans
You can use the scope attribute to specify additional places where bean is stored
Still also bound to local variable in _jspService <jsp:useBean id="" class="" scope="" />
Benefits
Lets multiple servlets or JSP pages share data Also permits conditional bean creation
Creates new object only if it can't find existing one
31
page2.jsp
<jsp:useBean id="foo" class="" scope="application"/> <jsp:getProperty name="foo" property="message"/>
Possible scenario 1
Joe goes to page 2 (output is "Default Message") Jane goes to page 1 (output is "Hello")
Possible scenario 2
Joe goes to page 1 (output is "Hello") Jane goes to page 2 (output is "Hello")
32
34
Important:
Use different names (i.e., id in jsp:useBean) for different beans
35
36
37
39
40
42
43
44
45
47
48
49
50
52
53
54
55
}
57
59
Summary
Benefits of jsp:useBean
Hides the Java syntax. No scripting in these pages! Makes it easier to associate request parameters with Java objects (bean properties) Simplifies sharing objects among multiple requests or servlets/JSPs
jsp:useBean
Creates or accesses a bean
jsp:setProperty
Sets bean property (i.e. passes value to setXxx)
You usually use property="*" to pass in request params
jsp:getProperty
60
Questions?
JSF 2, PrimeFaces, Java 7, Ajax, jQuery, Hadoop, RESTful Web Services, Android, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training
Developed and taught by well-known author and developer. At public venues or onsite at your location.