Brief introduction
JavaBeans is a Java class that conforms to a particular specification. The advantage of using JavaBeans is to solve the code duplication, the function is clear, improve the maintainability of the code.
is an ordinary Java class, but conforms to a specific specification.
Follow a design principle
Is the common sense of the physical class
Example:
JSP actions
The JSP action element provides information for the request processing phase. The action element follows the syntax of the XML element, with a start tag that contains the element name, an end tag that can have attributes, optional content, and matches the start tag.
Contains the following categories, the first class of three tags, focus!
- <jsp:userBean>
- <jsp:setProperty>
- <jsp:getProperty>
JavaBeans are usually used in JSP pages.
Two ways of using JavaBeans
The first way
After creating the JavaBeans class in the SRC directory, and then in the JSP page,
Import class
<%@ page import= "The package name where the user class is located"%>
After importing the package, add the following to the HTML tag:
There is no difference between creating a Java class with normal.
The second method of creating JavaBeans
JSP action tags are commonly used in JSP pages to use JavaBean
- Usebeans Action
- SetProperty action
- GetProperty Action
<jsp:useBeans>
Function: Instantiate in a JSP page or use JavaBean within a specified range
<jsp:usebean id= "" class= "Java class name" scope= "Scope"/>
Instead of importing the package and then creating the corresponding instance object, you can use the
After running,
Because we are not initialized, all are null
So how do you initialize it, using setProperty
<jsp:setProperty>
Function: Assign a value to the attribute of an JavaBean object that has already been instantiated, in a total of four different forms.
The first way to match all of the properties based on the form
The name here is the ID of the JavaBeans, and the * number is based on the form to match.
In other words, when the user fills out the form, for example, the username column is Wu, then it automatically corresponds to the username attribute in the JavaBeans class.
The second method is used when the attribute part matches
This time only matches the username, and there is no match password
The third way to manually assign values is not related to what the form fills out.
The fourth kind does not introduce, the feeling does not use.
GetProperty
Function: Gets the property value of the specified JavaBean object.
With GetProperty, you don't need to use this method to get property values.
To get Properties using GetProperty:
JavaBeans Four Scope scopes
......
Without JavaBeans, the JSP page interacts directly with the data layer, which makes the maintainability of the code very poor, and the presence of a large number of business logic code in the JSP is not good.
Introduction and use of JavaBeans