1

我有一个 JSP 文件 member.jsp,如下所示:

<%@ page import="java.util.*" %>
<jsp:include page="/html_functions.jsp" />

<% String heading = "Header" %>
<%= formStart("a_form") %>
<%= printPageHeader(heading) %>
<%= startMyLi() %>
<%= endLi() %>
<%= formEnd() %>

我的 html_functions.jsp 如下:

<%!

public String formStart(String name) {
    String structure = "<div id=\"content\"><form name=\"" + name + "\" method=\"post\"><ul>";
    return structure;
}//formStart

public String printPageHeader(String name) {
    String structure = "<li class=\"listLi\">\n<h3 class=\"formHeader\">" + name + ".</h3></li>";
    return structure;
}//printPageHeader

public String startMyLi() {
    String structure = "<li class=\"listLi\">";
    return structure;
}//startMyLi

public String endLi() {
    return ("</li>");
}//endMyLi

public String displayWithSpan(String str) {
    String structure = "<span class=\"labelSpan\">" + str + "</span>";
    return structure;
}//displayWithSpan

public String displayInputElement(String name) {
    return("Hiiiii");
}//displayInputElement

%>

但是,调用 member.jsp 文件时出现异常。这是我得到的例外:

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

An error occurred at line: xx in the jsp file: /member.jsp
The method formStart(String) is undefined for the type add_005fmember_jsp

所有其他方法也是如此。我在哪里犯错?

谢谢

4

1 回答 1

4

使用

<%@ include file="/html_functions.jsp" %>

工作正常,而不是<jsp:include page="html_functions.jsp" />

有两种方法可以将文件包含到应用程序的 JSP 页面中。这些如下:

  1. <%@include file="relativeURL" %>
  2. <jsp:include page="relativeURL" />

第一种情况在编译时包括调用 JSP 文件中的文件或文件的文本或代码,并在稍后执行。

并检查 html_functions.jsp 是否放置在正确的路径中。

于 2012-10-11T10:51:52.587 回答