Java Spring(4.0.1)Web应用程序(在NetBeans 8.0.2中设置)。没有Maven或Hibernate。 EclipseLink(JPA 2.1)持久化。 构建和部署没问题。运行时在服务器日志中获取以下内容:
StandardWrapperValve [dispatcher]:servlet调度程序的Servlet.service()抛出异常java.lang.NullPointerException 在main.CourseDao.getAllCoursename(CourseDao.java:29)
CourseDao如下:
package main;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
public class CourseDao {
@PersistenceContext private EntityManager em;
public List<String> getAllCoursename() {
TypedQuery<String> query = em.createQuery("SELECT c.coursename FROM Course AS c", String.class);
return query.getResultList();
}
}
第29行
TypedQuery<String> query = em.createQuery("SELECT c.coursename FROM Course AS c", String.class);
因此,它在数据库查询中变为null。 但是,在主要对象部署为实体的意义上,持久性似乎没有问题:因此,Course.java在部署时在数据库中创建了一个表课程。
persistence.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ReportsPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/reports</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Course.java如下:
package main;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class Course implements Serializable {
private int id;
private String coursecode;
private String coursename;
public Course() {
}
public Course(int id) {
this.id = id;
}
public Course(int id, String coursecode, String coursename) {
this.id = id;
this.coursecode = coursecode;
this.coursename = coursename;
}
@Id
@Column(name="ID")
@GeneratedValue
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="COURSECODE", length=12)
public String getCoursecode() {
return this.coursecode;
}
public void setCoursecode(String coursecode) {
this.coursecode = coursecode;
}
@Column(name="COURSENAME", length=256)
public String getCoursename() {
return this.coursename;
}
public void setCoursename(String coursename) {
this.coursename = coursename;
}
}
COURSE表目前只有3行,没有空值。
applicationContext.xml如下:
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
p:url="jdbc:derby://localhost:1527/reports"
p:username="*******"
p:password="*******" />
</beans>
我已经用Hibernate而不是EclipseLink JPA2.1尝试了所有这些 - 最终遇到同样的问题。
CourseDao目前由index.jsp调用(是的,所以这是一个便宜的第一步;几乎不是最佳实践;但这不是发生错误的地方):
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="main.*"%>
<jsp:useBean id="courseDao" class="main.CourseDao" scope="request" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head> etc </head>
<body>
<hr><ol>
<% for (String coursename : courseDao.getAllCoursename()) { %>
<li> <%= coursename %> </li>
<% } %>
</ol><hr>
</body>
</html>
我想要做的就是从带有JPQL查询的表中选择一列,并将其推送到前面的jsp。简单的东西。任何想法或帮助非常感谢。感谢。
答案 0 :(得分:0)
你不能通过<jsp:useBean id="courseDao" class="main.CourseDao" scope="request" />
访问Spring Bean(这会创建一个&#34;普通&#34; Java Bean,但你的DAO必须是一个Spring Bean)。
你应该看看Spring MVC(例如这个tutorial),所以看看Spring和JSP是如何协同工作的。