JSP Action Elements

Source: Internet
Author: User
Tags cdata

JspAction Elements

Unlike JSP directive elements, JSP action elements work in 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 additional pages, and generate HTML code for Java plugins.

The action element has only one syntax, which conforms to the XML standard:

<jsp:action_name attribute= "Value"/>

Action elements are basically predefined functions, the JSP specification defines a series of standard actions, which are prefixed with JSP, and the standard action elements available are as follows:

Grammar Description
Jsp:include A file is introduced when the page is requested.
Jsp:usebean Find or instantiate a JavaBean.
Jsp:setproperty Sets the properties of the JavaBean.
Jsp:getproperty Outputs the properties of a JavaBean.
Jsp:forward Move the request to a new page.
Jsp:plugin Generates an object or embed tag for the Java plug-in based on the browser type.
Jsp:element Defining dynamic XML Elements
Jsp:attribute Sets the dynamically defined XML element properties.
Jsp:body Sets the content of a dynamically defined XML element.
Jsp:text Using templates that write text in JSP pages and documents
Common Properties

All action elements have two attributes: The id attribute and the Scope property.

    • ID attribute:

      The id attribute is a unique identifier for an action element and can be referenced in a JSP page. The ID value created by the action element can be called through PageContext.

    • Scope Property:

      This property is used to identify the life cycle of an action element. The id attribute has a direct relationship to the scope property, and the scope property defines the lifetime of the associated ID object. The scope attribute has four possible values: (a) page, (b) request, (c) session, and (d) application.

<jsp:include> Action Elements

<jsp:include> action elements are used to contain static and dynamic files. This action inserts the specified file into the page that is being generated. The syntax format is as follows:

<jsp:include page= "Relative URL" flush= "true"/>

The include directive, which introduced the file when the JSP file was converted to a servlet, was introduced in the previous section, where the Jsp:include action was different and the time to insert the file was when the page was requested.

The following is a list of properties related to include actions.

Properties Description
Page The relative URL address that is contained in the page.
Flush Boolean property that defines whether to flush the buffer before the resource is included.
Instance

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

date.jsp File Code:

<p>   today's date: <%= (New Java.util.Date ()). toLocaleString ()%></p>

main.jsp File Code:

Now put the above two files in the root directory of the server to access the main.jsp file. The results appear as follows:

The Include action Exampletoday ' s date:12-sep-2013 14:54:22
<jsp:useBean> Action Elements

The Jsp:usebean action is used to load a javabean that will be used in a JSP page.

This feature is useful because it allows us to take advantage of the reuse of Java components, while also avoiding the convenience of losing the JSP that distinguishes it from the servlet.

The simplest syntax for the Jsp:usebean action is:

<jsp:usebean id= "name" class= "Package.class"/>

After the class is loaded, we can modify and retrieve the Bean's properties through both Jsp:setproperty and jsp:getproperty actions.

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

Properties Description
Class Specifies the full package name of the Bean.
Type Specifies the type of the object variable that will be referenced.
Beanname The name of the bean is specified by Java.beans.Beans's instantiate () method.

Before giving a specific example, let's take a look at the Jsp:setproperty and Jsp:getproperty action elements:

<jsp:setProperty> Action Elements

Jsp:setproperty is used to set properties of a bean object that has already been instantiated, in two ways. First, you can use jsp:setproperty outside of the Jsp:usebean element (behind), as follows:

<jsp:usebean id= "MyName" .../>...<jsp:setproperty name= "MyName" property= "Someproperty" .../>

At this point, Jsp:setproperty executes regardless of whether Jsp:usebean has found an existing bean, or if a new bean instance has been created. The second use is to put the jsp:setproperty inside the Jsp:usebean element, as follows:

<jsp:usebean id= "MyName"   ... <jsp:setproperty name= "MyName" property= "Someproperty" .../></jsp:usebean>

At this point, Jsp:setproperty is only executed when a new bean instance is created and Jsp:setproperty is not executed if an existing instance is used.

Properties Description
Name The Name property is required. It represents which bean to set the property to.
Property The property attribute is required. that represents which property to set. There is a special usage: if the value of the property is "*", the request parameter that indicates that all name and Bean attribute names match will be passed to the corresponding property set method.
Value The Value property is optional. This property is used to specify the value of the Bean property. The string data is automatically converted to a number, Boolean, Boolean, Byte, Byte, Char, Character in the target class through the standard valueof method.   For example, Boolean and Boolean property values (such as "true") are converted by boolean.valueof, and attribute values of int and integer types (such as "42") are transformed by integer.valueof. Value and param cannot be used at the same time, but any of them can be used.
Param The Param is optional. It specifies which request parameter to use as the value of the Bean property. If the current request has no parameters, then nothing is done and the system does not pass NULL to the set method of the Bean property. Therefore, you can let the bean provide its own default property value and modify the default property value only if the request parameter explicitly specifies a new value.
<jsp:getProperty> Action Elements

The Jsp:getproperty action extracts the value of the specified bean property, converts it to 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 a property associated with GetProperty:

Properties Description
Name The name of the Bean property to retrieve. The bean must already be defined.
Property Represents the value to extract the Bean property
Instance

The following example we used the bean:

/* File: Testbean.java */package action; public class Testbean {   private String message = "No message specified";    Public String GetMessage () {      return (message);   }   public void Setmessage (String message) {      this.message = message;   }}

Compile the above instance and generate the Testbean.class file, copy the file to the server formally stored in the Java class directory, rather than reserved for the modified to automatically load the directory of classes (such as: C:\apache-tomcat-7.0.2\webapps\ In the Web-inf\classes\action directory, the CLASSPATH variable must contain the path. )。   For example, for Java WEB Server, the classes used by beans and all beans should be put into the classes directory, or packaged into a jar file and placed in the Lib directory, but should not be placed under Servlets. The following is a very simple example of the function of loading a bean and then setting/reading its message property.

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

Execute the above file, the output is as follows:

Using JavaBeans in Jspgot message .... Hello JSP ...
<jsp:forward> Action Elements

The Jsp:forward action moves the request to a different page. The Jsp:forward tag has only one property page. The syntax format is as follows:

<jsp:forward page= "Relative URL"/>

The following are the properties associated with forward:

Properties Description
Page The page property contains a relative URL. The value of the page can be either directly or dynamically computed at the time of the request, either as a JSP page or as a Java Servlet.
Instance

In the following example we used two files, namely: Date.jsp and main.jsp.

The date.jsp file code is as follows:

<p>   today's date: <%= (New Java.util.Date ()). toLocaleString ()%></p>

main.jsp File Code:

Now put the above two files in the root directory of the server to access the main.jsp file. The results appear as follows:

Today ' s date:12-sep-2010 14:54:22
<jsp:plugin> Action Elements

The Jsp:plugin action is used to insert an object or EMBED element that is necessary to run Java applets through a Java plugin, depending on the type of browser.

If the required plug-in does not exist, it downloads the plug-in and executes the Java component. The Java component can be an applet or a javabean.

The plugin action has multiple properties corresponding to the HTML element used to format the Java component. The Param element can be used to pass parameters to an applet or Bean.

The following is a typical example of using plugin action elements:

<jsp:plugin type= "applet" codebase= "dirname" code= "Myapplet.class"                            width= "height=" > <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 in using an applet to test the Jsp:plugin action element,<fallback> element is a new element, errors in component failure are sent to the user error message.

<jsp:element>, <jsp:attribute>, <jsp:body> action elements

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

The following instance dynamically defines an XML element:

<% @page language= "java" contenttype= "text/html"%>

The HTML code generated at execution time is as follows:

<jsp:text> Action Elements

The <jsp:text> action element allows you to use a template that writes text in JSP pages and documents in the following syntax format:

<jsp:text>template data</jsp:text>

The text template above cannot contain other elements and can only contain text and El expressions (note: El expressions are described in subsequent chapters). Note that in an XML file, you cannot use expressions such as ${whatever > 0} because the > symbol is illegal. You can use the ${whatever GT 0} expression or a value embedded 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 as follows:

<jsp:text><! [cdata[<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Dtd/xhtml1-strict.dtd" >]]></jsp:text>

You can try to use <jsp:text> and do not use the action element to perform the difference between the above instance.

JSP Action Elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.