JSP Series 4: JSP action elements

Source: Internet
Author: User

1. Action: a predefined Java code. The purpose is to make code reusable.

1. Provide information for the request phase and allow control to be transferred between pages.
 
2. The jsp2.0 Specification defines standard actions. Standard actions are tags that affect JSP running behaviors and responses to customer requests.
When the page is converted to servlet, the JSP Container encounters these labels and uses the pre-defined Java code corresponding to the label to replace it.
 
3. The Action Element syntax is based on the XML format.
<JSP: actionname ATTR = "value"...> </jsp: actionname>

4. the JSP standard action is executed at the client request.

5. The Action Element tag is the usage form of these actions in JSP.

6. You can use custom tags to customize actions.

300 425
Ii. <JSP: userbean>, <JSP: setproperty>, and <JSP: getproperty>: Use Javabean in JSP, and the code inserts the code into the _ jspservice method according to the position where it appears.

1. <JSP: userbean> is used to instantiate a bean or locate an existing bean instance.

Syntax: <JSP: userbean id = "Bean's unique identifier" class = "package. Class" SCOP = "bean visibility"> </jsp: userbean>

Bean visibility: Page (default), request, session, Application

* Instantiate the class and bind the instantiated object to the variable in _ jspservice. The variable is named by ID and the type is set to itself or type.

* When using the class attribute to specify the Bean class, do not rely on the page command import to import the class package.

** During bean sharing, a new object is added only when beans with the same ID and scope do not exist.

* The type attribute is used to direct a bean instance to a variable with the specified name type specified by ID as type. The type can be parent class, implemented interface, or itself.
Example: Assume package. mybean inherits from Java. Beans. simplebeaninfo
<JSP: userbean id = "mybean" class = "package. mybean" SCOP = "session" type = "Java. Beans. simplebeaninfo"> </jsp: userbean>
Generate java. Beans. simplebeaninfo mybean = new package. mybean () in the servlet ();

* JSP: code between the userbean start tag and end tag. It is executed only when bean is instantiated. Therefore, it depends on the bean lifecycle specified by scope.
Therefore, you can put the code for setting bean properties (<JSP: setproperty>) in the tag.
(<JSP: userbean> code inside the tag </jsp: userbean>)

Execution Process:
1. the JSP Container searches for the JavaBean instance of the specified ID within the range specified by the scope attribute.
2. Find the instance and the instance is shared. "The code in the tag will not be executed. "
3. No. If only the class attribute is specified, a new instance is created based on the class attribute.
No. If only the beanname attribute is specified, use beanname as the parameter to call the instantiate method in the Java. Bean. Beans class to create a new bean instance.
4. Save the variable to the range specified by the scope attribute. The setattribute method must use the id value as the key (which is automatically processed by the container ).

Bean sharing: Beans are bound to the _ jspservice method as local variables.
Page (default): Because each page has a different pagecontext object, the bean is not shared and a new bean is created for each request. (Except for pages contained in the include command)
Request: when processing the current request, put the bean into the httpservletrequest object. It seems that the bean is not shared,
However, when the include command and action are used and forward Forwarding is performed, several pages will be formed to share a reqeust object, and bean will be shared.
Session: shared in the current session.
Application: shared in Web applications.

2. <JSP: setproperty>: Set the attributes of the bean, corresponding to (rather than simply calling) The setxxx method of the bean.
 
Important: the bean attribute naming rules are consistent with the JSP: setproperty tag naming rules. Invalid bean attribute naming causes org. Apache. Jasper. jasperexception.
These specifications are defined in Java. Beans. Beans and are called by development tools to check classes. Bean is determined by its specifications, rather than its parent class.

The bean analyzer will execute a "lowercase letter process" to export the attribute name and convert the first character after get/set to lowercase letters. This may cause inconsistency between attributes and parameter names.
The worst thing is that the first letter of the get/set attribute will not be changed to lowercase letters because the two letters after the get/set are uppercase letters.
Assume that the property parsed by the method name geturl is URL rather than URL. If you declare the property as URL, the default method is geturl. As the attribute URL and URL are obviously different.
Therefore, when naming bean attributes, either the first two letters are in uppercase or the first two letters are in uppercase.

What happened in JSP?
When <JSP: setproperty> and <JSP: getproperty> are used, the names of all request parameters are displayed, the JSP Container uses request parameters to export the get/Set Method of the corresponding attribute to be set. This method is called. There is no problem here.
The bean analyzer pushes the method name based on the bean attribute name. The method name derived from the JSP Container and the method name derived from the bean analyzer are also correct.
The problem is that when setting the attribute value, both of them are in trouble when using the method name to export the attribute name!

Syntax: <JSP: setproperty name = "bean ID" property = "property name" value = "property value"> </jsp: setproperty>
 
<JSP: setproperty name = "bean ID" property = "property name" Param = "parameter name"> </jsp: setproperty>

* The param attribute cannot be used with the value attribute.

* Difference between the value attribute and the param attribute:
Use Value Attribute: manually convert the bean attribute and request parameter type.
Use the param attribute: for basic types and packaging classes, the container automatically converts and matches the types of attributes and request parameters. If the matching fails, no action is taken, that is, null is not passed.

* If the bean attribute name is the same as the request parameter name, you can leave the parm attribute unspecified.

3. <JSP: getproperty>: access and output the bean property value, corresponding to (rather than simply calling) The getxxx method of bean.

Important: Same as above;
 
Syntax: <JSP: getproperty name = "bean ID" property = "property name"> </jsp: setproperty>

* Convert the attribute value to string and send it to the output stream. If the attribute is an object, the tostring () method of the object is called. Therefore, it is necessary to implement the tostring Method for our class, instead of relying on the default tostring method of the object;

3. <JSP: param>: Add or replace the request parameters sent to the contained page on the home page in the form of a "name-value" pair. Generally used together with include commands, actions, and forward actions;

1. Syntax: <JSP: Param name = "parameter name" value = "parameter value"> </jsp: param>

2. Visibility of parameters: parameters on the contained page cannot be accessed on the home page, but on the included page, parameters on the home page can be accessed.
 

4. <JSP: Include>: Include the "output" of other pages during the "request. This behavior is identical to the include method of the requestdispatcher class.
 
* When the page to be contained needs to change the response header and set the header, use the include command in this case.
So how can we use the JSP: Include element, set the response header, and set the header? Another way is to set the buffer, set the response header and set the header before the output stream is actually input to the client.
 
5. <JSP: Forward>: Avoid using this action to handle complex situations. Use the forward method of the servlet requestdispatcher.

6. <JSP: Element>, <JSP: attribute>, and <JSP: Body>: Specify the tag for dynamically defining an XML element.
 
Syntax: <JSP: element name = "element name">
<JSP: attributee name = "element attribute name" trim = "How to Deal with spaces"> element attribute value </jsp: attributee>
<JSP: Body> element content </jsp: Body>
</Jsp: Element>

Result: <element name: element attribute name = "element attribute value"> element content </element name>

7. Other actions are generally related to the XML definition of the JSP document.

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.