Я начинающий, поэтому сильно не пинайте.  В общем, ситуация такова: есть база данных, есть бин, который через JPA и hibernate работает с полями этой БД, есть манагер, который управляет бинами и есть JSP страничка, куда надо значение из БД вывести. Делаю так: Question.java| Код | @Entity @Table(name = "QUESTIONS") @NamedQueries({ @NamedQuery( name = "getQuestions", query = "select q from Question q" ) }) public class Question implements Serializable { @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "QUESTION") private String value; @Column(name = "ANSWER_ID") private int rightAnswerId;
public Question() { }
public Question(int id, String question) { this.id = id; this.value = question; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getValue() { return value; }
public void setValue(String question) { this.value = question; }
public int getRightAnswerId() { return rightAnswerId; }
public void setRightAnswerId(int rightAnswerId) { this.rightAnswerId = rightAnswerId; } }
|
QuestionManager.java| Код | public class QuestionManager implements Manager { @PersistenceUnit(unitName = "TestingList") EntityManager entityManager;
public void init() { entityManager = Main.getInstance().getEntityManager(); }
public Question getQuestion(int id) { return entityManager.find(Question.class, id); }
public ArrayList<Question> getQuestions() { Query query = entityManager.createNamedQuery("getQuestions"); return (ArrayList<Question>) query.getResultList(); }
public void destroy() { entityManager.clear(); } }
|
index.jsp| Код | <html>
<head> <title>Testing List</title> </head>
<body> [<a href="manage_list.jsp">Manage List</a>] </body> </html>
|
manage_list.jsp| Код | <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<jsp:useBean id="question" class="ru.testinglist.bean.Question"/> <jsp:useBean id="questionManager" class="ru.testinglist.manager.QuestionManager"/>
<html> <c:import url="index.html"/> <body> <table border="1" align="center" width="100%"> <tr> <td align="center" width="80%"><strong>Question</strong></td> <td align="center"><strong>Action</strong></td> </tr>
<%--<c:forEach var="question" items="${questionManager.questions}">--%> <tr> <td><c:out value="${questionManager.questions.question.value}"/></td> <td> <button>Edit</button> <button>Delete</button> </td> </tr> <%--</c:forEach>--%>
</table>
</body> </html>
|
web.xml| Код | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
|
В итоге, если я запускаю это всё как обычное десктопное приложение, то все записи из БД без проблем в консоль выводятся, а когда запускаю как вэб, то при переходе на страничку manage_list.jsp в браузере отображается такая ошибка: | Код | HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /manage_list.jsp at line 25
22: 23: <%--<c:forEach var="question" items="${questionManager.questions}">--%> 24: <tr> 25: <td><c:out value="${questionManager.questions.question.value}"/></td> 26: <td> 27: <button>Edit</button> 28: <button>Delete</button>
Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
javax.servlet.ServletException: javax.servlet.jsp.JspException: An error occurred while evaluating custom action attribute "value" with value "${questionManager.questions.question.value}": An error occurred while getting property "questions" from an instance of class ru.testinglist.manager.QuestionManager (java.lang.NullPointerException) org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779) org.apache.jsp.manage_005flist_jsp._jspService(manage_005flist_jsp.java:120) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException ru.testinglist.manager.QuestionManager.getQuestions(QuestionManager.java:29) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) java.lang.reflect.Method.invoke(Unknown Source) org.apache.taglibs.standard.lang.jstl.ArraySuffix.evaluate(ArraySuffix.java:278) org.apache.taglibs.standard.lang.jstl.ComplexValue.evaluate(ComplexValue.java:107) org.apache.taglibs.standard.lang.jstl.ELEvaluator.evaluate(ELEvaluator.java:204) org.apache.taglibs.standard.lang.jstl.ELEvaluator.evaluate(ELEvaluator.java:163) org.apache.taglibs.standard.lang.jstl.Evaluator.evaluate(Evaluator.java:101) org.apache.taglibs.standard.lang.jstl.Evaluator.evaluate(Evaluator.java:129) org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager.evaluate(ExpressionEvaluatorManager.java:75) org.apache.taglibs.standard.tag.el.core.ExpressionUtil.evalNotNull(ExpressionUtil.java:48) org.apache.taglibs.standard.tag.el.core.OutTag.evaluateExpressions(OutTag.java:99) org.apache.taglibs.standard.tag.el.core.OutTag.doStartTag(OutTag.java:57) org.apache.jsp.manage_005flist_jsp._jspx_meth_c_005fout_005f0(manage_005flist_jsp.java:164) org.apache.jsp.manage_005flist_jsp._jspService(manage_005flist_jsp.java:100) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs. Apache Tomcat/6.0.14
|
Объекты QuestionManager и Question создаются, но вот значения получить не удаётся...
|