无法在 Jboss7 EAP7 EAP 中上传我的 Web 应用程序

Cannot upload my Web Applicartion in Jboss7 EAP7 EAP

我必须测试这个网络应用程序,但是当我尝试在 JBoss 7 EAP 上部署时,这是错误,也许我忘记了什么?

这是应用程序抛出的异常:

Cannot upload deployment: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"WebAppGuestbooks.war\".INSTALL" => 
"org.jboss.msc.service.StartException in service jboss.deployment.unit.\"WebAppGuestbooks.war\".INSTALL: WFLYSRV0153: 
Failed to process phase INSTALL of deployment \"WebAppGuestbooks.war\" Caused by: 
org.jboss.as.server.deployment.DeploymentUnitProcessingException: WFLYEE0041: Component class it.matteo.nesea.ejb.GuestDao for 
component GuestDao has errors: 
 WFLYJPA0033: Can't find a persistence unit named null in deployment
 \"WebAppGuestbooks.war\""},"WFLYCTL0180: Services with missing/unavailable dependencies" => 
["jboss.deployment.unit.\"WebAppGuestbooks.war\".weld.weldClassIntrospector is missing 
[jboss.deployment.unit.\"WebAppGuestbooks.war\".beanmanager]","jboss.deployment.unit.\"WebAppGuestbooks.war\".batch.environment 
is missing [jboss.deployment.unit.\"WebAppGuestbooks.war\".beanmanager]"]}

这是我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">

  <persistence-unit name="GuestbookPU" transaction-type="JTA">
  <class>it.matteo.nesea.dao.jpa</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="$objectdb/db/guests.odb"/>
      <property name="javax.persistence.jdbc.user" value="admin"/>
      <property name="javax.persistence.jdbc.password" value="admin"/>
    </properties>
  </persistence-unit>
</persistence>

这是 Ejb GuestDAO:

package it.matteo.nesea.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import it.matteo.nesea.dao.jpa.Guest;

@Stateless
public class GuestDao {
    // Injected database connection:
    @PersistenceContext private EntityManager em;

    // Stores a new guest:
    public void persist(Guest guest) {
        em.persist(guest);
    }

    // Retrieves all the guests:
    public List<Guest> getAllGuests() {
        TypedQuery<Guest> query = em.createQuery(
                "SELECT g FROM Guest g ORDER BY g.id", Guest.class);
        return query.getResultList();
    }
}

这是 Jpa POJO Class 访客:

package it.matteo.nesea.dao.jpa;

import java.io.Serializable;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Guest implements Serializable {
    private static final long serialVersionUID = 1L;

    // Persistent Fields:
    @Id 
    @GeneratedValue
    Long id;
    private String name;
    private Date signingDate;

    // Constructors:
    public Guest() {
    }

    public Guest(String name) {
        this.name = name;
        this.signingDate = new Date(System.currentTimeMillis());
    }

    // String Representation:
    @Override
    public String toString() {
        return name + " (signed on " + signingDate + ")";
    }
}

这是一个 Servlet GuestServlet:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import it.matteo.nesea.dao.jpa.Guest;
import it.matteo.nesea.ejb.GuestDao;

@WebServlet(name="GuestServlet", urlPatterns={"/guest"})
public class GuestServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

    // Injected DAO EJB:
    @EJB GuestDao guestDao;

    @Override
    protected void doGet(
        HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // Display the list of guests:
        request.setAttribute("guests", guestDao.getAllGuests());
        request.getRequestDispatcher("/guest.jsp").forward(request, response);
    }

    @Override
    protected void doPost(
        HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // Handle a new guest:
        String name = request.getParameter("name");
        if (name != null)
            guestDao.persist(new Guest(name));

        // Display the list of guests:
        doGet(request, response);
    }
}

这是一个 JSP 页:

<%@page contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*,it.matteo.nesea.dao.jpa.Guest"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <title>JPA Guest Book Web Application</title>
    </head>

    <body>
        <form method="POST" action="guest">
            Name: <input type="text" name="name" />
            <input type="submit" value="Add" />
        </form>

        <hr><ol> <%
            @SuppressWarnings("unchecked") 
            List<Guest> guests = (List<Guest>)request.getAttribute("guests");
            if (guests != null) {
                for (Guest guest : guests) { %>
                    <li> <%= guest %> </li> <%
                }
            } %>
        </ol></hr>
     </body>

This is the path of Project

您的 persistence.xml 文件位置错误。 JPA 使用约定来查找您的 persistence.xml,因此您已将文件放在正确的位置。

根据 Oracle docs:

The JAR file or directory whose META-INF directory contains persistence.xml is called the root of the persistence unit.

  • If you package the persistent unit as a set of classes in an EJB JAR file, persistence.xml should be put in the EJB JAR’s META-INF directory.
  • If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be located in the WAR file’s WEB-INF/classes/META-INF directory.

如果 persistence.xml 文件位于 src/META-INF/(如果您使用 MAVEN,路径为 src/resources/META-INF),它将被打包到您的 war 在 WEB-INF/classes/META-INF 文件夹中,作为持久性单元的根。