Taglib Principle and Implementation: Discuss El and JSTL labels

Source: Internet
Author: User

Question: You want to work with JSTL. For example, after processing some logic with your own tags, let JSTL process the remaining work.

Take a look at the JSP Example:

<%

String name = "diego ";

Request. setAttribute ("name", name );

%>

<C: out value = "$ {name}"/>

......

Many JSTL labels support El expressions. Therefore, you can use other jstl labels as long as you insert values into your own labels into the request.

In the following example, get the object from the request, find its attribute value, and insert it to the request.

Package diegoyun;

Import javax. servlet. jsp. JspException;

Import javax. servlet. jsp. tagext. TagSupport;

Import org. apache. commons. beanutils. PropertyUtils;

Import org. apache. taglibs. standard. lang. support. ExpressionEvaluatorManager;

Public class SetVarTag extends TagSupport

{

Private Object value = null;

Private String property = null;

Private String var = null;

Public void setVar (String var)

{

This. var = var;

}

Public void setProperty (String property)

{

This. property = property;

}

Public void setValue (Object value) throws JspException {

This. value = ExpressionEvaluatorManager. evaluate ("value", value. toString (), Object. class, this, pageContext );

}

Public int doEndTag () throws JspException {

Object propertyValue = null;

Try {

PropertyValue = PropertyUtils. getProperty (value, property );

}

Catch (Exception e ){

Throw new JspException (e );

}

PageContext. setAttribute (var, propertyValue );

Return EVAL_PAGE;

}

}

Write TLD

<! -- SetVarTag -->

<Tag>

<Name> set </name>

<Tag-class> diegoyun. SetVarTag </tag-class>

<Body-content> empty </body-content>

<Attribute>

<Name> value </name>

<Required> true </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

<Attribute>

<Name> property </name>

<Required> false </required>

<Rtexprvalue> false </rtexprvalue>

</Attribute>

<Attribute>

<Name> var </name>

<Required> false </required>

<Rtexprvalue> false </rtexprvalue>

</Attribute>

</Tag>

Compile JSP

<% @ Page language = "java" %>

<% @ Page import = "diegoyun. vo. *" %>

<% @ Taglib uri = "/WEB-INF/tlds/diego. tld" prefix = "diego" %>

<% @ Taglib uri = "/WEB-INF/tlds/c. tld" prefix = "c" %>

<Html>

<Body bgcolor = "# FFFFFF">

<%

Man man = new Man ();

Man. setName ("diego ");

Request. setAttribute ("man", man );

%>

Get value from request and set its property value into request: <br>

<Diego: set value = "$ {man}" property = "name" var = "myname"/>

Now use OutTag of jstl taglib to get the name: <br>

Value is: <c: out value = "$ {myname}"/>

</Body>

</Html>

Run, the effect is as follows:

Get value from request and set its property value into request:

Now use OutTag of jstl taglib to get the name:

Value is: diego

Conclusion

Interaction with JSTL is a very useful technology. In JSTL, many labels are provided to complete basic functions, such as output, loop, and condition selection. You can implement your own labels only when processing your own specific logic, and provide interaction with jstl, which can greatly improve reusability and reduce workload.

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.