日期:2014-05-20  浏览次数:20841 次

Form 为空的问题
各位,本人是菜鸟,建了个工程测试struts登录,当点击subset时报空指针,哪位仁兄指点一下。
Java code
package com.isoftstone.Test.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
public class LoginForm extends ValidatorForm {
    private String userName;
    private String password;
    @Override  
    public void reset(ActionMapping mapping, HttpServletRequest request) {   
        System.out.println("--------LoginAcionForm.reset()--------");   
    }   
    /* (non-Javadoc)  
     * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)  
     */  
    @Override  
    public ActionErrors validate(ActionMapping mapping,   
            HttpServletRequest request) {   
        System.out.println("-----LoginAcitonForm.validate()");   
//      UserManager.getInstance().login(username, password);   
        return null;   
    }  
    
    
    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;
    }
}



下面是action
Java code
package com.isoftstone.Test.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.isoftstone.Test.dao.UserDAO;
import com.isoftstone.Test.form.LoginForm;

public class LoginAction extends Action{
    
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response){
        LoginForm loginForm = (LoginForm)form;
        System.out.println(loginForm.getUserName());
        UserDAO dao = new UserDAO();
        if(dao.check(loginForm.getUserName())){
            return mapping.findForward("success");
        }else{
            return mapping.findForward("error");
        }    
    }
}



这是jsp页面
HTML code
<%@ page language="java" pageEncoding="gb2312"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  <head>
    <html:base />
    <title>login.jsp</title>
  </head>
  <body>
   <form action="/Test/login.do" method="post"> 
    username:<input type=text name="userName" size="15"/><br>
    password:<input type=password name="password" size="15"/><br>
    <input type="submit" value="submit">
    <input type="reset"  value="reset">
   </form> 
  </body>
</html:html>




------解决方案--------------------
伙计,写代码的时候小心点,
你看看