HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
JavaBean is a special Java class, using the J ava language to write, and to comply with JavaBean API specification.
The next JavaBean is given in terms of unique features compared to other Java class:
A JavaBean property object should be accessible. This property can be any valid Java data types, including custom Java classes.
A JavaBean property object may be read-write or read-only or write-only. JavaBean JavaBean property objects via classes implement two methods provide access to:
method | description |
---|---|
get PropertyName () | For example, if the name attribute is myName, then the name of this method must be written getMyName () to read this property. This method is also called an access device. |
set PropertyName () | For example, if the name attribute is myName, then the name of this method must be written setMyName () to write to this property. This method is also known writer. |
A read-only attribute getPropertyName () method, a write-only property only setPropertyName () method.
This is StudentBean.java file:
package com.w3resource; public class StudentsBean implements java.io.Serializable { private String firstName = null; private String lastName = null; private int age = 0; public StudentsBean() { } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public void setFirstName(String firstName){ this.firstName = firstName; } public void setLastName(String lastName){ this.lastName = lastName; } public void setAge(int age) { this.age = age; } }
Compile StudentBean.java file (last instance will be used):
$ javac StudentsBean.java
Compile get StudentBean.class file, copy it to the <JSP project> / WebContent / WEB-INF / classes / com / w3resource, as shown below:
<Jsp: useBean> tag can be declared a JavaBean in JSP, and then used. After the declaration, JavaBean objects became script variables can be accessed through scripting elements or other custom labels. <Jsp: useBean> tag syntax is as follows:
<jsp:useBean id="bean 的名字" scope="bean 的作用域" typeSpec/>
Wherein, depending on the circumstances, the value of scope can be page, request, session or application. id can be any value as long as no other files and the same JSP <jsp: useBean> id value in the same line.
Next, given that <jsp: useBean> tag is a simple usage:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <title>useBean 实例</title> </head> <body> <jsp:useBean id="date" class="java.util.Date" /> <p>日期为:<%= date %> </body> </html>
It will produce the following results:
日期为:Tue Jun 28 15:22:24 CST 2016
In the <jsp: useBean> tag body using <jsp: getProperty /> tag to invoke the getter method, use <jsp: setProperty /> tag to invoke the setter method syntax is as follows:
<jsp:useBean id="id" class="bean 编译的类" scope="bean 作用域"> <jsp:setProperty name="bean 的 id" property="属性名" value="value"/> <jsp:getProperty name="bean 的 id" property="属性名"/> /en/en/en/en/en. </jsp:useBean>
name attribute refers to the id attribute of the Bean. property attribute refers to the getter or setter method you want to call.
Next, using the above syntax is given access to the property with a simple example:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <title>get 和 set 属性实例</title> </head> <body> <jsp:useBean id="students" class="com.w3resource.StudentsBean"> <jsp:setProperty name="students" property="firstName" value="小强"/> <jsp:setProperty name="students" property="lastName" value="王"/> <jsp:setProperty name="students" property="age" value="10"/> </jsp:useBean> <p>学生名字: <jsp:getProperty name="students" property="firstName"/> </p> <p>学生姓氏: <jsp:getProperty name="students" property="lastName"/> </p> <p>学生年龄: <jsp:getProperty name="students" property="age"/> </p> </body> </html>
Access over JSP, results are as follows:
学生名字: 小强 学生姓氏: 王 学生年龄: 10