Web应用程序不会让我登录

时间:2017-10-15 18:30:12

标签: java xml spring spring-mvc

我一直在关注如何使用Spring MVC创建一个具有登录和注册功能的简单Web应用程序DZONE example,但是我正在努力解决一个问题。 Tomcat在DAO实现类中抛出异常,说

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

我试过改变弹簧配置,但什么都没发生。也许有人可以帮我解决我的问题。 这是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 Config 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);

    }

}

1 个答案:

答案 0 :(得分: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可访问的地方等。