2017-10-15 32 views
0

我一直在關注如何使用Spring MVC創建一個具有登錄和註冊功能的簡單Web應用程序的DZONE example,但是我正在努力解決一個問題。 Tomcat在DAO實現類中拋出一個異常,說Web應用程序不會讓我登錄

SEVERE: Servlet.service() for servlet [spring] in context with path [/PortfolioWebApp] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException 

我試過改變spring的配置,但什麼也沒有發生。也許有人可以幫我解決我的問題。 這是DAO實現類:

package org.madness.java.dao; 

import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.List; 

import org.madness.java.model.Login; 
import org.madness.java.model.User; 
import org.springframework.jdbc.core.RowMapper; 
import org.springframework.jdbc.core.support.JdbcDaoSupport; 

public class JdbcDaoImpl extends JdbcDaoSupport{ 

public User validateAccount(Login login) { 
    String sql = "SELECT * FROM Accounts WHERE USERNAME = '"+login.getUsername()+"' AND PASSWORD = '"+login.getPassword()+"'"; 
    List<User> users = this.getJdbcTemplate().query(sql, new UserMapper()); 
    if (users.size() > 0) { 
     return users.get(0); 
    }else { 
     return null; 
    } 

} 

private static class UserMapper implements RowMapper<User>{ 

    @Override 
    public User mapRow(ResultSet rs, int arg1) throws SQLException { 
     User user = new User(); 

     user.setId(rs.getLong("ID")); 
     user.setUsername(rs.getString("USERNAME")); 
     user.setPassword(rs.getString("PASSWORD")); 
     user.setFirstname(rs.getString("FIRSTNAME")); 
     user.setSurname(rs.getString("SURNAME")); 
     user.setEmail(rs.getString("EMAIL")); 
     user.setAddress(rs.getString("ADDRESS")); 

     return null; 
    } 

} 

}

這是登錄控制器:

package org.madness.java.controller; 

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

import org.madness.java.model.User; 
import org.madness.java.model.Login; 
import org.madness.java.service.UserService; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class LoginController { 
    private UserService userService; 

    @RequestMapping(value="/login", method=RequestMethod.GET) 
    public ModelAndView showLogin(HttpServletRequest req, HttpServletResponse res) { 
     ModelAndView mav = new ModelAndView("login"); 
     mav.addObject("login", new Login()); 

     return mav; 
    } 

    @RequestMapping(value="/loginProcess", method=RequestMethod.POST) 
    public ModelAndView loginProcess(HttpServletRequest req, HttpServletResponse res, @ModelAttribute("login") Login login) { 
     ModelAndView mav = null; 
     if(login != null) { 
      userService = new UserService(); 
      User user = userService.validateUser(login); 
      if(user != null) { 
       mav = new ModelAndView("home"); 
       mav.addObject("firstname", user.getFirstname()); 
      }else { 
       mav = new ModelAndView("login"); 
       mav.addObject("message", "Error: Provided username or password are incorrect. Please, try again..."); 
      } 
     }else { 
      mav = new ModelAndView("login"); 
      mav.addObject("message", "Something happened inside DAO and Service!"); 
     } 
     return mav; 
    } 
} 

這是Spring配置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:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
    <context:component-scan base-package="org.madness.java" /> 
    <context:annotation-config /> 

<bean name="userService" class="org.madness.java.service.UserService" /> 

<bean name="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://192.168.0.27:3306/spring" /> 
    <property name="username" value="student" /> 
    <property name="password" value="password" /> 
</bean> 

<bean name="jdbcDaoImpl" class="org.madness.java.dao.JdbcDaoImpl"> 
    <property name="dataSource" ref="datasource" /> 
</bean> 

這是UserService:

package org.madness.java.service; 

import org.madness.java.dao.JdbcDaoImpl; 
import org.madness.java.dao.UserDaoImpl; 
import org.madness.java.model.Login; 
import org.madness.java.model.User; 
import org.springframework.beans.factory.annotation.Autowired; 

public class UserService { 

    @Autowired 
    private JdbcDaoImpl dao; 

    public User validateUser(Login login) { 
     dao = new JdbcDaoImpl(); 
     return dao.validateAccount(login); 

    } 

} 
+0

可能的複製[什麼是空指針異常,並且我怎麼解決?(https://開頭計算器。 COM /問題/ 218384 /什麼 - 是 - 一 - NullPointerException異常和知識-DO-I-FIX-IT) –

+0

這是關於Spring MVC的問題,而不是NullPointerException異常本身 –

+0

然後編輯你的問題,因爲現在你有錯誤'嵌套異常是java.lang.NullPointerException],其根本原因是java.lang.NullPointerException' –

回答

0

您需要自動裝載UserService,而不是手動實例化它。

public class LoginController { 
    @Autowired 
    private UserService userService; 

    ... 
    @RequestMapping(value="/loginProcess", method=RequestMethod.POST) 
    public ModelAndView loginProcess(HttpServletRequest req, HttpServletResponse res, @ModelAttribute("login") Login login) { 
     ModelAndView mav = null; 
     if(login != null) { 
      userService = new UserService(); //remove this ! 

然後將使用由Spring初始化實例,其中DAO是訪問等

+0

但是在春天的配置文件中,我正在爲userService創建一個bean,是不是一樣? –

+0

您創建了該bean,但不使用Spring創建的bean。相反,您需要重新創建它。 – StanislavL

+0

我已經嘗試過,但仍然是給出相同的例外。 –