Struts ActionForm为空

阿杰心路历程 2010-02-10 01:54:51
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> </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
.....................
}
...全文
439 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿杰心路历程 2012-09-20
  • 打赏
  • 举报
回复
18楼都什么年代了居然还用struts1啊?
cityline 2012-08-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
<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="……
[/Quote]

正解
on_my_way20xx 2010-02-10
  • 打赏
  • 举报
回复
引用 13 楼 mikemoon 的回复:
配置文件不完整
<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>

恩 对的
lp19890601 2010-02-10
  • 打赏
  • 举报
回复
引用 13 楼 mikemoon 的回复:
配置文件不完整
<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>


配置文件的问题
mucrea 2010-02-10
  • 打赏
  • 举报
回复
配置文件中你没给action指定form
yueguangkai001 2010-02-10
  • 打赏
  • 举报
回复
一看就看出来了,Action中没有配置name属性指向Form
中国匠心 2010-02-10
  • 打赏
  • 举报
回复
配置文件不完整
<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>
老张-AI 2010-02-10
  • 打赏
  • 举报
回复
配置文件里面
没有指定
name="PetForm"
BlackGlove 2010-02-10
  • 打赏
  • 举报
回复
引用 4 楼 bearkin 的回复:
引用 3 楼 nihaozhangchao 的回复:<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 的名字.

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


正解!!!!
fancy161 2010-02-10
  • 打赏
  • 举报
回复
回复内容太短了!
一束光丫. 2010-02-10
  • 打赏
  • 举报
回复
你没有用 html:form
<html:form action="..."
luffyke 2010-02-10
  • 打赏
  • 举报
回复
引用 3 楼 nihaozhangchao 的回复:
<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 的名字.

正确答案
guyuxuelang 2010-02-10
  • 打赏
  • 举报
回复
我是路过来接分的,你要在配置文件里把form配给action才行啊。一起学习。
colin_pxx 2010-02-10
  • 打赏
  • 举报
回复
<action path="/DoAdopt" scope="request" name="PetForm" type="com.aptech.jb.epet.web.action.DoAdoptAction" >
antony0203 2010-02-10
  • 打赏
  • 举报
回复
struts-config.xml action配置里没指定form
BearKin 2010-02-10
  • 打赏
  • 举报
回复
引用 3 楼 nihaozhangchao 的回复:
<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 的名字.


页面用html:form标签 如果直接使用form的话 也需要给那个form指定一个名字 名字则是你在struts-config.xml里配置的那个formbean的名字 比如<form action="XXX" name="PetForm"....
SambaGao 2010-02-10
  • 打赏
  • 举报
回复
<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 的名字.

myj870517 2010-02-10
  • 打赏
  • 举报
回复
sturts1到时记不大清了,html里form不要写name的吗?
阿杰心路历程 2010-02-10
  • 打赏
  • 举报
回复
找出问题,马上结贴。

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧