I have been learning custom tags recently, so I want to implement some self-contained tag library functions in JSP. By the way, I will also review the reflection mechanism. (If you are not familiar with these two technologies, at the bottom of this blog, you can download example and PPT documents about reflection and custom tags)
The function to be implemented today is the usebean tag. The following table lists its attributes and uses. (I only selected an important attribute and did not implement all attributes)
Attribute |
Purpose |
id |
Name the variable to be applied bean. If a bean object with the same ID and scope is found, this object will be applied without a new example. |
class |
Specifies the entire Bean package name. |
scope |
The bean has four values: Page, request, session, and application. The default value is the page attribute, it indicates that this bean can only be applied to the current page (stored in the pagecontext of the current page); The request attribute indicates that this bean can only be applied to the current user request (stored in the servletrequest object ); the session attribute indicates that the bean can be applied to all pages of the current httpsession lifecycle. The application attribute value indicates that the bean can be applied to all pages of the shared servletcontext. Note that when there is no same ID and scope object, a JSP: usebean entity can only act on a new example, and vice versa, it acts on the previous object, any JSP: setparameter and other entities between JSP: usebean labels will be ignored. |
type |
Indicates the variable type of the object to be indexed. It must match the class name and parent class name. Remember, the variable name is replaced by the ID property value. |
beanName |
Given the bean name, you can provide it to Bean's example method. Only beanname and type are provided, while class attributes are ignored. |
The following is the tag processing method class: usebean. Java:
Import javax. servlet. jsp .*;
Import javax. servlet. jsp. tagext .*;
Import java. Lang. Reflect .*;
//
Public class usebean extends tagsupport {// inherits from the tagsupport class
Private string scope;
Private string type;
Public usebean () {super ();}
/**
* Sets the attribute access method, which is automatically called by the container. Setid () and GETID () are automatically implemented by the system.
*/
Public void setscope (string s ){
This. Scope = s;
}
Public String getscope () {return this. Scope ;}
Public void settype (string type ){
This. type = type;
}
Public String GetType () {return this. type ;}
/**
* Override the dostarttag Method
*/
Public int dostarttag () throws jsptagexception
{
Object o = NULL;
// Find the bean in the specified scope
If (scope. Equals ("page ")){
O = pagecontext. getattribute (GETID (), pagecontext. page_scope );
} Else if (scope. Equals ("request ")){
O = pagecontext. getattribute (GETID (), pagecontext. request_scope );
} Else if (scope. Equals ("session ")){
O = pagecontext. getattribute (GETID (), pagecontext. session_scope );
} Else if (scope. Equals ("application ")){
O = pagecontext. getattribute (GETID (), pagecontext. application_scope );
}
If (O = NULL)
{
System. Out. println ("O is null! ");
Try {
Class U = Class. forname (type );
O = U. newinstance (); // construction method without Parameters
System. Out. println ("create success! ");
}
Catch (exception e ){
Throw new jsptagexception ("error to created a" + GETID () + "object! ");
}
}
Pagecontext. setattribute (GETID (), O); // Save the instance object to the context object
Return eval_body_include; // return type
}
}
Now we have put the object instance in pagecontext. Can this be directly referenced on the JSP page? Of course not. It is different to directly reference a Java object in pagecontext. The difference is that the JSP Container is responsible for obtaining this object and providing it to the page as a script variable. The JSP Container is responsible for maintaining the state of the script variable and the corresponding object in pagecontext. There are two ways to declare script variables for custom tags.
One is to declare variable, and the other is to declare variables through the tagextrainfo class. The former is the method after jdk1.2. the advantage is that it is more convenient. The latter is troublesome because it needs to write another class file, but it is more flexible. It is recommended to use the latter for compatibility and functionality considerations. (For more information about this type, see the PPT document)
Import javax. servlet. jsp .*;
Import javax. servlet. jsp. tagext .*;
Public class usebeantag extends tagextrainfo {
Public variableinfo [] getvariableinfo (tagdata data ){
Return new variableinfo [] {
New variableinfo (
Data. GETID (),
Data. getattributestring ("type "),
True,
Variableinfo. at_begin)
};
}
}
Now, most of the work has been done to define a usebean tag. below is the definition tag library description (TLD) file, which is an XML document, it mainly defines the attributes of tags, the processing class, and the declaration of extended information classes. The main declaration part is as follows: (tag. TLD)
........................ (Skip the header section)
<! -- Usebean tag -->
<Tag>
<Name> usebean </Name>
<! -Declare the tag processing class -->
<Tag-class> CN. DEver. Tag. usebean </Tag-class>
<! -Declare the tag Extension Information Class -->
<Tei-class> CN. DEver. taginfo. usebeantag </Tei-class>
<! -Subject content type -->
<Body-content> JSP </body-content>
<! -Property settings -->
<Attribute>
<Name> scope </Name>
<! -Required? -->
<Required> true </required>
<! -Can JSP expressions be used? -->
<Rtexprvalue> true </rtexprvalue>
</Attribute>
<Attribute>
<Name> id </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
<Attribute>
<Name> type </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
</Tag>
In fact, this label library description file should be first created, because we mainly want to illustrate the implementation method, so the label description is placed behind. Next we will deploy the things we just made to our application. Click reference on the target JSP page.
<% @ Taglib uri = "/WEB-INF/Tag. TLD" prefix = "DEver" %>
<DEver: usebean id = "test" type = "cn. DEver. Common. User" Scope = "page"/>
OK. So far, we can start working on the custom usebean tag. Is it easy? It is actually not difficult. As long as you have mastered the process and configuration method for defining the tag, the rest is just the tag processing class. In the subsequent blogs, I will not detail these deployment and configurations, and directly provide code for the tag processing class.