当前位置: 代码迷 >> J2EE >> Struts ActionForm替空
  详细解决方案

Struts ActionForm替空

热度:25   发布时间:2016-04-22 00:57:28.0
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;            oPic.value += "unkown.jpg";        }        oImg.src = oPic.value;    }    </script>  </head>        <body>    <form action="DoAdopt.do">        <input type="hidden" name="petPic" />        <div class="main_div">        <table>            <tr>                <td width="320px" valign="top">         <table class="input_table" width="100%">                          <tr><th>宠物名:</th><td><input type="text" name="petName" /></td></tr>             <tr><th>类:</th><td>                <select name="petType" onchange="javascript:setPetType(this);">                    <option value="">请选择...</option>                    <option value="1">千禧猪</option>                    <option value="2">喵咪</option>                    <option value="3">哥斯拉</option>                </select>            </td></tr>             <tr><th>性别:</th><td>                 <input type="radio" name="petSex" value="男" />男                 <input type="radio" name="petSex" value="女" />女                 <input type="radio" name="petSex" value="小" />小             </td></tr>             <tr><th>力量:</th><td><input type="text" name="petStrength" readonly="true" /></td></tr>             <tr><th>能力:</th><td><input type="text" name="petCute" readonly="true" /></td></tr>             <tr><th>爱心:</th><td><input type="text" name="petLove" readonly="true" /></td></tr>                          <tr><th>介绍:</th><td><textarea name="petIntro"></textarea></td></tr>             <tr><th>主人名:</th><td><input type="text" name="petOwnerName" /></td></tr>             <tr><th>宠物EMail:</th><td><input type="text" name="petOwnerEmail" /></td></tr>             <tr><th>密码:</th><td><input type="password" name="petPassword" /></td></tr>             <tr><th>&nbsp;</th><td><button onclick="javascript:doAdopt();">领养</button></td></tr>                      </table>             </td>        <td valign="top">            <img id="petImg" src="images/pet/unkown.jpg" />                         </td>        </tr>    </table>    </div>    </html:form>  </body></html>
  相关解决方案