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:

##SyntaxDescriptionjsp:includeIntroduce a file when the page is requested. jsp:useBeanFind or instantiate a JavaBean. jsp:setPropertySet the properties of JavaBean. jsp:getPropertyOutput the properties of a JavaBean. jsp:forwardForward the request to a new page. jsp:pluginGenerates OBJECT or EMBED tags for Java plug-ins depending on the browser type. jsp:elementDefine dynamic XML elementjsp:attributeSet dynamically defined XML element attributes. jsp:bodySet the dynamically defined XML element content. jsp:textUse templates for writing text in JSP pages and documents

Common attributes

All 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>Action element

<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.

Attributepageflush

Example

Below we define two files date.jsp and main.jsp, the code is as follows:

date. jsp file code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<p>
   今天的日期是: <%= (new java.util.Date()).toLocaleString()%>
</p>

main.jsp file code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<h2>include 动作实例</h2>
<jsp:include page="date.jsp" flush="true" />

</body>
</html>

Now place the above two files in the root directory of the server and access the main.jsp file. The display result is as follows:

include 动作实例

今天的日期是: 2016-6-25 14:08:17

<jsp:useBean>Action element

jsp:useBean The action is used to load a JavaBean that will be used in the JSP page .

This feature is very useful because it allows us to take advantage of Java component reuse.

The simplest syntax of the jsp:useBean action is:

<jsp:useBean id="name" class="package.class" />

After the class is loaded, we can modify and retrieve the properties of the bean through the jsp:setProperty and jsp:getProperty actions.

The following is a list of properties related to the useBean action.

Description
The relative URL address contained in the page .
Boolean attribute that defines whether to flush the cache before including the resource.
AttributeDescription
classSpecifies the complete package name of the Bean.
typeSpecifies the type that will reference the object variable.
beanNameSpecify 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:

##Attribute Descriptionname The name attribute is required. It indicates which Bean the property is to be set on. propertyThe 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. valueThe 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. paramparam 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 element

jsp: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:

AttributeDescriptionnameThe name of the Bean property to be retrieved. Bean must be defined. propertyIndicates the value of the Bean property to be extracted

Example

We used Bean in the following example:

package com.php.main;

public class TestBean {
   private String message = "php中文网";
 
   public String getMessage() {
      return(message);
   }
   public void setMessage(String message) {
      this.message = message;
   }
}

Compile the above example file TestBean.java:

$ javac TestBean.java

After the compilation is completed, a # will be generated in the current directory. ##TestBean.class file, Copy this file to WebContent/WEB-INF/classes/com/php/main of the current JSP project (com/php/main package path, no need to create it manually).

The following is a directory structure diagram in Eclipse:

6AC33FBA-0B76-4BFD-A690-E856E9E01900.jpg

The following is a very simple example. Its function is to load a Bean and then set/read Get its message attribute.


Now let us call the Bean in the main.jsp file:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<h2>Jsp 使用 JavaBean 实例</h2>
<jsp:useBean id="test" class="com.php.main.TestBean" />
 
<jsp:setProperty name="test" 
                    property="message" 
                    value="php中文网..." />
 
<p>输出信息....</p>
 
<jsp:getProperty name="test" property="message" />

</body>
</html>

Browser access, execute the above file, the output is as follows:

D7AD87A8-3392-4D4E-8731-18806B0644CD.jpg


<jsp:forward> Action element

The jsp:forward action transfers the request to another page. The jsp:forward tag has only one attribute, page. The syntax format is as follows:

<jsp:forward page="相对 URL 地址" />

The following are the attributes associated with forward:

AttributeDescriptionpageThe 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.

Example

We use two files in the following example: date.jsp and main.jsp.

date.jsp file code is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<p>
   今天的日期是: <%= (new java.util.Date()).toLocaleString()%>
</p>

main.jsp file code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<h2>forward 动作实例</h2>
<jsp:forward page="date.jsp" />
</body>
</html>

Now place the above two files in the root directory of the server and access main. jsp file. The display result is as follows:

今天的日期是: 2016-6-25 14:37:25

<jsp:plugin>Action element

jsp:plugin action is used to insert the necessary elements for running Java Applet through Java plug-in according to the type of browser. OBJECT or EMBED element.

If the required plug-in does not exist, it will download the plug-in and then execute the Java component. A Java component can be an applet or a JavaBean.

The plugin action has multiple attributes corresponding to HTML elements for formatting Java components. The param element can be used to pass parameters to an Applet or Bean.

The following is a typical example of using the plugin action element:

<jsp:plugin type="applet" codebase="dirname" code="MyApplet.class"
                           width="60" height="80">
   <jsp:param name="fontcolor" value="red" />
   <jsp:param name="background" value="black" />
 
   <jsp:fallback>
      Unable to initialize Java Plugin
   </jsp:fallback>
 
</jsp:plugin>

If you are interested, you can try to use applet to test the jsp:plugin action element. The <fallback> element is a new element. Component failure errors are error messages sent to the user.


<jsp:element> , <jsp:attribute>、 <jsp:body>Action element

<jsp:element> , <jsp:attribute>、 <jsp:body>The action element dynamically defines the XML element. Dynamic is very important, which means that XML elements are dynamically generated at compile time rather than static.

The following example dynamically defines XML elements:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<jsp:element name="xmlElement">
<jsp:attribute name="xmlElementAttr">
   属性值
</jsp:attribute>
<jsp:body>
   XML 元素的主体
</jsp:body>
</jsp:element>
</body>
</html>

The browser accesses the following page, and the output result is as follows:

7D8C47F0-0DDE-4F1D-8BE1-B2C9C955683E.jpg


<jsp:text>Action element

<jsp:text>The action element allows the use of templates for writing text in JSP pages and documents. The syntax format is as follows:

<jsp:text>模板数据</jsp:text>

The above text The template cannot contain other elements, but can only contain text and EL expressions (Note: EL expressions will be introduced in subsequent chapters). Please note that in XML files you cannot use expressions such as ${whatever > 0} because the > symbol is illegal. You can use the ${whatever gt 0} expression or embed the value in a CDATA section.

<jsp:text><![CDATA[<br>]]></jsp:text>

If you need to declare DOCTYPE in XHTML, you must use the <jsp:text> action element. The example is as follows:

<jsp:text><![CDATA[<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">]]>
</jsp:text>
<head><title>jsp:text action</title></head>
<body>

<books><book><jsp:text>  
    Welcome to JSP Programming
</jsp:text></book></books>

</body>
</html>

You can try to use <jsp:text> for the above example ;The difference between execution results without using this action element.