日期:2014-05-19  浏览次数:20732 次

Struts ActionForm为空
1.2版本的struts 
 execute里的参数form初始化就是空的。

Java code

public class DoAdoptAction extends Action {
    public ActionForward execute(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response){
        ActionForward af=null;
        int num=0;
        if(form==null){
            System.out.println("error!");
            System.out.println(request.getAttribute("PetForm"));//输出null
            return null;
        } 
                  //form不知道什么原因为空了,下面都不执行了。
        PetForm myForm=(PetForm)form;
        PetInfo info=new PetInfo(myForm);
        try{
            PetInfoBiz petBiz=new PetInfoBizImpl();
            num=petBiz.add(info);
            
        }catch(Exception e){
            request.setAttribute("error", "修改失败");
            return mapping.findForward("error");
        }
        if(num>0){
            try {
                response.sendRedirect("/epet/ListPet.do");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        request.setAttribute("error", "修改失败");
        return mapping.findForward("error");
    }
}



adopt.jsp
HTML code

<html>
  <head>
    <title>领养宝贝</title>
    <script language="javascript">
    var validateMsg = "";
    function checkNotEmpty(ctlName,label)
    {
        var oCtl = document.forms[0].elements[ctlName];
        if (oCtl.value=="")
        {
            if (label)
            {
                validateMsg += label;
            }
            validateMsg += "不能为空!\n";
            oCtl.focus();
        }
    }
    function checkValidateMsg()
    {
        if (validateMsg!="")
        {
            alert(validateMsg);
            return false;
        }
        return true;
    }
    function doAdopt()
    {
        validateMsg = "";
        checkNotEmpty("petPassword","密码");
        checkNotEmpty("petOwnerName","宠物主人");
        checkNotEmpty("petType","类");
        checkNotEmpty("petName","宠物名");
        if ( !checkValidateMsg() )
        {
            return;
        }
        document.forms[0].submit();
    }
    function setPetType(oSelect)
    {
        var typeValue = oSelect.options[oSelect.selectedIndex].value;
        var oStrength = document.forms[0].elements["petStrength"];
        var oCute = document.forms[0].elements["petCute"];
        var oLove = document.forms[0].elements["petLove"];
        var oPic = document.forms[0].elements["petPic"];
        var oImg = document.getElementById("petImg");
        oPic.value = "images/pet/";
        if (typeValue==1)//Pit
        {
            oStrength.value = 60;
            oCute.value = 60;
            oLove.value = 60;
            oPic.value += "pig.jpg";            
        }
        else if (typeValue==2)//Cat
        {
            oStrength.value = 40;
            oCute.value = 60;
            oLove.value = 80;
            oPic.value += "cat.jpg";
        }
        else if (typeValue==3)//Dragon
        {
            oStrength.value = 80;
            oCute.value = 60;
            oLove.value = 40;
            oPic.value += "dragon.jpg";
        }
        else{
            oStrength.value = 0;
            oCute.value = 0;
            oLove.value = 0;