我无法从 JSP 页面调用带有可变参数的方法
I can not call a method with varargs from JSP page
我在我的申请中定义了 class DBBase
:
public class DBBase {
public static void close(Statement stmt) {
try {
if (stmt != null) stmt.close();
} catch (Exception ex) {
logger.error("Exception:(", ex);
}
}
public static void close(ResultSet resultSet) {
try {
if (resultSet != null) resultSet.close();
} catch (Exception ex) {
logger.error("Exception:(", ex);
}
}
public static void close(Connection con) {
try {
if (con != null) con.close();
} catch (Exception ex) {
logger.error("Exception:(", ex);
}
}
public static void close(Object... closeable) {
for (Object obj : closeable) {
if (obj != null) {
if (obj instanceof Connection) close((Connection) obj);
else if (obj instanceof ResultSet) close((ResultSet) obj);
else if (obj instanceof Statement) close((Statement) obj);
}
}
}
}
当我从另一个 class 调用关闭方法 DBBase.close(con, stmt, resSet)
时,一切正常,但是当我从 JSP
页面调用此方法时,出现以下错误:
An error occurred at line: 20 in the jsp file: /admin/test.jsp
Generated servlet error:
The method close(Statement) in the type DBBase is not applicable for the arguments (Connection, Statement, ResultSet)
这是我的 JSP 页面:
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="db.DBBase" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Connection con = null;
Statement stmt = null;
ResultSet resSet = null;
try {
// Some code here
} catch (Exception ex) {
ex.printStackTrace();
} finally {
DBBase.close(con, stmt, resSet);
}
%>
<html>
<head>
<title>Test JSP File</title>
</head>
<body>
</body>
</html>
我的 JDK 版本是 1.7.0._51,语言级别为 7
有什么问题?
更新
这是我的服务器和 jsp 配置:
Server Version: Apache Tomcat/5.5.17
Servlet Version: 2.4
JSP Version: 2.0
其实我有 JBoss 4.0.4 GA
更新
关于@AlexC 的评论,我必须更改我的 JBoss 版本以使用可变参数。
JSR-245 for JSP 2.1 在 JSP 和 Tomcat 中定义了可变参数的使用,这是在 7.x 发布周期中完成的。 (见 http://alex-perevalov.livejournal.com/30156.html and https://wiki.apache.org/tomcat/Specifications )
在web.xml中(见http://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html)如果需要限制可以指定JSP版本支持。
正在将您的应用服务器升级到 JSP 2.1 兼容服务器应该允许可变参数。
我在我的申请中定义了 class DBBase
:
public class DBBase {
public static void close(Statement stmt) {
try {
if (stmt != null) stmt.close();
} catch (Exception ex) {
logger.error("Exception:(", ex);
}
}
public static void close(ResultSet resultSet) {
try {
if (resultSet != null) resultSet.close();
} catch (Exception ex) {
logger.error("Exception:(", ex);
}
}
public static void close(Connection con) {
try {
if (con != null) con.close();
} catch (Exception ex) {
logger.error("Exception:(", ex);
}
}
public static void close(Object... closeable) {
for (Object obj : closeable) {
if (obj != null) {
if (obj instanceof Connection) close((Connection) obj);
else if (obj instanceof ResultSet) close((ResultSet) obj);
else if (obj instanceof Statement) close((Statement) obj);
}
}
}
}
当我从另一个 class 调用关闭方法 DBBase.close(con, stmt, resSet)
时,一切正常,但是当我从 JSP
页面调用此方法时,出现以下错误:
An error occurred at line: 20 in the jsp file: /admin/test.jsp
Generated servlet error:
The method close(Statement) in the type DBBase is not applicable for the arguments (Connection, Statement, ResultSet)
这是我的 JSP 页面:
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="db.DBBase" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Connection con = null;
Statement stmt = null;
ResultSet resSet = null;
try {
// Some code here
} catch (Exception ex) {
ex.printStackTrace();
} finally {
DBBase.close(con, stmt, resSet);
}
%>
<html>
<head>
<title>Test JSP File</title>
</head>
<body>
</body>
</html>
我的 JDK 版本是 1.7.0._51,语言级别为 7
有什么问题?
更新
这是我的服务器和 jsp 配置:
Server Version: Apache Tomcat/5.5.17
Servlet Version: 2.4
JSP Version: 2.0
其实我有 JBoss 4.0.4 GA
更新
关于@AlexC 的评论,我必须更改我的 JBoss 版本以使用可变参数。
JSR-245 for JSP 2.1 在 JSP 和 Tomcat 中定义了可变参数的使用,这是在 7.x 发布周期中完成的。 (见 http://alex-perevalov.livejournal.com/30156.html and https://wiki.apache.org/tomcat/Specifications )
在web.xml中(见http://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html)如果需要限制可以指定JSP版本支持。
正在将您的应用服务器升级到 JSP 2.1 兼容服务器应该允许可变参数。