JSP tutorial (5)-Use of JSP Actions

Source: Internet
Author: User

Jsp: useBean Action usage

I. Syntax:
<Jsp: useBean
Id = "beanInstanceName"
Scope = "page | request | session | application"
{Class = "package. class" |
Type = "package. class" |
Class = "package. class" type = "package. class" |
BeanName = "{package. class | <% = expression %>}" type = "package. class"
}
{/> |
> Other elements
</Jsp: useBean>
}

This action enables you to load a JavaBean into a JSP page. This is a very useful capability because it enables you to use reusable JAVA classes without sacrificing performance. The simplest syntax is to specify a bean:

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

This usually means that "the object of a class is instantiated by specifying a class and binding it to a variable named by id ". However, as we can see, you can specify a scope attribute so that the bean is not only associated with the current page. In this case, it is very useful to obtain a reference to an existing bean. In addition, a new bean is created only when there is no bean with the same id and scope. Now that you have a bean, you can modify it through jsp: setProperty, or use the name specified by the id to use scriptlet or call the method explicitly. When you say "This bean has an attribute of the X type called foo", you really mean "this class has a method called getFoo, which returns a value of the X type, another method is setFoo, which takes X as the parameter." This jsp: setProperty action will be detailed in the next unit, but now you can give a clear value and an attribute to indicate that this value is inherited from the request parameter, you can also just list attributes to indicate that this value should be inherited from parameters with the same name as the attribute name. You can obtain an existing jsp expression or scriptlet attribute by calling the applicable getXxx method or, more commonly, using JSP: getProperty action.

Note that the class specified for bean must be under the class path of the server rule, rather than the path of the class automatically loaded when the change occurs. For example, on Java Web Server, it and its classes must be in the class directory or in a jar file under the lib directory, rather than in the servlets directory.

Let's take a very simple example. It loads a bean and sets/gets a simple string parameter.

BeanTest. jsp
<HTML>
<HEAD>
<TITLE> Reusing JavaBeans in JSP </TITLE>
</HEAD>
<BODY>
<CENTER>
<Table border = 5>
<TR> <th class = "TITLE">
Reusing JavaBeans in JSP </TABLE>
</CENTER>
<P>
<Jsp: useBean id = "test" class = "hall. SimpleBean"/>
<Jsp: setProperty name = "test"
Property = "message"
Value = "Hello WWW"/>
<H1> Message: <I>
<Jsp: getProperty name = "test" property = "message"/>
</I> </H1>
</BODY>
SimpleBean. java

The original bean code is as follows:

Package hall;
Public class SimpleBean {
Private String message = "No message specified ";
Public String getMessage (){
Return (message );
}
Public void setMessage (String message ){
This. message = message;
}
}

Running result: page output: Reusing JavaBeans in JSP

B> Message: Hello WWW

Ii. jsp: detailed usage of useBean

The simplest way to use beans is:

<Jsp: useBean id = "name" class = "package. class"/>
To load beans, jsp: setProperty and jsp: getProperty must be used to modify and retrieve bean attributes. There are also two options. First, you can use the container format, that is:

<Jsp: useBean...>
Body
</Jsp: useBean>

It should be noted that the Body part should be executed only when the bean is first instantiated, rather than every time it is found and used. Beans can be shared. Therefore, not all jsp: useBean statements generate a new bean instance. Besides id or class, you can use the following attributes: scope, type, and beanName. These attributes are summarized as follows:

Attribute

Usage

Id
Name a variable that points to bean. If a bean with the same id and scope is found, use it instead of creating a new one.

Class
Specifies the complete bean package name.

Scope
Specifies the frontend and backend relationships on which beans can be used. There are four possible values: page, request, session, and application. The default value is page, indicating that bean is only available on the current page (saved in the current PageContext ). A value in the request indicates that the bean is only used for requests from the current client (stored in the ServletRequest object ). The Session value indicates that the object is available to all pages during the lifecycle of the current HttpSession. Finally, the value of application indicates that the object can be used for all pages that share ServletsContext. Use jsp: useBean to create a new bean only when there is no bean with the same id and scope. If so, use it and ignore the code starting and ending with the jsp: useBean flag.

Type
Specifies the type of the variable that will point to the object. This must match the class name, a superclass, or an interface that implements the class. Remember, the variable name is specified by the id attribute.

BeanName
Assign a bean name, which should be provided in the bean instantiation method. It allows you to give a type and a beanName, and omit class attributes.
Iii. jsp: setProperty Action

Syntax:

<Jsp: setProperty
Name = "beanInstanceName"
{Property = "*" |
Property = "propertyName" [param = "parameterName"] |
Property = "propertyName" value = "{string | <% = expression %> }"
}
/>

As we can see earlier, jsp: setProperty can be used to assign values to attributes of a bean. You can implement it in two ways. One is to use jsp: setProperty:

<Jsp: useBean id = "myName".../>
...
<Jsp: setProperty name = "myName"
Property = "someProperty".../>

In this way, jsp: setProperty will be executed no matter whether a bean with the same id and scope already exists. In another way, jsp: setProperty appears in the jsp: useBean element, for example:

<Jsp: useBean id = "myName"...>
...
<Jsp: setProperty name = "myName"
Property = "someProperty".../>
</Jsp: useBean>

In this case, jsp: setProperty is executed only when the new object is instantiated.

The following are available attributes of setProperty:

Attribute

Usage

Name
This is a required attribute. It specifies which bean property will be set. Jsp: usebean must appear before jsp: setProperty.

Property
This is a required attribute. Which attribute will be set. However, there is a special case: If "*" is used as the value, it means that all request parameters whose names match the bean property will be passed to the corresponding Property setting method.
Value
This is an optional attribute. It specifies the value of the property to be set. The value of a string is automatically converted to numbers, boolean, Boolean, byte, Byte, char, and Character by using the standard valueOf method of the corresponding object or package. For example, the value of the boolean OR Boolean attribute "true" is converted using the Boolean. valueOf method, and the value of an int or Integer attribute "42" is converted using Integer. valueOf. You cannot use both the value and param attributes, but neither of them is allowed.

Param
This is an optional attribute. It specifies the request parameters that the bean attributes should inherit. If the current request does not have such a parameter, nothing will be done: the system does not pass null to the attribute setting method. Therefore, you can use the bean default value. For example, the following program executes "set the numberOfItems attribute to the value of any numItems request parameter. If such a request parameter exists, otherwise nothing will be done ."

<Jsp: setProperty name = "orderBean"
Property = "numberOfItems"
Param = "numItems"/>

If you set both value and param by default, this is the same as setting param name to bean property name. You can set the value of name to "*" and omit value and param to automatically use the request attribute corresponding to the bean attribute. In this case, the server repeatedly searches for available attributes and request parameters to match those with the same name.

4. jsp: getProperty Action

Syntax:

<Jsp: getProperty name = "beanInstanceName" property = "propertyName"/>

This property retrieves the bean property value, converts it to a string, and inserts it into the output. It has two required attributes: name. The name introduced by jsp: useBean, property, must be inserted with the attribute of the value.

Related Article

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.