【发布时间】:2013-02-14 13:53:06
【问题描述】:
我在查看 JSP 页面中的数组列表中的记录时遇到问题。每次我通过 javascript onload 事件自动加载我的 JSP 页面时,都会显示数据但进程不会停止。
Categoria类:
package proyecto.modelo;
public class Categoria {
private int idcategoria;
public int getIdcategoria() {
return idcategoria;
}
public void setIdcategoria(int idcategoria) {
this.idcategoria = idcategoria;
}
}
BaseDAO类:
package proyecto.dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDAO {
protected void cerrarConexion(Connection con) throws RuntimeException {
try {
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarConexion: " + se);
}
}
protected void cerrarResultSet(ResultSet rs) throws RuntimeException {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarResultSet: " + se);
}
}
protected void cerrarStatement(PreparedStatement stmt)
throws RuntimeException {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarStatement: " + se);
}
}
protected void cerrarCallable(CallableStatement callstmt)
throws RuntimeException {
try {
if (callstmt != null) {
callstmt.close();
}
} catch (SQLException se) {
System.err.println("Error: cerrarCallable: " + se);
}
}
}
CategoriaDAO类:
package proyecto.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Categoria;
import proyecto.util.ConexionBD;
public class CategoriaDAO extends BaseDAO {
public Collection<Categoria> listarIdCat() throws DAOExcepcion{
Collection<Categoria> = new ArrayList<Categoria>();
String query = "SELECT id_categoria from categoria ";
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con=ConexionBD.obtenerConexionDirecta();
stmt=con.prepareStatement(query);
rs=stmt.executeQuery();
while(rs.next()) {
Categoria vo=new Categoria();
vo.setIdcategoria(rs.getInt("id_categoria"));
c.add(vo);
}
} catch (SQLException e) {
System.err.println(e.getMessage());
throw new DAOExcepcion(e.getMessage());
} finally {
this.cerrarStatement(stmt);
this.cerrarResultSet(rs);
this.cerrarConexion(con);
}
return c;
}
}
CategoriaNegocio类:
package proyecto.negocio;
import java.util.Collection;
import java.util.List;
import proyecto.dao.CategoriaDAO;
import proyecto.excepcion.DAOExcepcion;
import proyecto.modelo.Categoria;
public class CategoriaNegocio {
public Collection<Categoria> listarIdCat() throws DAOExcepcion {
CategoriaDAO dao = new CategoriaDAO();
Collection<Categoria> lista = dao.listarIdCat();
return lista;
}
}
Servlet doPost() 方法:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CategoriaNegocio negocio = new CategoriaNegocio();
try {
Collection<Categoria> lista = negocio.listarIdCat();
request.setAttribute("IDCATEGORIA", lista);
} catch (DAOExcepcion e) {
System.out.println(e.getMessage());
}
RequestDispatcher rd = request.getRequestDispatcher("listar_idcat.jsp");
rd.forward(request, response);
}
JSP:
<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<script type="text/javascript">
window.onload = function() {
document.formulario.submit()
};
document.close();
</script>
</head>
<body>
<form action="ListarIdCatServlet" method="post" name="formulario"></form>
<table>
<c:forEach items="${IDCATEGORIA}" var="c">
<tr>
<td>${c.idcategoria}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
【问题讨论】:
-
老兄,正确格式化你的问题!并删除所有不必要的代码,以便我们仅尝试了解与您的问题实际相关的部分
-
你能告诉我你为什么要在页面加载时提交表单吗?
-
我想要的是让页面在没有按钮的情况下自动加载
-
使用引号按钮格式化引号,使用代码按钮格式化代码。不要同时使用引号和代码按钮来格式化代码。更重要的是,在您的整个问题中,您没有任何引号,因此您根本不应该在整个问题中使用引号按钮。
-
我修复了疯狂的格式。我注意到您甚至删除了所有
>字符,使代码在语法上无效。你不应该那样做。只需选择代码并按代码按钮。就这样。真的。
标签: java javascript jsp servlets