Tomcat,JPA,Eclipse - 名称未绑定在此上下文中

时间:2015-10-03 10:21:19

标签: java eclipse tomcat jpa jdbc

您好我写了简单的电话簿应用程序,当我在表单中键入id并从数据库中单击按钮搜索记录时我有这个例外

  

javax.naming.NameNotFoundException:名称   [com.kamil.controller.PersonServlet / personDao]不受此限制   语境。无法找到[com.kamil.controller.PersonServlet]。     org.apache.naming.NamingContext.lookup(NamingContext.java:819)     org.apache.naming.NamingContext.lookup(NamingContext.java:167)     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)     org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)     org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:625)     org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:318)     java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)     java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)     org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     java.lang.Thread.run(未知来源)

只有我第一次在服务器上运行应用程序时才会出现这种情况,下次我有声明:请求的资源不可用。

的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="test">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.kamil.model.Person</class>
    <properties>
    <property name="hibernate.connection.username" value="root"/>
    <property name="hibernate.connection.password" value=""/>
    <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
    <property name="hibernate.connection.url" value="jdbc:mysql://localhost/phonebook?charsetEncoding=utf8"/>
    <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

    </properties>
    </persistence-unit>
</persistence>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>test</display-name>
  <welcome-file-list>
   <welcome-file>personinfo.jsp</welcome-file>
  </welcome-file-list>
  <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>ds/phonebook</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

PersonServlet.java

 package com.kamil.controller;

import com.kamil.dao.PersonDaoLocal;
import com.kamil.model.Person;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "PersonServlet", urlPatterns = {"/PersonServlet"})
public class PersonServlet extends HttpServlet {
    @EJB
    private PersonDaoLocal personDao;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        String personIdStr = request.getParameter("personId");
        int personId=0;
        if(personIdStr!=null && !personIdStr.equals("")){
            personId=Integer.parseInt(personIdStr);    
        }
        String firstname = request.getParameter("firstname");
        String lastname = request.getParameter("lastname");
        String numberStr = request.getParameter("number");
        int number=0;
        if(numberStr!=null && !numberStr.equals("")){
            number=Integer.parseInt(numberStr);
        }
        Person person = new Person(personId, firstname, lastname, number);

        if("Add".equalsIgnoreCase(action)){
            personDao.addPerson(person);
        }else if("Edit".equalsIgnoreCase(action)){
            personDao.editPerson(person);
        }else if("Delete".equalsIgnoreCase(action)){
            personDao.deletePerson(personId);
        }else if("Search".equalsIgnoreCase(action)){
            person = personDao.getPerson(personId);
        }
        request.setAttribute("person", person);
        request.setAttribute("allPersons", personDao.getAllPersons());
        request.getRequestDispatcher("personinfo.jsp").forward(request, response);
    }

Person.java

package com.kamil.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name="person")
@NamedQueries({@NamedQuery(name="Person.getAll",query="SELECT e FROM Person e")})
public class Person {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column
    private int personId;
    @Column
    private String firstname;
    @Column
    private String lastname;
    @Column 
    private int number;
    public int getPersonId() {
        return personId;
    }
    public void setPersonId(int personId) {
        this.personId = personId;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public Person(int personId, String firstname, String lastname, int number) {
        super();
        this.personId = personId;
        this.firstname = firstname;
        this.lastname = lastname;
        this.number = number;
    }

    Person(){}
}

PersonDao.java

package com.kamil.dao;

import com.kamil.model.Person;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;


@Stateless
public class PersonDao implements PersonDaoLocal {
    @PersistenceContext
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
    private EntityManager em = emf.createEntityManager();

    @Override
    public void addPerson(Person person) {
        em.persist(person);
    }

    @Override
    public void editPerson(Person person) {
        em.merge(person);
    }

    @Override
    public void deletePerson(int personId) {
        em.remove(getPerson(personId));
    }

    @Override
    public Person getPerson(int personId) {
        return em.find(Person.class, personId);
    }

    @Override
    public List<Person> getAllPersons() {
        return em.createNamedQuery("Person.getAll").getResultList();
    }

}

PersonDaoLocal.java

package com.kamil.dao;

import com.kamil.model.Person;
import java.util.List;
import javax.ejb.Local;


@Local
public interface PersonDaoLocal {

    void addPerson(Person person);

    void editPerson(Person person);

    void deletePerson(int personId);

    Person getPerson(int personId);

    List<Person> getAllPersons();

}

personinfo.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Information</title>
    </head>
    <body>
        <h1>Person  Information</h1>
        <form action="./PersonServlet" method="POST">
            <table>
                <tr>
                    <td>Person ID</td>
                    <td><input type="text" name="personId" value="${person.personId}" /></td>
                </tr>
                <tr>
                    <td>First Name</td>
                    <td><input type="text" name="firstname" value="${person.firstname}" /></td>
                </tr>
                <tr>
                    <td>Last Name</td>
                    <td><input type="text" name="lastname" value="${person.lastname}" /></td>
                </tr>
                <tr>
                    <td>Number</td>
                    <td><input type="text" name="number" value="${person.number}" /></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" name="action" value="Add" />
                        <input type="submit" name="action" value="Edit" />
                        <input type="submit" name="action" value="Delete" />
                        <input type="submit" name="action" value="Search" />
                    </td>                
                </tr>            
            </table>
        </form>        
        <br>
        <table border="1">
            <th>ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Number</th>
            <c:forEach items="${allPersons}" var="pers">
                <tr>
                    <td>${pers.personId}</td>
                    <td>${pers.firstname}</td>
                    <td>${pers.lastname}</td>
                    <td>${pers.yearLevel}</td>
                </tr>
            </c:forEach>
        </table>  
    </body>
</html>

View full project on github

0 个答案:

没有答案