Java Beans
In this part of the JEE tutorials, we will talk about client side Java Beans components. This is a technology directly supported by the JavaServer pages.Java Beans are reusable software components. The idea behind a software component is to create a specialized piece of self-contained code, that could be easily plugged into various applications as needed. For example in GUI programming, we might have a chart, a clock or a spreadsheet component that could be used in an application without exposing the programmer to complicated details that are behind the code.
Technically Java Beans are Java classes conforming to particular conventions. A bean can be a particular specialized Java Swing component (e.g. a chart ) that can be plugged into the application, a server side component called Enterprise Java Bean, EJB or a client side component. In this chapter, we will talk about client side Java Beans.
- The class must have a no-argument public constructor
- The properties of the Bean must be accessible using accessor methods
- The class should be serializable
A Bean
In the Bean example we have a form that sends data to a jsp page. The JSP page will output those data. This time we do not use scriptlets or expressions, but we use Java Beans technology.style.css
* { font-size: 12px; font-family: Verdana }This is a css file for the code example.
input { border: 1px solid #ccc }
index.css
<%@page contentType="text/html" pageEncoding="UTF-8"%>Here we define a simple form. We have three input boxes. The author, title and the availability of the book. The parameters are sent to the show.jsp page.
<html>
<head>
<title>Bean</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<form action="show.jsp" method="post">
<table>
<tr>
<td>Author</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>Title</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>Available</td>
<td><input type="checkbox" name="available" value="true"></td>
</tr>
</table>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
MyBean.java
package com.zetcode;This is our bean. It is a simple java class. Has no argument constructor. It implements the
import java.io.Serializable;
public class MyBean implements Serializable {
private String author = "";
private String title = "";
private String available = "";
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAvailable() {
return available;
}
public void setAvailable(String available) {
this.available = available;
}
}
Serializable
interface. We have three properties of a Bean. Each of the property names begins with small letter. Each of the accessor methods is public. The properties are private. The accessor methods consists of two parts. The first part begins with get, set or is and the second part is the name of the property with first letter capitalized. show.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>The show.jsp page sets the parameters into the bean and prints them.
<html>
<head>
<title>Show</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<jsp:useBean id="MyBean" class="com.zetcode.MyBean" scope="page">
<jsp:setProperty name="MyBean" property="author" param="author" />
<jsp:setProperty name="MyBean" property="title" param="title" />
<jsp:setProperty name="MyBean" property="available" param="available" />
</jsp:useBean>
<jsp:getProperty name="MyBean" property="author"/><br>
<jsp:getProperty name="MyBean" property="title"/><br>
<jsp:getProperty name="MyBean" property="available"/>
</body>
</html>
<jsp:useBean id="MyBean" class="com.zetcode.MyBean" scope="page">We use this element to declare that our JSP page will use a bean. The id parameter identifies the bean. The class parameter is a fully clasified classname. The scope parameters sets the bean validity for this page only.
...
</jsp:useBean>
<jsp:getProperty name="MyBean" property="author"/><br>This element retrieves the author property from the bean.
Another Bean
Next we modify our previous example a bit.show.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>We slightly change the show.jsp page.
<html>
<head>
<title>Show</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<jsp:useBean id="MyBean" class="com.zetcode.MyBean" scope="page">
<jsp:setProperty name="MyBean" property="*"/>
</jsp:useBean>
<jsp:getProperty name="MyBean" property="author"/><br>
<jsp:getProperty name="MyBean" property="title"/><br>
<jsp:getProperty name="MyBean" property="available"/>
</body>
</html>
<jsp:setProperty name="MyBean" property="*"/>This element will automatically fill the bean properties with the request parameters. This works only if the parameter names match the bean property names.
In this chapter we have briefly mentioned Java Beans.
0 comments:
Post a Comment