我试图创建一个自定义标签来检查两个字符串是否相等,这个想法后来我可以改变我放入标签的javabeans。
package cknight.java3;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.*;
public class MyTagHandler extends TagSupport {
private String text ;
private String convertedtext ;
/**
* @return the text
*/
public String gettext() {
return text;
}
/**
* @param text the text to set
*/
public void settext(String text) {
this.text = text;
}
/**
* @return the convertedText
*/
public String getconvertedText() {
return convertedtext;
}
/**
* @param convertedtext the convertedText to set
*/
public void setconvertedText(String convertedtext) {
this.convertedtext = convertedtext;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();//returns the instance of JspWriter
try {
if(text.equals(convertedtext))
out.print("equal");
else
out.print("not equal");
} catch (Exception e) {
System.out.println(e);
}
return SKIP_BODY;//will not evaluate the body content of the tag
}
}
我不确定有什么问题,因为我在标签中设置了它。我不断得到错误,没有setter或该属性是必需的
<%--
Document : index
Created on : May 1, 2016, 10:20:53 AM
Author : cknig
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ taglib uri="WEB-INF/tlds/mytags.tld" prefix="m" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<p>this is where your custom tag would show up<m:tagExampleName text="example" contvertedText="example"/> <p>
</body>
</html>
这是我的tld文件
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>mytags</short-name>
<uri>/WEB-INF/tlds/mytags</uri>
<!-- A validator verifies that the tags are used correctly at JSP
translation time. Validator entries look like this:
<validator>
<validator-class>com.mycompany.TagLibValidator</validator-class>
<init-param>
<param-name>parameter</param-name>
<param-value>value</param-value>
</init-param>
</validator>
-->
<!-- A tag library can register Servlet Context event listeners in
case it needs to react to such events. Listener entries look
like this:
<listener>
<listener-class>com.mycompany.TagLibListener</listener-class>
</listener>
-->
<tag>
<name>tagExampleName</name>
<tag-class>cknight.java3.MyTagHandler</tag-class>
<attribute>
<name>text</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type> String </type>
</attribute>
<attribute>
<name>contvertedtext</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type> String </type>
</attribute>
</tag>
</taglib>