jsp自定义标签
程序员文章站
2022-06-08 23:46:50
...
1.定义处理类
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.etool.modules.curd.service.CurdService;
public class OutputTag extends TagSupport{
//接收2个参数
private String target;
private String fieldName;
@Override
public int doStartTag() throws JspException {
String result="";
JspWriter out = this.pageContext.getOut();
ServletContext servletContext = pageContext.getServletContext();
//逻辑
try
{
out.print(result);
}catch(IOException e)
{
e.printStackTrace();
}
return super.doStartTag();
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
}
2.编辑tld文件,在WEB-INF\my-tag.tld
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>/</description> <display-name>My Tag Library</display-name> <tlib-version>1.0</tlib-version> <short-name>m</short-name> <uri>/my-tags</uri> <tag> <name>out</name> <tag-class>com.etool.commons.tag.OutputTag</tag-class> <body-content>JSP</body-content> <attribute> <name>target</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>String</type> </attribute> <attribute> <name>fieldName</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>String</type> </attribute> </tag> </taglib>
3.配置web.xml
<jsp-config> <taglib> <taglib-uri>/my-tags</taglib-uri> <taglib-location>/WEB-INF/my-tags.tld</taglib-location> </taglib> </jsp-config>
4.使用前
<%@ taglib uri="/my-tags" prefix="m" %>
这个uri和web.xml中的<taglib-uri>的值一样
5.使用
<m:out target="${target}" fieldName="${item.name}"></m:out>
在eclipse会有提示
上一篇: c# winform文本框数字,数值校验
下一篇: Adroid相关问题收集