IDEA中web.xml无法识别taglib
一、问题描述
在孙卫琴的《Tomcat与JavaWeb开发技术详解》第3章的第3.4节中,书中使用了自定义的JSP标签taglib,但在IDEA中出现了无法识别taglib的情况,经查资料,大致是版本原因,这里不做详细解释,需要对源码进行一些修改,修改后能够正确运行。
原因这篇文章写得比较清楚,本文也参考了这篇文章:https://blog.csdn.net/u012165313/article/details/20548653
另外,关于为什么要用Taglib,Taglib到底有什么好处?参考文章:https://blog.csdn.net/mark_to_win/article/details/84870796
二、解决方案
1.index.jsp(无需修改)
<%--
Created by IntelliJ IDEA.
User: yongpu
Date: 2019/2/19
Time: 21:57
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/mytaglib" prefix="mm" %>
<html>
<head><title>helloapp</title></head>
<body>
<b><mm:hello/>:<%=request.getAttribute("USER")%></b>
</body>
</html>
2.HelloTag.java(无需修改)
package mypack;
import java.io.PrintWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
public class HelloTag extends TagSupport{
/** 当JSP解析器遇到hello标签的结束标志时,调用此方法 */
public int doEndTag() throws JspException{
try{
//打印字符串“Hello”
pageContext.getOut().print("Hello");
}catch (Exception e) {
throw new JspTagException(e.getMessage());
}
return EVAL_PAGE;
}
}
3.mytaglib.tld(修改后)
由<tag-class>改为<tagclass>,由<body-content>改为<bodycontent>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<!-- a tag library descriptor -->
<taglib>
<tlibversion>1.1</tlibversion>
<jspversion>2.1</jspversion>
<shortname>mytaglib</shortname>
<uri>/mytaglib</uri>
<tag>
<name>hello</name>
<tagclass>mypack.HelloTag</tagclass>
<bodycontent>empty</bodycontent>
<info>Just Says Hello</info>
</tag>
</taglib>
4.DispatcherServlet.java(无需修改)
package mypack;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class DispatcherServlet extends GenericServlet {
private String target = "/index.jsp";
/** 响应客户请求*/
public void service(ServletRequest request,ServletResponse response)
throws ServletException, IOException {
System.out.println("*****************test 2**********");
//读取表单中的用户名
String username = request.getParameter("username");
//读取表单中的口令
String password = request.getParameter("password");
//在request对象中添加USER属性
request.setAttribute("USER", username);
//在request对象中添加PASSWORD属性
request.setAttribute("PASSWORD", password);
/*把请求转发给hello.jsp */
ServletContext context = getServletContext();
RequestDispatcher dispatcher =context.getRequestDispatcher(target);
dispatcher.forward(request, response);/*转发给index.jsp*/
}
}
5.web.xml(修改后)
需要在<taglib>的前后加上<jsp-config>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
metadata-complete="true">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>mypack.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping><!--用于为Servlet映射一个URL-->
<servlet-name>dispatcher</servlet-name>
<url-pattern>/dispatcher</url-pattern>
</servlet-mapping>
<welcome-file-list><!--设置默认的主页-->
<welcome-file>login.htm</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/mytaglib</taglib-uri>
<taglib-location>/WEB-INF/mytaglib.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
三、运行效果
可知,修改后正常运行。