0

我对 Spring+Hibernate 非常陌生,并且正在开发一个非常基本的 Web 项目。使用简单的形式,有两个字段用户名和密码。它有一个提交按钮。单击提交时,我应该检查数据库中是否存在用户名和密码。这是我的 AuthenticationService 类

package com.service;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

@Service
public class AuthenticateService{

    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.hibernateTemplate=new HibernateTemplate(sessionFactory);
    }

    public boolean verifyUsernameAndPassword(String username,String password)
    {
        System.out.println("Into the Service Class");
        boolean userStatus=false;
        try
        {

            List userObj = hibernateTemplate.find("from user u where u.username=? and u.password=?",username,password);     
            if(userObj.size()!=0)
            {
                userStatus=true;
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return userStatus;
    }

}

这是我的 bean 配置文件:

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

    <bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/springmvc"></property>
        <property name="username" value="root"></property>
        <property name="password" value=""></property>        
    </bean> 
    <!-- org.springframework.orm.hibernate3.LocalSessionFactoryBean -->
     <bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSourceBean"></property>
        <property name="mappingResources">
            <value>com/pojo/user.hbm.xml</value>                    
        </property>
        <property name="hibernateProperties">
              <props>
                  <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                  <prop key="hibernate.show_sql">true</prop>
              </props>
         </property>
    </bean>
    <bean id="hibernateTemplateBean" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactoryBean"></property>
    </bean>
    <bean id="authenticateServiceBean" class="com.service.AuthenticateService">
        <property name="hibernateTemplate" ref="hibernateTemplateBean"></property>
    </bean> 

</beans>

这是我的登录控制器:

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.service.AuthenticateService;

@Controller
@RequestMapping("/login.spring")

public class LoginController {

    @Autowired
    AuthenticateService authenticateService;
    @RequestMapping(method= RequestMethod.POST)
    public ModelAndView processCredentials(@RequestParam("username")String username ,@RequestParam("password")String password)
    {
        System.out.println("Inside username"+username + "  password"+password);

        String message="Invalid Credentials";

        if(authenticateService.verifyUsernameAndPassword(username, password)==true)
        {
            message="Welcome"+username;
        }

return new ModelAndView("results","message",message);
    }
}

当我运行应用程序时,我得到以下堆栈跟踪:

java.lang.NullPointerException
    at com.service.AuthenticateService.verifyUsernameAndPassword(AuthenticateService.java:31)
    at com.controller.LoginController.processCredentials(LoginController.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

我确信应用程序会进入服务类,因为我已经打印了“服务类内部”。我不知道为什么它给了我一个 NullPointer。

4

1 回答 1

0

您有两个“com.service.AuthenticateService”类型的 bean,一个通过在 xml 文件中的注释另一个。

通过注释定义的没有提供休眠模板引用,因此为空。xml 中定义的引用具有适当的引用,但不是被注入的引用。

希望这可以帮助。

于 2013-07-11T14:17:49.290 回答