0

我是春天的新手。现在尝试做一个登录页面。我收到空指针异常。我不知道如何解决它。请帮助我。这是我得到的错误..

HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:681)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause 

java.lang.NullPointerException
    com.sce.controller.LoginController.processForm(LoginController.java:29)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

登录控制器.java:

package com.sce.controller;
import com.sce.model.LoginData;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.validation.BindingResult;
import java.util.Map;
import javax.validation.Valid;

@Controller
@RequestMapping("login.html")
public class LoginController {
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(Map model) {
        LoginData loginobj = new LoginData();
        model.put("loginobj", loginobj);
        return "login";
        }

    @RequestMapping(method = RequestMethod.POST)
    public String processForm(@Valid LoginData loginobj, BindingResult result,Map model) {
        String userName = "user";
        String password = "pass";
        if (result.hasErrors()) {
            return "login";
            }
        loginobj = (LoginData)model.get("loginobj");
        System.out.println("username:" +loginobj.getUserName());
        System.out.println("password:" +loginobj.getPassword());
        if (!loginobj.getUserName().equals(userName)||!loginobj.getPassword().equals(password)) {
            return "loginerror";
            }
        model.put("loginobj", loginobj);
        return "loginsucces";
        }
    }

登录数据.java:

package com.sce.model; 

import javax.validation.constraints.Size; 
import org.hibernate.validator.constraints.NotEmpty; 

public class LoginData {
    @NotEmpty@Size(min = 1, max = 50)
    private String userName;
    @NotEmpty@Size(min = 1, max = 20)
    private String password;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Online Book Shopping</title>
</head>
<body>
<h1>Welcome to Online Book Shopping</h1>
<ul>
<li><a href="forms/login.html">Click here to Login</a></li>
</ul>
</body>
</html>

登录.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Online Shopping</title>
</head>
<body>
<h3>Login Form</h3>
<form:form action="login.html" commandName="loginobj">
<table>
<tr><td>User Name:<FONT color="red">
<form:errors path="userName" /></FONT></td></tr>
<tr><td><form:input path="userName" /></td></tr>
<tr><td>Password:<FONT color="red">
<form:errors path="password" /></FONT></td></tr>
<tr><td><form:password path="password" /></td></tr>
<tr><td><input type="submit" value="Submit" /></td></tr>
</table>
</form:form>
</body>
</html>

登录成功.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Online Shopping</title>
</head>
<body>
<h3>Login Form</h3>
<form:form action="login.html" commandName="loginobj">
<table>
<tr><td>User Name:<FONT color="red">
<form:errors path="userName" /></FONT></td></tr>
<tr><td><form:input path="userName" /></td></tr>
<tr><td>Password:<FONT color="red">
<form:errors path="password" /></FONT></td></tr>
<tr><td><form:password path="password" /></td></tr>
<tr><td><input type="submit" value="Submit" /></td></tr>
</table>
</form:form>
</body>
</html>

登录错误.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Username or password is wrong..
<table>
<tr><td><a href="login.html">Try again..</a></td></tr>
</table>
</body>
</html>

调度程序-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd">
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<context:component-scan base-package="com.sce.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
</bean>
</beans>

消息属性:

NotEmpty.loginobj.userName=must not be blank.
Size.loginobj.userName=size must between 1 to 50 characters.
NotEmpty.loginobj.Password=must not be blank.Size.loginobj.
Password=size must between 1 to 20 characters.

网页.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
<display-name>OnlineShopping</display-name>
<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>/forms/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4

2 回答 2

0

从您提供的代码来看,这似乎loginobj是导致NullPointerException.

于 2013-01-18T12:59:07.013 回答
0

根据堆栈跟踪并查看您的processForm方法,唯一可能导致 a 的对象NullPointerExceptionloginobj.

这一行应该实际返回一个null对象,当您打印用户的用户名时会在下一行引发异常。

loginobj = (LoginData)model.get("loginobj");

检查您是否在模型上正确设置了此对象,或者您是否使用不同的键访问它。

于 2013-01-18T13:00:54.727 回答