JSP action elements
Different from JSP instruction elements, JSP action elements work in the request processing phase. JSP action elements are written in XML syntax.
Use JSP actions to dynamically insert files, reuse JavaBean components, redirect users to other pages, and generate HTML code for Java plug-ins.
The action element has only one syntax, which conforms to the XML standard:
<jsp:action_name attribute="value" />
The action elements are basically predefined functions. The JSP specification defines a series of standard actions, which uses JSP as Prefix, the available standard action elements are as follows:
Description | |
---|---|
Introduce a file when the page is requested. | |
Find or instantiate a JavaBean. | |
Set the properties of JavaBean. | |
Output the properties of a JavaBean. | |
Forward the request to a new page. | |
Generates OBJECT or EMBED tags for Java plug-ins depending on the browser type. | |
Define dynamic XML element | |
Set dynamically defined XML element attributes. | |
Set the dynamically defined XML element content. | |
Use templates for writing text in JSP pages and documents |
Common attributesAll action elements have two attributes: id attribute and scope attribute.
id attribute:
The id attribute is the unique identifier of the action element and can be referenced in the JSP page. The id value created by the action element can be called through PageContext.scope attribute:
This attribute is used to identify the life cycle of the action element. The id attribute is directly related to the scope attribute, and the scope attribute defines the lifespan of the associated id object. The scope attribute has four possible values: (a) page, (b) request, (c) session, and (d) application.
<jsp:include>The action element is used to include static and dynamic files. This action inserts the specified file into the page being generated. The syntax format is as follows:
<jsp:include page="相对 URL 地址" flush="true" />
The include directive has been introduced before. It introduces files when the JSP file is converted into a Servlet. The jsp:include action here is different. The time when the file is inserted is when the page is requested. when.
The following is a list of attributes related to the include action.
Description | |
---|---|
The relative URL address contained in the page . | |
Boolean attribute that defines whether to flush the cache before including the resource. |
Attribute | Description |
---|---|
class | Specifies the complete package name of the Bean. |
type | Specifies the type that will reference the object variable. |
beanName | Specify the name of the Bean through the instantiate() method of java.beans.Beans. |
Before giving specific examples, let us first look at the jsp:setProperty and jsp:getProperty action elements:
<jsp:setProperty>action element
jsp :setProperty is used to set the properties of the instantiated Bean object. There are two ways to use it. First, you can use jsp:setProperty outside (behind) the jsp:useBean element, as shown below:
<jsp:useBean id="myName" ... /> ... <jsp:setProperty name="myName" property="someProperty" .../>
At this time, no matter whether jsp:useBean finds an existing Bean or creates a new one For a Bean instance, jsp:setProperty will be executed. The second usage is to put jsp:setProperty inside the jsp:useBean element, as shown below:
<jsp:useBean id="myName" ... > ... <jsp:setProperty name="myName" property="someProperty" .../> </jsp:useBean>
At this time, jsp:setProperty will only be executed when creating a new Bean instance. If you use an existing Instances do not execute jsp:setProperty.
jsp:setProperty action has the following four attributes, as shown in the following table:
Description | |
---|---|
The name attribute is required. It indicates which Bean the property is to be set on. | |
The property property is required. It indicates which property to set. There is a special usage: if the value of property is "*", it means that all request parameters whose names match the Bean property names will be passed to the corresponding property set method. | |
The value attribute is optional. This property is used to specify the value of the Bean property. String data will be automatically converted into numbers, boolean, Boolean, byte, Byte, char, and Character through the standard valueOf method in the target class. For example, boolean and Boolean type attribute values (such as "true") are converted by Boolean.valueOf, and int and Integer type attribute values (such as "42") are converted by Integer.valueOf. Value and param cannot be used at the same time, but either one can be used. | |
param is optional. It specifies which request parameter is used as the value of the Bean property. If the current request has no parameters, nothing will be done, and the system will not pass null to the set method of the Bean property. Therefore, you can let the bean provide the default property value itself, and only modify the default property value when the request parameter explicitly specifies a new value. |
<jsp:getProperty>Action elementjsp:getProperty action extracts the value of the specified Bean property, converts it into a string, and then outputs it . The syntax format is as follows:
<jsp:useBean id="myName" ... /> ... <jsp:getProperty name="myName" property="someProperty" .../>The following table is the properties associated with getProperty:
Description | |
---|---|
The name of the Bean property to be retrieved. Bean must be defined. | |
Indicates the value of the Bean property to be extracted |
Description | |
---|---|
The page attribute contains a relative URL. The value of page can be given directly or dynamically calculated during the request. It can be a JSP page or a Java Servlet. |