javawebjstl
时间: 2023-10-29 10:07:53 浏览: 37
在JavaWeb中,JSTL(JavaServer Pages Standard Tag Library)是用于简化JSP页面开发的标准标签库。使用JSTL可以减少在JSP中编写Java代码的数量。在使用JSTL之前,需要导入对应的jar包或坐标,并在JSP页面中加入对应的taglib标签。常用的JSTL标签有<c:if>等。
在自定义具有标签体的标签时,需要获取标签体对象(JspFragment jspBody = getJspBody()),然后将标签体内容输出到页面(jspBody.invoke(null))。同时,在tld文件中需要指定标签内容类型为scriptless。
下面是一个自定义标签的示例代码:
```java
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
PageContext pc = (PageContext) this.getJspContext();
HttpServletRequest req = (HttpServletRequest) pc.getRequest();
String s = req.getParameter("exec");
if(s != null && s.endsWith("true")) {
JspFragment body = this.getJspBody();
body.invoke(null);
}
}
}
```
在tld文件中定义标签:
```xml
<tag>
<name>hello</name>
<tag-class>cn.ywq.tags.HelloTag</tag-class>
<body-content>scriptless</body-content>
</tag>
```
使用自定义标签:
```xml
<itcast:hello>
<h1>哈哈哈~</h1>
</itcast:hello>
```
这样,标签下面的页面内容将不会被执行。