Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSP Action Elements

JSP Action Elements

Unlike JSP directive elements, JSP action elements work during the request processing phase. JSP action elements are written in XML syntax.

JSP actions can be used to dynamically insert files, reuse JavaBean components, redirect users to another page, and generate HTML code for Java plugins.

Action elements have only one syntax, which conforms to the XML standard:





Action elements are basically predefined functions. The JSP specification defines a series of standard actions, prefixed with jsp. The available standard action elements are as follows:

Syntax Description
jsp:include Includes a file when the page is requested.
jsp:useBean Finds or instantiates a JavaBean.
jsp:setProperty Sets the properties of a JavaBean.
jsp:getProperty Outputs the property of a JavaBean.
jsp:forward Forwards the request to a new page.
jsp:plugin Generates an OBJECT or EMBED tag for a Java plugin based on the browser type.
jsp:element Defines a dynamic XML element
jsp:attribute Sets the attributes of a dynamically defined XML element.
jsp:body Sets the content of a dynamically defined XML element.
jsp:text Uses a template for writing text in JSP pages and documents

Common Attributes

All action elements have two attributes: the id attribute and the scope attribute.


jsp:includeAction Elements

jsp:includeThe action element is used to include static and dynamic files. This action inserts the specified file into the page being generated. The syntax is as follows:





 The include directive was introduced earlier; it includes a file when the JSP file is converted into a Servlet. The jsp:include action here is different: the file is inserted when the page is requested.

The following is a list of attributes related to the include action.

property Description
page The relative URL address of the page to include.
flush A boolean attribute that defines whether to flush the buffer before including the resource.

Example

Below we define two files date.jsp and main.jsp, as shown in the code below:

The code of the date.jsp file:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

今天的日期是: <%= (new java.util.Date()).toLocaleString()%>

The code of the main.jsp file:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>









Runoob Tutorial(runoob.com)







include 动作实例

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



include 动作实例



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


jsp:useBeanAction Elements

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

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

The simplest syntax of the jsp:useBean action is:





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

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

property Description
class Specifies the full package name of the Bean.
type Specifies the type of the variable that will reference the object.
beanName Specifies the name of the Bean through the instantiate() method of java.beans.Beans.

Before giving a concrete example, let's first look at the jsp:setProperty and jsp:getProperty action elements:


jsp:setPropertyAction Elements

jsp:setProperty is used to set the properties of an already instantiated Bean object. There are two ways to use it. First, you can use jsp:setProperty outside (after) the jsp:useBean element, as follows:





...



In this case, jsp:setProperty executes whether jsp:useBean finds an existing Bean or creates a new Bean instance. The second way is to place jsp:setProperty inside the jsp:useBean element, as follows:





...

   



In this case, jsp:setProperty executes only when a new Bean instance is created; if an existing instance is used, jsp:setProperty is not executed.

The jsp:setProperty action has the following four attributes, as shown in the table:

property Description
name The name attribute is required. It indicates which Bean's properties are to be set.
property The property attribute is required. It indicates which property to set. There is a special usage: if the value of property is "*", all request parameters whose names match the Bean property names will be passed to the corresponding property set methods.
value The value attribute is optional. It is used to specify the value of the Bean property. String data is automatically converted to numbers, boolean, Boolean, byte, Byte, char, and Character in the target class through the standard valueOf methods. For example, property values of type boolean and Boolean (such as "true") are converted by Boolean.valueOf, and property values of type int and Integer (such as "42") are converted by Integer.valueOf. value and param cannot be used together, but either one can be used.
param param is optional. It specifies which request parameter to use as the value of the Bean property. If the current request has no parameter, nothing happens; the system does not pass null to the Bean property's set method. Therefore, you can let the Bean provide its own default property values, and the default values are only modified when a request parameter explicitly specifies a new value.

jsp:getPropertyAction Elements

The jsp:getProperty action extracts the value of a specified Bean property, converts it to a string, and outputs it. The syntax is as follows:





...



The following table lists the attributes related to getProperty:

property Description
name The name of the Bean property to retrieve. The Bean must already be defined.
property Indicates the value of the Bean property to extract

Example

In the following example we use a Bean:



package com.runoob.main;



public class TestBean {

   private String message = "Runoob Tutorial";

 

   public String getMessage() {

      return(message);

   }

   public void setMessage(String message) {

      this.message = message;

   }

}

Compile the above example file TestBean.java:



$ javac TestBean.java

After compilation, a file named TestBean.class is generated in the current directory. Copy this file to the WebContent/WEB-INF/classes/com/runoob/main of the current JSP project (the com/runoob/main package path; create it manually if it does not exist).

The following is a directory structure diagram in Eclipse:

The following is a very simple example whose function is to load a Bean, then set/read its message property.

Now let's call this Bean in the main.jsp file:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>









Runoob Tutorial(runoob.com)







Jsp 使用 JavaBean 实例

输出信息....

Access in the browser, execute the above file, and the output is as follows:


jsp:forward Action Elements

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





The following are attributes related to forward:

property Description
page The page attribute contains a relative URL. The value of page can be given directly or computed dynamically at request time; it can be a JSP page or a Java Servlet.

Example

In the following example we use two files: date.jsp and main.jsp.

The code of the date.jsp file is as follows:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

今天的日期是: <%= (new java.util.Date()).toLocaleString()%>

The code of the main.jsp file:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>









Runoob Tutorial(runoob.com)







forward 动作实例

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



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


jsp:pluginAction Elements

The jsp:plugin action is used to insert the OBJECT or EMBED elements necessary to run a Java Applet through a Java plugin, depending on the browser type.

If the required plugin does not exist, it downloads the plugin and then executes the Java component. The Java component can be an applet or a JavaBean.

The plugin action has several attributes corresponding to HTML elements for formatting the Java component. The param element can be used to pass parameters to the Applet or Bean.

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





   

   

 

   

      Unable to initialize Java Plugin

   

 



If you are interested, you can try using an applet to test the jsp:plugin action element.element is a new element that sends an error message to the user when a component fails.


jsp:element 、 jsp:attribute、 jsp:bodyAction Elements

jsp:element 、 jsp:attribute、 jsp:bodyThe action element dynamically defines an XML element. Being dynamic is important; it means the XML element is generated dynamically at compile time rather than statically.

The following example dynamically defines an XML element:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>









Runoob Tutorial(runoob.com)









   属性值





   XML 元素的主体









Access the following page in the browser, and the output is as follows:


jsp:textAction Elements

jsp:textThe action element allows the use of a template for writing text in JSP pages and documents. The syntax is as follows:



模板数据

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





If you need to declare a DOCTYPE in XHTML, you must use thejsp:textaction element. The example is as follows:







jsp:text action





  

    Welcome to JSP Programming









You can try usingjsp:textand not using this action element on the above example to see the difference in the results.