我对lista-dados.jsp的问题有疑问。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="utf-8" />
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="dao" class="bean.ListaDados" scope="session"/>
<table>
<c:forEach var="contato" items="${dao.lista}">
<tr>
<td>${contato.nome}</td>
<td>${contato.email}</td>
<td>${contato.endereco}</td>
<td>${contato.dataNascimento.time}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
My Class ListaDados:(WEBContent / WEB-INF / classes / bean)
package bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import br.com.cad.basica.Contato;
import br.com.cad.dao.ConnectDb;
public class ListaDados extends ConnectDb {
Connection c = this.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
public List<Contato> getContatos(String cpf) {
try {
List<Contato> contatos = new ArrayList<Contato>();
ps = c.prepareStatement("select * from dados_cadastro where pf_cpf= ? ");
ps.setString(1, cpf);
ResultSet rs = ps.executeQuery();
if (rs.next()){
Contato contato = new Contato();
contato.setEmail(rs.getString("pf_email"));
contato.setNome( rs.getString("pf_nome") );
contato.setEmail(rs.getString("pf_email"));
contato.setEndereco(rs.getString("pf_endereco"));
Calendar data = Calendar.getInstance();
data.setTime(rs.getDate("pf_dt_nasc"));
contato.setDataNascimento(data);
contatos.add(contato);
}
rs.close();
ps.close();
return contatos;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
这是我的checa.jsp:
<%@ page language="java" import="java.sql.Connection,java.sql.DriverManager,java.sql.SQLException" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="utf-8" />
<title>Painel Administrativo</title>
<c:choose>
<c:when test="${ user eq null }">
<jsp:forward page="erroLogin.jsp" />
</c:when>
<c:otherwise>
<p><h2>Bem-vindo, ${ user.nome }</p></h2>
</head>
<body>
<form action="lista-dados.jsp" method="POST">
<label for="cpf">Busque o CPF</label>
<br>
<input type="text" id="cpf" required name="cpf">
<button type="submit">Consultar</button>
<%
String msg=(String) request.getAttribute("msg");
if(msg!=null)
out.println(msg);
%>
</c:otherwise>
</c:choose>
</form>
</body>
</html>
当我在我的servlet checa.jsp中输入一个doc号并按下一个提交按钮时,我收到一个错误:
HTTP Status 500 - /lista-dados.jsp (line: 14, column: 0) The value for the useBean class attribute bean.ListaDados is invalid.
org.apache.jasper.JasperException: /lista-dados.jsp (line: 14, column: 0) The value for the useBean class attribute bean.ListaDados is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:149)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1237)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1182)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Generator.generate(Generator.java:3503)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
我不明白这个错误,有什么问题?
错误继续我认为我的bean有错误,但我不知道什么!!
package bean;
import br.com.cad.basica.Contato;
import br.com.cad.dao.ConnectDb;
public class ListaDados extends ConnectDb {
public List<Contato> getContatos() {
try {
List<Contato> contatos = new ArrayList<Contato>();
PreparedStatement stmt = this.getConnection()
.prepareStatement("select * from dados_cadastro where pf_cpf= ?");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
contatos.add(contato);
}
rs.close();
stmt.close();
return contatos;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Contato contato = new Contato();
contato.setNome(rs.getString("pf_nome"));
有一个jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="bean" class="bean.ListaDados" />
<jsp:setProperty name="bean" property="*"/>
<table>
<c:forEach var="contato" items="${bean.contatos}">
<tr>
<td>${contato.nome}</td>
</c:forEach>
</table>
我认为错误只是我的ListaDados类,但我不知道是否正确?
答案 0 :(得分:0)
您必须按照以下规则更正您的课程:
以下是区分JavaBean与其他Java类的独特特征:
它提供了一个默认的无参数构造函数。
它应该是可序列化的并实现Serializable接口。
它可能有许多可以读取或写入的属性。
这些属性可能有许多“getter”和“setter”方法。
public class ListaDados extends ConnectDb implements java.io.Serializable{
public ListaDados(){
}
//Your Code haere
}