当前位置: 首页 > 知识库问答 >
问题:

Java Spring:严格的Servletservice

松增
2023-03-14

我不知道控制器< code>map.put("contactList ",contactService.listContact())中我的< code>listContacts出了什么问题;有人能帮我吗?

严重:路径为[/test]的上下文中servlet [dispatcher]的Servlet.service()引发异常[请求处理失败;嵌套异常是Java . lang . nullpointerexception]的根本原因Java . lang . nullpointerexception at pl . ivmx . contact . controller . contact controller . list contacts(contact controller . Java:26)at sun . reflect . nativemethodaccessorimpl . invoke 0(本机方法)at sun . reflect . nativemethodaccessorimpl . invoke(nativemethodaccessorimpl . Java:57)at sun . reflect . delegatingmethodaccessorimpl . Java:43

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>

应用程序上下文.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>

dispatcher-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>

共有3个答案

林博厚
2023-03-14

我没有看到任何明显的。。。sessionFactory是否可以为空?应该很容易找到,tho。单步执行调试器中的代码。

穆宏胜
2023-03-14

您在方法中返回< code>"/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标准目录布局)。

乌璞瑜
2023-03-14

这里的方法中似乎抛出了一个NullPointerException:

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

    return "/contact";
}

我将调试并检查contactService是否已正确自动连接。

空指针是第26行,如果这是包含< code > contactService . list contact()的行,则contact service为空。如果是上面的线,那么< code>map为空。

我不确定,但请尝试添加:

<代码>

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

 类似资料:
  • 问题内容: 我知道这个问题应该在scipy.optimize手册中处理,但是我不太了解。也许你可以帮忙 我有一个函数(这只是一个示例,不是真正的函数,但是我需要在这个级别上理解它): 编辑(更好的示例): 假设我有一个矩阵 具有目标功能 现在,我想假设t [i]是实数,并且类似 问题答案: 这个约束 将是等式()约束,其中您必须创建一个必须等​​于零的函数: 然后,您对约束进行了定义(字典列表(如

  • 除了正常的运行模式,JavaScript 还有第二种运行模式:严格模式(strict mode)。顾名思义,这种模式采用更加严格的 JavaScript 语法。 同样的代码,在正常模式和严格模式中,可能会有不一样的运行结果。一些在正常模式下可以运行的语句,在严格模式下将不能运行。 设计目的 早期的 JavaScript 语言有很多设计不合理的地方,但是为了兼容以前的代码,又不能改变老的语法,只能不

  • 概述 进入标志 如何调用 语法和行为改变 全局变量显式声明 静态绑定 增强的安全措施 禁止删除变量 显式报错 重名错误 禁止八进制表示法 arguments对象的限制 函数必须声明在顶层 保留字 参考链接 概述 除了正常运行模式,ECMAscript 5添加了第二种运行模式:“严格模式”(strict mode)。顾名思义,这种模式使得Javascript在更严格的条件下运行。 设立”严格模式“的

  • ECMAScript 5 最早引入了“严格模式”(strict mode)的概念。通过严格模式,可以在函数内部选择进行较为严格的全局或局部的错误条件检测。使用严格模式的好处是可以提早知道代码中存在的错误,及时捕获一些可能导致编程错误的ECMAScript 行为。 理解严格模式的规则非常重要,ECMAScript 的下一个版本将以严格模式为基础制定。支持严格模式的浏览器包括IE10+、Firefox

  • 我正在阅读关于reinterpret_cast的笔记,它是混淆现象(http://en.cppreference.com/w/cpp/language/reinterpret_cast)。 我写了代码: 我认为这些规则在这里不适用: T2是对象的(可能是cv限定的)动态类型 T2和T1都是指向相同类型T3的指针(可能是多级的,可能在每个级别都是cv限定的)(因为C11) T2是一个聚合类型或并集类

  • 主要内容:什么是严格模式,启用严格模式,严格模式中的变化由于 JavaScript 语法不够严谨,一直被人们所诟病,例如在使用一个变量时,可以不使用 var 关键字来提前声明(例如: ),此时 JavaScript 解释器会自动为您创建这个变量。为了使代码更加严谨,JavaScript 中引入了严格模式,一旦使用了严格模式,将不再允许使用那些不严谨的语法。 什么是严格模式 严格模式是在 ECMAScript5(ES5)中引入的,在严格模式下,JavaS

  • 要启用严格模式,只需在创建 Vuex store 的时候简单地传入 strict: true。 const store = new Vuex.Store({ // ... strict: true }) 在严格模式下,只要 Vuex 状态在 mutation 方法外被修改就会抛出错误。这确保了所有状态修改都会明确的被调试工具跟踪。 开发阶段 vs. 发布阶段 不要在发布阶段开启严格模式! 严格

  • 问题内容: 和之间的真正区别是什么?我可以阅读ehcache和Hibernate文档,但据我所知,他们只说“如果进行更新,读写会更好”。我觉得不满意。 我可能对这样配置的长期缓存集合有问题: 更新集合时,发生更新的节点以及其他节点上究竟发生了什么?和这里和有什么不一样?一个节点是否有可能会使用其缓存中的10分钟版本? 请注意冗长的超时和异步复制。 问题答案: 读写:如果两个事务试图修改数据,则这些