java - 无法使用 Eclipse Juno 编译 JSP 类

标签 java eclipse jsp

我无法让 JSP 页面识别我的 Java 类(DbSettings) 我相信下面我的文件目录设置正确。

问题在这一行崩溃: rs = DbSettings.getResultSet

有人能指出我如何解决这个问题的正确方向吗?

已解决:必须将类导入到 JSP 文件中 在顶部添加了导入。

<%@page import="classes.DbSettings"  %>
<%@page import="classes.DateValidator"  %>

文件目录: 项目名称:货币

Currency
-Deployment Description
-JAX-WS Web Services
-Java Resources
--src
---classes (Package)
----DateValidator.java
----DbSettings.java
--Libraries
----Apache
----EAR Libraries
----JRE
----Web App

-JavaScript Resources (default sub cats)
-build
-libs
ojdbc7.jar

-WebContent
--META-INF
--WEB-INF
---file.jsp
---file.jsp
---file.jsp

X .classpath
X .project

DbSettings.java(无法在 JSP 文件中识别此类)

package classes;  <--- New Package
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DbSettings {
    private static Connection getConnection() throws Exception {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = null;
        conn = DriverManager.getConnection(
                "jdbc:oracle:thin:@uschduxcls004sg09:1527:xxxxx", "xxxxx",
                "xxxxx");
        return conn;

    }

    public static final ResultSet getResultSet(String command) throws Exception {
        Connection conn = getConnection();
        Statement stmt = null;
        stmt = conn.createStatement();
        System.out.println("Statement was Succesful");
        ResultSet rs = null;
        rs = stmt.executeQuery(command);
        System.out.println("Query was Succesful");
        return rs;

    }
}

JSP 文件片段:

<%@page import="java.util.Calendar"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="classes.DbSettings"  %>
<%@page import="classes.DateValidator"  %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
<script language="javascript">
    // Set frametest1 level
    level = 0;

    function submitCurrency() {
        var valid = checkForm(document.currency_form);

        if (valid)
            document.currency_form.submit();
    }

    function checkForm(theForm) {
        // This function validates the entries entered by the user before passing the values
        // to the next page.

        // Check number of units
        if (theForm.txtUnits.value == "") {
            alert("Please enter the number of units you wish to convert.");
            theForm.txtUnits.focus();
            return (false);
        } else {
            result = isNaN(theForm.txtUnits.value);
            if (result == true) {
                alert("Please enter only numeric values in the \"Units\" field. No commas or other characters are needed or recognized.");
                theForm.txtUnits.focus();
                return (false);
            }
        }
        return true;
    }
</script>
</head>
<body>
    <%
        // Declare variables
        boolean fEmptyRecordset, fFirstPass, fNeedRecordset;
        int i; // as Integer
        fEmptyRecordset = false;
        fFirstPass = true;
        fNeedRecordset = true;
        ResultSet rs = null;
        String message = null;
        try {
            rs = DbSettings
                    .getResultSet("SELECT BLMBG_CURR_CODE,BLMBG_CURR_NAME FROM AON_CURRENCY_SDO ORDER BY BLMBG_CURR_NAME");
            fEmptyRecordset = rs.first();
        } catch (Exception ex) {
            fEmptyRecordset = true;
            message = ex.getMessage();
        }
        String aMonth = "";
        int aDate, aYear;
        if ("results".compareToIgnoreCase(request.getParameter("action")) == 0) {
            aMonth = request.getParameter("selMonth");
            aDate = Integer.parseInt(request.getParameter("selDate"));
            aYear = Integer.parseInt(request.getParameter("selYear"));
        } else {
            switch (Calendar.getInstance().get(Calendar.MONTH)) {
            case Calendar.JANUARY:
                aMonth = "JAN";
            case Calendar.FEBRUARY:
                aMonth = "FEB";
            case Calendar.MARCH:
                aMonth = "MAR";
            case Calendar.APRIL:
                aMonth = "APR";
            case Calendar.MAY:
                aMonth = "MAY";
            case Calendar.JUNE:
                aMonth = "JUN";
            case Calendar.JULY:
                aMonth = "JUL";
            case Calendar.AUGUST:
                aMonth = "AUG";
            case Calendar.SEPTEMBER:
                aMonth = "SEP";
            case Calendar.NOVEMBER:
                aMonth = "NOV";
            case Calendar.DECEMBER:
                aMonth = "DEC";
            }
            aDate = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
            aYear = Calendar.getInstance().get(Calendar.YEAR);
        }
        if (message != null || fEmptyRecordset) {
            out.println("<tr><td>");
            out.println(message);
            out.println("</td></tr>");
    %>
    <%
        } else {
    %>

堆栈跟踪

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

An error occurred at line: 53 in the jsp file: /currency_form.jsp
DbSettings cannot be resolved
50:         ResultSet rs = null;
51:         String message = null;
52:         try {
53:             rs = DbSettings
54:                     .getResultSet("SELECT BLMBG_CURR_CODE,BLMBG_CURR_NAME FROM AON_CURRENCY_SDO ORDER BY BLMBG_CURR_NAME");
55:             fEmptyRecordset = rs.first();
56:         } catch (Exception ex) {


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:485)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:379)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

最佳答案

现代 Java 不鼓励使用不在包内的类,以至于它们在许多情况下不起作用。将您的类放入包中(在 .java 文件顶部使用 package 语句)——所有这些包。

关于java - 无法使用 Eclipse Juno 编译 JSP 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27832176/

相关文章:

Java 8 使用 2 个字段进行排序

java - 所有 gms/firebase 库必须使用完全相同的版本。找到版本 15.1.0、15.0.2、15.0.1、15.0.0。

Java GUI 计算器文本字段

java - 将数据从 JSON Android 保存到 SQLite

java - 在 Android 中使用本地 json 文件

eclipse - Vitamio 库没有链接?找不到方法 io.vov.vitamio.LibsChecker.checkVitamioLibs

eclipse - 64 位 Windows 7 上的 32 位 Java,用于在 Eclipse Juno 中运行 JBoss JSP 编辑器

eclipse - 无法将服务器添加到移动的工作区

jsp - 当EL变量不为空时添加前导空格

java - 我在哪里可以下载 JSTL 标签库?即 jSTL.jar 和 standard.jar