在浏览器中输入http://localhost:8080/ch03_1/login.jsp能进入登录页面,但是在表单中输入用户名和密码后,就出现http500错误:
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: java.lang.NullPointerException
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
root cause java.lang.NullPointerException
org.sunxin.struts2.ch03.action.LoginAction.execute(LoginAction.java:35)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)................
LoginAction.java文件内容如下:package org.sunxin.struts2.ch03.action;import com.opensymphony.xwork2.Action;
public class LoginAction implements Action { private String username;
private String password;

public String getUser()
{
return username;
}
public void setUser(String username)
{
this.username=username;
} public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}



public String execute() throws Exception {

if(username.equals("zhangsan")&&password.equals("123"))
return SUCCESS;
else
return ERROR;
}
}
struts.xml内容:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
  
  <struts>
    <package name="default" extends="struts-default">
      <action name="login" class="org.sunxin.struts2.ch03.action.LoginAction">
         <result>/success.jsp</result>
         <result name="error">/error.jsp</result>
      </action>
    </package>
  </struts>web.xml内容:<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
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/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list> <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter> <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>login.jsp内容:<%@ page language="java" contentType="text/html; charset=gbk" %>
<html>
  <head>
    <title>登录页面</title>
  
  
  </head>
  
  <body>
  <form action="login.action"  method="post">
    <table align="center">
      <caption><h3> 用户登录</h3></caption>
<tr> 
  <td>用户名:<input type="text" name="username" /> </td>
</tr>  
  <tr>
   <td> 密码:<input type="text" name="password" /> </td>
  </tr><tr align="center">
  <td clospan="2"><input type="submit" value="登录" />
                  <input type="reset" value="重填" />     
  </td>
</tr></table>
</form>
  
  </body></html>error.jsp内容:<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>登录失败</title>
</head>
<body>
  用户名或密码错误,请重新<a href="login.jsp">登录</a></body>
</html>success.jsp内容:<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
  <h3><s:property value="username" />,欢迎您登录程序员之家网站。
  </h3></body>
</html>

解决方案 »

  1.   

    <input type="text" name="username" /> </td>
    控件的名字要是javaBean的属性, LZ去巩固下自己的基础知识吧.!
    所谓的javaBean的属性不是指你的成员变量字段, 而是get/set后面跟的那个!
    所以按照你类里面的写法, 你这里应该写
    <input type="text" name="user" /> </td>或者把你的Action中改成getUsername, setUsername
      

  2.   


    public String getUsername()
    {
    return username;
    }
    public void setUsername(String username)
    {
    this.username=username;
    }
    你的username的get,set方法写错了。
    后台取值取不到,所以空指针了。
      

  3.   

    页面的name名字要和action中一样,并且action中的属性要有对应的get/set方法
    楼上正解
      

  4.   

    楼主为什么要去修改get和set方法的名字呢?
      

  5.   

    空指针。
    get,set方法最好让编译器自动生成,这样比较不容易出错。