Java + Spring:SEVERE Servletservice

我不知道我的listContacts在控制器map.put("contactList", contactService.listContact()); 有人能帮助我吗?

SEVERE:Servlet.service()for servlet [dispatcher]与path [/ test]的上下文抛出异常[Request processing failed; 嵌套异常是java.lang.NullPointerException],其根本原因是java.lang.NullPointerException在pl.ivmx.contact.controller.ContactController.listContacts(ContactController.java:26)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在太阳.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at org.springframework上的java.lang.reflect.Method.invoke(Method.java:601)处的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)。 web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)位于org.springframework.web.servlet的org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)。 mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)at org.springframework.web.servlet.Dispatc 在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)处的org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)处的herServlet.doService(DispatcherServlet.java:852) .servlet.http.HttpServlet.service(HttpServlet.java:621)at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305 )在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)at org.apache.catalina.core.StandardContextValve。调用(StandardContextValve.java:123)在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)at org.apache .catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)at org.apache.catalina.va lorg.accessLogValve.invoke(AccessLogValve.java:927)在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:585)at org.apache.tomcat.util.net.JIoEndpoint在Java中,java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603)处的java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)处为$ SocketProcessor.run(JIoEndpoint.java:312)。 lang.Thread.run(Thread.java:722)

package pl.ivmx.contact.controller;

import java.util.Map;    
import pl.ivmx.contact.dao.ContactDAO;
import pl.ivmx.contact.form.Contact;
import pl.ivmx.contact.service.ContactService;    
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ContactController {

    @Autowired
    private ContactService contactService;

    @RequestMapping("/contact")
    public String listContacts(Map<String, Object> map) {
        map.put("contact", new Contact());
        map.put("contactList", contactService.listContact());       

        return "/contact";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addContact(@ModelAttribute("contact") Contact contact,
            BindingResult result) {

        contactService.addContact(contact);

        return "redirect:/contact";
    }

    @RequestMapping("/delete/{contactId}")
    public String deleteContact(@PathVariable("contactId") Integer contactId) {

        contactService.removeContact(contactId);

        return "redirect:/contact";
    }   
}

package pl.ivmx.contact.dao;

import java.util.List;

import pl.ivmx.contact.form.Contact;

public interface ContactDAO {

    public void addContact(Contact contact);
    public List<Contact> listContact();
    public void removeContact(Integer id);
}

包pl.ivmx.contact.dao;

import java.util.List;

import pl.ivmx.contact.form.Contact;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

@Repository
public class ContactDAOImpl implements ContactDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void addContact(Contact contact) {
        sessionFactory.getCurrentSession().save(contact);
    }

    public List<Contact> listContact() { 
        return  sessionFactory.getCurrentSession().createQuery("from Contact").list();      
    }

    public void removeContact(Integer id) {
        Contact contact = (Contact) sessionFactory.getCurrentSession().load(
                Contact.class, id);
        if (null != contact) {
            sessionFactory.getCurrentSession().delete(contact);
        }

    }
}

包pl.ivmx.contact.service;

import java.util.List;

import pl.ivmx.contact.form.Contact;

public interface ContactService {

    public void addContact(Contact contact);
    public List<Contact> listContact();
    public void removeContact(Integer id);
}

包pl.ivmx.contact.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import pl.ivmx.contact.dao.ContactDAO;
import pl.ivmx.contact.form.Contact;

@Service
public class ContactServiceImpl implements ContactService {

    @Autowired
    private ContactDAO contactDAO;

    @Transactional
    public void addContact(Contact contact) {
        contactDAO.addContact(contact);
    }

    @Transactional
    public List<Contact> listContact() {
        return contactDAO.listContact();
    }

    @Transactional
    public void removeContact(Integer id) {
        contactDAO.removeContact(id);
    }  
}

包pl.ivmx.contact.form;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="CONTACTS")
public class Contact {

    @Id
    @Column(name="ID")
    @GeneratedValue
    private Integer id;

    @Column(name="FIRSTNAME")
    private String firstname;

    @Column(name="LASTNAME")
    private String lastname;

    @Column(name="EMAIL")
    private String email;

    @Column(name="TELEPHONE")
    private String telephone;


    public String getEmail() {
        return email;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public String getFirstname() {
        return firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
}

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    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">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
        <welcome-file>/pages/index.jsp</welcome-file>
    </welcome-file-list>

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:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="pl.ivmx.contact" />  

<!--    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="messages_en.properties" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean> -->


    <import resource="commonContext.xml" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="userTestDao" class="pl.ivmx.dao.impl.UserTestDaoImpl">
    <!--     <property name="dataSource" ref="dataSource" />   -->
        <property name="sessionFactory" ref="sessionFactory" />             
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >

    <!--    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> -->
        <property name="dataSource" ref="dataSource" />                 
         <property name="configLocation" value="META-INF/hibernate.cfg.xml" />    
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
    <!--    <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>      
            </props>
        </property>         
        <property name="annotatedClasses">
            <list>
                <value>pl.ivmx.model.UserTest</value>
            </list>
        </property>    -->     
    </bean>    


    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

调度员servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"                                 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--  <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />    -->

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

        <property name="urlMap">
            <map>
                <entry key="/index.do"> <ref bean="index" /></entry>
                <entry key="/registration.do"> <ref bean="registration" /></entry>
                <entry key="/usertestlist.do"> <ref bean="usertest" /></entry>  
                <entry key="/contact.do"> <ref bean="contact" /></entry>        
            </map>
        </property>
    </bean>

    <bean id="index" class="pl.ivmx.web.IndexController"/>

        <bean id="registrationValidator" class="pl.ivmx.validation.RegistrationValidator" />  
        <bean id="registration" class="pl.ivmx.web.RegistrationFormController" >                 
            <property name="commandName"><value>userTest</value></property> 
            <property name="commandClass"><value>pl.ivmx.model.UserTest</value></property> 
            <property name="validator"><ref local="registrationValidator"/></property>  
            <property name="formView"><value>registration</value></property> 
            <property name="successView"><value>registrationsuccess</value></property> 
            <property name="userTestDao"><ref bean="userTestDao"/></property>               
        </bean>     

        <bean id="usertest" class="pl.ivmx.web.UserTestController">                  
             <property name="userTestDao"><ref bean="userTestDao"/></property>          
        </bean>     

    <bean id="contact" class="pl.ivmx.contact.controller.ContactController">
    </bean>

    <bean id="contactService" class="pl.ivmx.contact.service.ContactServiceImpl">
    </bean>
    <bean id="contactDAO" class="pl.ivmx.contact.dao.ContactDAOImpl">
    </bean>

</beans>

它看起来像这里抛出的方法中的NullPointerException:

@RequestMapping("/contact")
public String listContacts(Map<String, Object> map) {
    map.put("contact", new Contact());
    map.put("contactList", contactService.listContact());       

    return "/contact";
}

我会调试并检查contactService是否已正确自动装配。

空指针是第26行,如果这是包含contactService.listContact()的行,那么contactService为空。 如果它是上面的map线,那么map是空的。

我不确定,但尝试添加:

<context:component-scan base-package="pl.ivmx.contact" />

到dispatcher-servlet.xml中,也可能是注释没有得到处理


您在方法中返回"/contact"作为要显示的视图:

@RequestMapping("/contact")
public String listContacts(Map<String, Object> map) {
    map.put("contact", new Contact());
    map.put("contactList", contactService.listContact());       

    return "/contact"; // <---- here!
}

这意味着你应该在某处定义一个名为/ contact的视图,但我没有看到它。 您需要有一个与您定义的值相对应的视图。

您可能想要返回一个类似"contact"而不是"/contact" ,然后在webapp / WEB-INF / views文件夹中使用contact.jsp(假设您使用maven标准目录布局)。


我没有看到任何明显的...可以sessionFactory为空? 应该很容易找到,寿。 逐句通过调试器中的代码。

链接地址: http://www.djcxy.com/p/27881.html

上一篇: Java+Spring: SEVERE Servletservice

下一篇: Paypal Express Checkout : Creating subscriptions / recurring payments