Reputation: 1771
I'm trying to write custom jsp tag. But my tag is not invoked. Instead I got response in plain text with my html and jsp tags(maybe browser doesn't parse it as html because of jsp tags). My tld file is in the right place(/WEB-INF/custom.tld) and I have no errors or exceptions in console and I got no clue what is wrong with my tag. Probably it's because of some jsp dependencies or deployment descriptors. I'm using tomcat 7. My code:
web.xml
<web-app 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-app_3_0.xsd"
version="3.0">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
custom.tld
<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">
<tlib-version>1.0</tlib-version>
<short-name>Test</short-name>
<tag>
<name>greet</name>
<tag-class>com.test.tag.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
hello.jsp
<%@ taglib prefix="ex" uri="/WEB-INF/custom.tld"%>
<html>
<head>
<title></title>
</head>
<body>
<ex:greet/>
</body>
</html>
Tag
package com.test.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class HelloTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
getJspContext().getOut().println("Hello!");
}
}
And my response looks exactly like this:
<%@ taglib prefix="ex" uri="/WEB-INF/custom.tld"%>
<html>
<head>
<title></title>
</head>
<body>
<ex:greet/>
</body>
</html>
Could you please tell me what am I doing wrong?
Upvotes: 1
Views: 162