您的位置:首页 > Web前端 > JavaScript

jsp自定义标签——部分容易不注意到的属性

2016-09-02 17:34 253 查看
看项目中的源代码才发现自定义标签中<rtexprvalue>也是个小知识点。

有些小激动的查了下,以下就是查阅别人的博客后的观后感。

rtexprvalue 全称Run - time Expression Value,表示是否可以使用jsp表达式。

rtexprvalue标签在<attribute>标签下面,其中有true,false。

当<rtexprvalue>true</rtexprvalue>时,表示该自定义标签的某个属性的值可以直接指定也可以通过动态计算指定。

<rtexprvalue>false</rtexprvalue>时,表示该自定义标签的某个属性的值只能直接指定。

此处可以看下rtexprvalue标签在attribute标签中的位置:

<tag>
<name>selectDetail</name>
<tag-class>com.nms.taglib.SelectDetail_Tag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>id</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>selectVal</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>collection</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>


此处给出rtexprvalue标签为true时的例子:

<%
User user = new User();
user.setId(1);
request.setAttribute("user",user);
%>
<html:selectDetail id="" collection="<%="SEX" %>" selectVal="${user.id}"></html:selectDetail>


此处给出rtexprvalue标签为false时的例子:

<html:selectDetail id="" collection="SEX" selectVal="男"></html:selectDetail>
如果还是使用上面为true时的样式,就会报错啦!!

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

此处再说下<body-context>这个标签,有三个值可选:jsp, empty, tagdependent,scriptless

jsp 表示标签体由其他jsp元素组成,比如EL表达式,标准或定制动作以及脚本元素(常用)

如果有jsp元素,那么标签会先解释这些元素,然后再将实际值传入。比如标签体里含有<%= attributeName %>这样的jsp元素,此时标签就会把attributeName的实际值传入进来。

empty 表示标签体必须为空

在引用这个Tag的时候,可以<bean:write bundle="attributeName"
/>,而不用<bean:write bundle="attributeName"></bean:write>

tagdependent 表示标签体可以包含看似为JSP元素的内容,但是容器不对其进行计算(当标签体中的内容与JSP元素产生混淆时采用这个,不常用)
<span style="font-size:12px;"><body>
<jsp:useBean id="catalog" class="liangchao.bean.chap10.CatalogBean"></jsp:useBean>
<my:forEachOption items="${catalog.productList}" var="product">
${product.name }:${product.price }<br/>
</my:forEachOption>
</body>  </span>
结果:${product.name
}:${product.price }

${product.name }:${product.price }

${product.name }:${product.price }

scriptless 标签体重可以包含EL表达式和动作,但是不能有脚本元素

<body>
<jsp:useBean id="catalog" class="liangchao.bean.chap10.CatalogBean"></jsp:useBean>
<my:forEachOption items="${catalog.productList}" var="product">
${product.name }:${product.price }<br/>
</my:forEachOption>
</body>


结果:
Thinking in Java:108.5

JSP Programming:58.5

Core Java:99.5

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
其中<attribute>标签中还有个<required>标签,表示用户是否必须填写的属性,true表示必须填,false表示不用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp 自定义标签