IDEA中web.xml无法识别taglib

 

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" %>

helloapp

:<%=request.getAttribute("USER")%>


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(修改后)

改为,由改为







  1.1
  2.1
  mytaglib
  /mytaglib

  
    hello
    mypack.HelloTag
    empty
    Just Says Hello
  


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(修改后)

需要在的前后加上




    
        dispatcher
        mypack.DispatcherServlet
    

    
        dispatcher
        /dispatcher
    

    
        login.htm
    

    
    
        /mytaglib
        /WEB-INF/mytaglib.tld
    
    

三、运行效果

IDEA中web.xml无法识别taglib_第1张图片

IDEA中web.xml无法识别taglib_第2张图片

可知,修改后正常运行。

你可能感兴趣的:(JavaWeb)