Bean action and EL expression language basics

Source: Internet
Author: User

Bean-related standard actions:

<Jsp: useBean id = "person" class = "foo. Person" scope = "request"/>
<Jsp: getProperty name = "person" property = "name"/>
<Jsp: setProperty name = "person" property = "name" value = "Fred"/>
Jsp: useBean action automatically creates a person attribute when there is no person attribute, and assigns it null

Jsp: useBean body: www.2cto.com

<Jsp: useBean id = "person" class = "foo. Person" scope = "request">
<Jsp: setProperty name = "person" property = "name" value = "Fred"/>
</Jsp: useBean>

When jsp: setProperty is in the jsp: useBean body, attributes are set for newly created attributes only when the person attribute does not exist, and the existing attribute values are not changed.

Create a multi-state bean reference:

<Jsp: useBean id = "person" type = "foo. Person" class = "foo. Empolyee" scope = "request"/>
If type is used, but no class exists, the bean must already exist.
If class (with or without type) is used, class cannot make abstract classes, and there must be a public constructor without parameters.

The param attribute can be used to set the attribute property to the value of a request parameter. For example:
<Jsp: useBean id = "person" class = "foo. Person" scope = "request">
<Jsp: setProperty name = "person" property = "name" param = "userName"/>
</Jsp: useBean>

 
If the request parameter name is the same as the bean property name, you do not need to specify the param attribute. For example:
<Jsp: useBean id = "person" class = "foo. Person" scope = "request">
<Jsp: setProperty name = "person" property = "name"/>
</Jsp: useBean>


If all request parameter names match the bean nature, you can do the following:
<Jsp: useBean id = "person" class = "foo. Person" scope = "request">
<Jsp: setProperty name = "person" property = "*"/>
</Jsp: useBean>

 
Bean tag automatically converts the basic type and String type.

If a script is used, automatic conversion is not completed, that is, the expression fails in the <jsp: setProperty> tag.

With <jsp: getProperty>, you can only access the nature of bean, but not the nested nature.

--------------------------------------------------------------------

EL expression: for example, $ {person. name}

The first variable name in the expression can make an implicit object or attribute

EL implicit objects include pagination, requestScope, sessionScope, applicationScope, param, paramValues, header, headerValues, cookie, initParam, pageContext

Attributes include: attributes in any scope, including page, request, session, and application.

Besides pageContext, all EL implicit objects are mapped objects. pageContext is a reference to pageContext objects.

Use the dot "." To access the property and ing value, such as $ {person. name}. The variable on the right of the dot must comply with the java variable name naming rules. This variable is a ing key or property.

Use the [] symbol to access the nature or ing key. In addition to the vertex that can be accessed, [] can also access the content in the array and List, and the String index in the array and List is forcibly converted to int, for example:

$ {FavoriteFood [0]} and $ {favoriteFood ["0"] can be accessed

When [] is used, if [] does not contain quotation marks, the container calculates the content in brackets, searches for the attribute bound to the name, and replaces it with the value of this attribute.

Nested expressions can be used in parentheses, for example: $ {musicMap [MusicType [0]}

Param implicit object: Get the parameter of the corresponding parameter name, for example, $ {param. food}

ParamValues implicit object: obtain multiple parameters of the corresponding parameter name, for example, $ {paramValues. food [0]}, $ {paramValues. food [1]}

Obtain the host header: $ {header ["host"]} or $ {header. host}

Get the http request method: Not $ {requestScope. method}, but $ {pageContext. request. method}

RequestScope is only a Map of the Request scope attribute, not the request object itself.

Implicit objects in the scope can solve naming conflicts and point numbers in attribute names. For example, an attribute is foo. person, $ {foo. person. name} cannot find the desired property, because the foo of the container is treated as the property name and is resolved with an implicit object. $ {requestScope ["foo. person "]. name}, OK.

Obtain the cookie value, for example, $ {cookie. userName. value}

Get the context initialization parameters, for example, $ {initParam. mainEmail}. Note that initParam indicates the context initialization parameter, not the servlet initialization parameter.

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.