JSP actions allows you to dynamically insert a file, reuse the JavaBeans component, forward to another page, or generate an HTML for the Java Plug-in. The following actions can be used:
(1) JSP: Include -- include a file when the page is requested.
(2) JSP: usebean -- locate or instantiate a JavaBean.
(3) JSP: setproperty -- set a JavaBean attribute.
(4) JSP: getproperty -- Insert the attributes of Javabean to the output.
(5) JSP: forward -- enables the requester to go to a new page.
(6) JSP: plugin -- use the object or embed label to generate Code .
1. jsp: include action
This action allows you to include some files on the page to be generated:
<JSP: Include page = "relative URL" Flush = "true"/>
Unlike include direve ve, this action contains the file only when the page is requested, while include Directive contains the file when the JSP page is converted to servlet. To improve efficiency, include action makes a small sacrifice, that is, it does not allow the contained page to contain general JSP code (for example, HTTP headers cannot be set). However, it has significant flexibility. The following JSP code inserts four different fragments into the following page. Each time the title changes, you only need to modify the four files without changing the main JSP page.
Whatsnew. jsp
<HTML>
<Head>
<Title> JSP tutorial </title>
<Body>
<Center>
<Table border = 5 bgcolor = "# ef8429">
<Tr> <TH class = "title"> what "s new at Chinese Comic sites </table>
</Center>
<P>
Here is a summary of our four most recent news stories:
<Ol>
<Li> <JSP: Include page = "News/item1.html" Flush = "true"/>
<Li> <JSP: Include page = "News/item2.html" Flush = "true"/>
<Li> <JSP: Include page = "News/item3.html" Flush = "true"/>
<Li> <JSP: Include page = "News/item4.html" Flush = "true"/>
</OL>
</Body>
</Html>
Of course, you can define your own HTML file, but note the following:
You should put the file in the news directory under your JSP directory.
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> </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 know before, JSP: setproperty can be used to assign values to attributes of a bean. You can implement it in two ways. First, use JSP: setproperty:
...
property = "someproperty "... />
in this way, JSP: setproperty will be executed whether or not a bean with the same ID and scope exists. In another way, JSP: setproperty appears in the JSP: usebean element, for example,
...
property = "someproperty "... />
in this case, JSP: setproperty is executed only when the new object is instantiated.
The following are available attributes of setproperty:
attributes
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 exampleProgramRun "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:
This property retrieves the bean property value, converts it to a string, and inserts it into the output. It has two mandatory attributes: name, the name introduced with JSP: usebean before, property, must be inserted Value Attribute
Data source: http://hi.baidu.com/rpgboy/blog/item/023f38ddc24421d98c10297d.html