JSP: type, class, and beanname in usebean

Source: Internet
Author: User

The online explanation is as follows:
Class = "package. Class"
Use the New Keyword and the class constructor to sample a bean from a class. this class cannot be abstract and must have a common constructor without parameters. the package name is case sensitive.

Type = "package. Class"
If the bean already exists in the specified range, write the bean to a new database type. If you do not use the class or beanname to specify the type, bean will not be named by the example. Package and class names, case sensitive.

Beanname = "{package. Class | <% = expression %>}" type = "package. Class"
Use the java. Beans. Beans. instantiate method to sample a bean from a class or continuous template, and specify the bean type. Beanname can be package or class, and its value will be passed to beans. instantiate. tupe with the same value as bean.

 

After the experiment, I understand:
The following two statements have the same meaning:

<%
// Check session to see if there's a cart in it;
Cart cart = (Cart) Session. getattribute ("cart ");
If (Cart = NULL ){
Cart = new cart ();
Session. setattribute ("cart", cart );
}
%>

First:

<JSP: usebean id = "cart" class = "com. Shopping. User. Cart" Scope = "session"> </jsp: usebean>

Second:

<JSP: usebean id = "cart" type = "com. Shopping. User. Cart" Scope = "session"> </jsp: usebean>

 

Assume that the current cart! = NULL is almost the same as the two. If session is used. setattribute ("cart", null); after setting, the Access contains <JSP: usebean id = "cart" type = "com. shopping. user. cart "Scope =" session "> </jsp: usebean> --- An exception is thrown when the page of the JSP Action Statement (Org. apache. jasper. jasperexception: bean cart not found within scope ). Use <JSP: usebean
Id = "cart" class = "com. shopping. user. cart "Scope =" session "> </jsp: usebean> does not throw an exception. Therefore, the second writing method should have the same meaning as the first writing method. // Why is the same

That is to say, when using class, it first determines whether to get the bean of the instance. If it cannot get the bean, it will use the new keyword to instantiate the bean. When using type, it only finds whether the specified range exists and does not use the class when it does not exist.,Beanname specifies type,This will throw an exception, so pay attention to this when using type.

In addition, the class and beanname must specify the package (even if the package is introduced), and the type can be unspecified.

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.