我有一个带有以下设置的网络项目:
我想从ManagedBean到注入的EJB进行简单的方法调用。 Hibernate配置为EJB的EntityManager。
观点:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<h:html xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<h:head>
</h:head>
<h:body>
<h:form>
<h:inputText value="#{courseController.course.room}"/>
<h:commandButton
value="save" action="#{courseController.addCourse}"/>
</h:form>
</h:body>
</h:html>
视图的ManagedBean:
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import de.uulm.vs.increase.teachinput.ejb.CourseEJB;
import de.uulm.vs.increase.teachinput.model.Course;
@Named(value="courseController")
@ViewScoped
public class CourseController implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@EJB
private CourseEJB courseEJB;
private Course course = new Course();
public String addCourse()
{
courseEJB.addCourse(course);
return "course.xhtml";
}
public Course getCourse()
{
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
EJB:
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import de.uulm.vs.increase.teachinput.model.Course;
@Stateless(mappedName = "courseEJB")
@LocalBean
public class CourseEJB implements CourseEJBLocal {
@PersistenceContext(unitName = "teach-input")
EntityManager em;
public CourseEJB() {
}
public Course addCourse(Course c)
{
em.persist(c);
return c;
}
}
EJB接口:
import javax.ejb.Local;
import de.uulm.vs.increase.teachinput.model.Course;
@Local
public interface CourseEJBLocal {
public Course addCourse(Course c);
}
的persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="teach-input" transaction-type="JTA">
<description>
Persistence unit for the teach-input project.
</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="hibernate.connection.username" value="user"></property>
<property name="hibernate.connection.password" value="pw"></property>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"></property>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
beans.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>
我通过maven构建一个war文件
的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupid</groupId>
<artifactId>artifactid</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>name</name>
<properties>
<hibernate.version>4.2.21.Final</hibernate.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- EJB -->
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>
<!-- JSF -->
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- WELD -->
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.3.2.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>finalname</finalName>
<defaultGoal>install</defaultGoal>
<pluginManagement>
<plugins>
<!-- WildFly plugin to deploy the application -->
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.2.Final</version>
<configuration>
<filename>${project.build.finalName}.war</filename>
<hostname>127.0.0.1</hostname>
<port>9990</port>
<username>user</username>
<password>password</password>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
服务器正在正确部署。通过Web界面调用addCourse()
方法时,将显示以下异常:
WFLYEJB0034:对于方法publicEJB,EJB调用失败方法public course addCourse(Course):javax.ejb.EJBException:java.lang.NullPointerException
因此,注入EJB似乎出现了问题。 我在WildFly文档中查找了它,但缺少EJB配置章节。
提前感谢您的帮助。
答案 0 :(得分:0)
@LocalBean
注释mappedName
属性 - 您是否需要此示例?@EJB CourseEJBLocal
希望有所帮助
答案 1 :(得分:0)
您正在将CDI- @ Named与JSF-Scopes混合使用:
使用
import javax.inject.Named;
import javax.enterprise.context.ConversationScoped;
@Named
@ConversationScoped
public class CourseController implements Serializable {
或
import javax.faces.bean.ViewScoped;
import javax.faces.bean.ManagedBean;
@ManagedBean
@ViewScoped
public class CourseController implements Serializable {