Java EE Java Beans

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
(Новая: 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 softw...)
(нарушало авторские права)
 
Строка 1: Строка 1:
-
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 <b>Enterprise Java Bean</b>, 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
 
-
 
-
JavaServer Pages technology directly supports using JavaBeans components with standard JSP language elements.
 
-
 
-
== 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.
 
-
<source lang="java">
 
-
* { font-size: 12px; font-family: Verdana }
 
-
 
-
input { border: 1px solid #ccc }
 
-
</source>
 
-
 
-
This is a css file for the code example.
 
-
 
-
<source lang="java">
 
-
 
-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
 
-
 
-
<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>
 
-
</source>
 
-
 
-
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.
 
-
 
-
<source lang="java">
 
-
package com.zetcode;
 
-
 
-
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;
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
This is our bean. It is a simple java class. Has no argument constructor. It implements the <b>Serializable</b> 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.
 
-
 
-
<source lang="java">
 
-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
 
-
 
-
<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>
 
-
</source>
 
-
 
-
The show.jsp page sets the parameters into the bean and prints them.
 
-
 
-
<source lang="java">
 
-
<jsp:useBean id="MyBean" class="com.zetcode.MyBean" scope="page">
 
-
...
 
-
</jsp:useBean>
 
-
</source>
 
-
 
-
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.
 
-
 
-
<source lang="java">
 
-
<jsp:getProperty name="MyBean" property="author"/><br>
 
-
</source>
 
-
 
-
This element retrieves the author property from the bean.
 
-
 
-
== Another Bean ==
 
-
Next we modify our previous example a bit.
 
-
<source lang="java">
 
-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
 
-
 
-
<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>
 
-
</source>
 
-
 
-
We slightly change the show.jsp page.
 
-
 
-
<source lang="java">
 
-
 
-
<jsp:setProperty name="MyBean" property="*"/>
 
-
</source>
 
-
 
-
This element will automatically fill the bean properties with the request parameters. This works only if the parameter names match the bean property names.
 
-
 
-
[[Категория:Java]]
 

Текущая версия на 11:37, 7 апреля 2009