Home  >  Article  >  Java  >  How to introduce beans using jsp

How to introduce beans using jsp

(*-*)浩
(*-*)浩Original
2019-05-18 15:38:535018browse

Using Javabeans in jsp pages mainly involves three jsp action elements: , and .

How to introduce beans using jsp

(1) Instantiate Javabean. The jsp action element is used to instantiate a Javabean component in the jsp page. This instantiated Javabean component object can be called elsewhere in the jsp page. The basic syntax of

is as follows:

<pre class="brush:php;toolbar:false"><jsp:useBean id="name" scope="page|request|session|application" class="className"/><pre class="brush:php;toolbar:false">

The id attribute is used to set the name of the Javabean. The id can be used to identify the same jsp page. For different JavaBean component instances used, the class attribute specifies the path for the jsp engine to search for Javabean bytecode, which is generally the Javabean class name corresponding to this Javabean. For example: com.company.UserEntity; the scope attribute is used to specify the life cycle of the Javabean instance object and is also the effective scope of the Javabean. The value of scope can be page, request, session and application.

For example:

<jsp:useBean id="user2" scope="page" class="com.communal.UserEntity"></jsp:useBean>

id="user2" is the name or identifier of the specified Javabean, used for the name of the class instance, scope="page" represents the Javabean Scope, page indicates that it is only available within the scope of this jsp page, class="com.communal.UserEntity" indicates the class name of the Javabean

(2) Accessing the properties of the Javabean. After using in the jsp page to instantiate the Javabean component object, you can access its properties by using the action elements and

<jsp:setProperty property="propertyName" name="name" value=“string”/>

name is used to specify the name of the Javabean. This Javabean must first be instantiated using , and its value should be the same as the value of the id attribute in the operation. The property attribute is used to specify the name of the property that the Javabean needs to set. The value attribute is the attribute value to be assigned to the Javabean with the name specified by property.

<jsp:useBean id="user2" scope="session" class="com.communal.UserEntity"></jsp:useBean>
<jsp:setProperty property="*" name="user2"/>

This is a powerful function of Javabeans: when the value of the property attribute of the action element is "*", it means that the jsp engine is expected to automatically match user request parameters with Javabeans. When the parameter name requested by the user matches the property name of the Javabean, the property assignment is automatically completed. If there is a null value in the parameter value of the request object, the corresponding Javabean property will not have any value set. Similarly, if there is a property in the Javabean that does not have a corresponding request parameter value, then this property will not be set.

operation is used together with the operation to obtain the property value of a Javabean component object and use the output method to output this value to the page.

The syntax format of the action is as follows:

<jsp:getProperty property="propertyName" name="BeanName" />

Among them, name specifies the name of the Javabean. It should be noted that the Javabean component object specified by name must have been used. Instantiation; property: used to specify the name of the property of the Javabean component object to be read.

Javabeans exist on the server as instance objects of a certain class, so using the action is equivalent to directly using the getXxx method of the object to obtain the property value, such as:

<%=beanName.getPropertyName()%>

The above is the detailed content of How to introduce beans using jsp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn