Taglib principle and the support of El expression

Author:Anonymous    Updated:2008-2-26 22:04:34
1. Look at the example of such a

<% @ Page contentType = "text / html; charset = gb2312" language = "java"%>
<% @ Taglib uri = "/ WEB-INF/tlds/c.tld" prefix = "c"%>
<! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<%
String tut = "tutorial";
Request.setAttribute ( "tut" tut);
%>
The String in request is:
<c:out Value="${tut}"/>
</ Body>
</ Html>

2. El how to support the expression

In the path org.apache.taglibs.standard.lang.support,有个叫ExpressionEvaluatorManager.evaluate way, when el expression as to the Senate, calling this method, it will automatically tag el expression transformed. For example, you want to tag the value field support el expression, then simply set method, the following call:

Public void setValue (Object value) throws JspException
(
ExpressionEvaluatorManager.evaluate this.value = (
"Value", value.toString (), Object.class, this, pageContext);
)

ExpressionEvaluatorManager.evaluate four parameters. A name tag said, in admitting mistakes el expressions used. General and attribute names the same. The second requirement string, usually called simple object toString input methods. The third category is usually used Object.class. This can be by the fourth, fifth is pageContext variables.

Usually not thinking too much of this method. Only changed the name attribute, the other copy can be.

Note: When you tag attributes el expression of support, you must put it Object object to the statement. If the above value, the statement should be:

Private Object value = null;

3. Example: El expression of support for OutputTag

Package diegoyun;

Import javax.servlet.jsp.JspException;
Import javax.servlet.jsp.JspWriter;
Import javax.servlet.jsp.tagext.TagSupport;

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

Public class NewOutputTag extends TagSupport
(
Private Object name = null;

Public void setName (Object name) throws JspException
(
ExpressionEvaluatorManager.evaluate this.name = (
"Name", name.toString (), Object.class, this, pageContext);
)
Public int doStartTag () (throws JspException
Try
(
PageContext.getOut JspWriter out = ();
Out.print ( "Hello!" + Name);
)
Catch (Exception e)
(
Throw new JspException (e);
)
Return EVAL_PAGE;


)
)

Adding in a statement diego.tld

<! - NewOutputTag ->
<tag>
<name> Newout </ name>
<tag-class> Diegoyun.NewOutputTag </ tag-class>
<body-content> Empty </ body-content>
<attribute>
<name> Name </ name>
<required> False </ required>
<rtexprvalue> True </ rtexprvalue>
</ Attribute>
</ Tag>

Jsp test preparation

<% @ Page language = "java"%>
<% @ Taglib uri = "/ WEB-INF/tlds/diego.tld" prefix = "diego"%>
<html>
<body Bgcolor="#FFFFFF">
<%
String s = "diego";
Request.setAttribute ( "name", s);
%>
Test El supported tag:
<br>
<diego:newout Name="${name}"/>

</ Body>
</ Html>

Pages can be seen output:

Test El supported tag:
Hello! Diego
Previous:JSP entry primary guide the use of the Session
Next:ASP in the inside pages of the register DLL VBScript CLASS
User Reviews
Site Search
Related Articles
Recommended article
AD