日期:2014-05-17  浏览次数:20733 次

JSP页面中嵌入代码,编译报错!!!!
Java code

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%@ page import="com.alipay.services.*"%>
<%@ page import="com.alipay.util.*"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <%
        String test = "";
        TestJava.getTestString();
    %>
    

  </head>
  
  <body>
    
  </body>
</html>


JSP页面代码。

<%@ page import="com.alipay.util.*"%>这已经引入 TestJava这个类了。
Java code
package com.alipay.util;

public class TestJava {
    
    public static String getTestString() {
        return "TestString";
    }
    
}



报错信息:
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 34 in the jsp file: /MyJsp.jsp
TestJava cannot be resolved
31: <body>
32: <%
33: String test = "";
34: TestJava.getTestString();
35: %>
36: </body>
37: </html>


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)



请教~~~

------解决方案--------------------
<%@ page import="com.alipay.services.*"%>
<%@ page import="com.alipay.util.*"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>

把這些放在第一行吧,如果可以,嘗試看下jsp翻譯后的java文件,或許能看出什麽來。
------解决方案--------------------
探讨

<body>
<%
Test.getTestString(