【问题标题】:<jsp:include tag doesnt work <jsp:include page="Remote.jsp"/><jsp:include 标签不起作用 <jsp:include page="Remote.jsp"/>
【发布时间】:2011-04-01 13:15:44
【问题描述】:

Remote.jsp 代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Remote Value</title>
</head>
<body>
<% 
     File remote = new File("D:\\test.txt");
     FileReader fr = new FileReader(remote);
     int ch;
     while((ch = fr.read()) != -1)
       out.println("<p>" + (char)ch + "</p>");
     fr.close();
  %>
</body>
</html>

&lt;jsp:include page="Remote.jsp"/&gt;嵌入到另一个jsp中,路径正确。 但是每次遇到异常。

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 11 in the jsp file: /Remote.jsp File cannot be resolved to a type

【问题讨论】:

    标签: jsp tags include


    【解决方案1】:

    Remote.jsp 无法自行编译时,你会得到这个异常。您可以通过直接打开文件来测试它,而无需将其包含在另一个页面中。然后,您会看到有关原因的明显异常。据我所知,您至少缺少 Remote.jsp 中的 java.io.* 导入。

    <%@page import="java.io.*" %>
    

    与具体问题无关,这是一种糟糕的方法。它不仅可能导致其他 JSP 中的 HTML 语法无效(您不能嵌套 &lt;html&gt; 等等)。考虑使用从磁盘读取文件并将其流式传输到响应的 servlet。然后你就可以做

    <div style="white-space: pre;">
        <jsp:include page="fileservlet/test.txt" />
    </div>
    

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 2011-12-14
      • 1970-01-01
      • 2017-07-16
      • 1970-01-01
      相关资源
      最近更新 更多