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
.....................
}
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
.....................
}
<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"....
<html:form action="..."
没有指定
name="PetForm"
<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>