简体   繁体   English

Web应用程序不会让我登录

[英]Web Application wont let me login

I have been following DZONE example of how to create a simple web application with login and registration features using Spring MVC, but however I am struggling with one issue. 我一直在遵循DZONE示例 ,该示例演示如何使用Spring MVC创建具有登录和注册功能的简单Web应用程序,但是我一直在努力解决一个问题。 Tomcat throws an exception in DAO Implementation class, saying Tomcat在DAO Implementation类中引发异常,说

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

I`ve tried changing spring config but nothing happened. 我尝试过更改spring config,但是什么也没发生。 Maybe someone could help me out with my problem. 也许有人可以帮助我解决我的问题。 This is DAO Implementation class: 这是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;
    }

}

} }

This is Login Controller: 这是登录控制器:

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;
    }
}

This is Spring Config XML file: 这是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>

And this is UserService: 这是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);

    }

}

You need to autowire UserService rather than instantiating it manually. 您需要自动装配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 !

Then the instance initialized by Spring will be used where the DAO is accessible etc. 然后在可访问DAO的地方使用Spring初始化的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM