1.2版本的struts 
 execute里的参数form初始化就是空的。
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>
  <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>
struts-config.xml
   <struts-config>
         <form-beans>
                <form-bean name="PetForm" type="com.aptech.jb.epet.web.form.PetForm"/>
          </form-beans>         <action-mappings>
              <action path="/DoAdopt" scope="request" type="com.aptech.jb.epet.web.action.DoAdoptAction">
<forward name="list" path="/WEB-INF/jsp/pet/list.jsp"/>
<forward name="error" path="/WEB-INF/jsp/error.jsp" />
</action>
                 ..................
</action-mappings>   
   <struts-config>
PetForm.java
public class PetForm extends ActionForm{
private Integer petId; private String petName; private String petSex = "С"; private Integer petStrength; private Integer petCute; private Integer petLove; private String petIntro; private String petOwnerName; private String petOwnerEmail; private String petPassword; private String petPic; private Integer petType; //getter&setter
         .....................
}

解决方案 »

  1.   

    sturts1到时记不大清了,html里form不要写name的吗?
      

  2.   

    <action path="/DoAdopt" scope="request" type="com.aptech.jb.epet.web.action.DoAdoptAction"name="PetForm">
                <forward name="list" path="/WEB-INF/jsp/pet/list.jsp"/>
                <forward name="error" path="/WEB-INF/jsp/error.jsp" />
            </action>
    需要指定ActionForm 的名字.
      

  3.   


    页面用html:form标签 如果直接使用form的话 也需要给那个form指定一个名字 名字则是你在struts-config.xml里配置的那个formbean的名字 比如<form action="XXX" name="PetForm"....
      

  4.   

    struts-config.xml action配置里没指定form
      

  5.   

    <action path="/DoAdopt" scope="request" name="PetForm" type="com.aptech.jb.epet.web.action.DoAdoptAction" >
      

  6.   

    我是路过来接分的,你要在配置文件里把form配给action才行啊。一起学习。
      

  7.   

    你没有用  html:form 
    <html:form action="..."
      

  8.   

    配置文件里面
    没有指定
    name="PetForm" 
      

  9.   

    配置文件不完整
    <struts-config>
             <form-beans>
                    <form-bean name="PetForm" type="com.aptech.jb.epet.web.form.PetForm"/>
              </form-beans>         <action-mappings>
                     <action path="/DoAdopt" scope="request" type="com.aptech.jb.epet.web.action.DoAdoptAction" name="PetForm">
                <forward name="list" path="/WEB-INF/jsp/pet/list.jsp"/>
                <forward name="error" path="/WEB-INF/jsp/error.jsp" />
            </action>
                     ..................
        </action-mappings>      
       <struts-config>
      

  10.   

    一看就看出来了,Action中没有配置name属性指向Form
      

  11.   

    配置文件中你没给action指定form
      

  12.   

    18楼都什么年代了居然还用struts1啊?