java.lang.IllegalArgumentException:参数类型不匹配:在数据库中存储表单值时
我想使用struts在数据库中存储表单值 我的表单Bean:java.lang.IllegalArgumentException:参数类型不匹配:在数据库中存储表单值时,java,jsp,struts,Java,Jsp,Struts,我想使用struts在数据库中存储表单值 我的表单Bean: import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; public class TeacherForm extends ActionForm { private String tfastname; private FormFile teacherImage; private For
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class TeacherForm extends ActionForm {
private String tfastname;
private FormFile teacherImage;
private FormFile teacherprofile;
public String getTfastname() {
return tfastname;
}
public void setTfastname(String tfastname) {
this.tfastname = tfastname;
}
public FormFile getTeacherImage() {
return teacherImage;
}
public void setTeacherImage(FormFile teacherImage) {
this.teacherImage = teacherImage;
}
public FormFile getTeacherprofile() {
return teacherprofile;
}
public void setTeacherprofile(FormFile teacherprofile) {
this.teacherprofile = teacherprofile;
}
以下是jsp:
<form action="teacherregistration.html" enctype="multipart/form-data">
<input type="hidden" name="method" value="teacherregister"></input>
<table class="tablecss" cellspacing="3" cellpadding="3">
<tr>
<td align="right" valign="middle" width="10%"><label
class="LblDialog">First Name</label></td>
<td><input type="text" name="tfastname" id="fname"
class="textBoxInDialog"/></td>
<tr>
<td align="center" valign="middle" width="10%"><label
class="textBoxInDialog">Upload Profile</label></td>
<td align="center" valign="middle" width="10%"><input
type="file" id="teaprofile" name="teacherprofile"/></td>
<td align="center" valign="middle" width="10%"><label
class="textBoxInDialog">Image</label></td>
<td align="center" valign="middle" width="10%"><input
type="file" id="teaImageId" name="teacherImage" /><img
id="imagePreview" src="#" alt="your image" width="70px"
height="70px"/></td>
</tr>
</table>
<br />
<table id="parentReg" cellspacing="3" cellpadding="3" width="100%">
<tr class="odd">
<td colspan="3"><center>
<input type="submit" class="submitButton" id="buttonID"></input></td>
</tr>
我在这里使用log4j,当我点击submit按钮时,我会得到以下错误:
Caused by: java.lang.IllegalArgumentException: Cannot invoke com.centris.campus.forms.TeacherForm.setTeacherprofile on bean class 'class com.centris.campus.forms.TeacherForm' - argument type mismatch - had objects of type "java.lang.String" but expected signature "org.apache.struts.upload.FormFile"
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2181)
at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2141)
at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1948)
at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2054)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1015)
at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:830)
at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:433)
at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:473)
... 25 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2155)
... 32 more
请给我任何解决这个问题的办法。
这对我很有帮助。teacherRegisterForward()需要返回ActionForward对象,因此返回该对象
public ActionForward teacherregister(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception
{
TeacherForm tform=(TeacherForm)form;
formFile = tform.getTeacherprofile();
System.out.println("tfastname-----"+tform.getTfastname());
return null;
}
表单未调用构造函数意味着Struts表单传递的参数类型和提供的bean类不同。Bean类具有FormFile类型teacherfile的贴花,而来自form value的by textfields值作为字符串
可能本教程可以帮助您:-哪一行抛出异常
Caused by: java.lang.IllegalArgumentException: Cannot invoke com.centris.campus.forms.TeacherForm.setTeacherprofile on bean class 'class com.centris.campus.forms.TeacherForm' - argument type mismatch - had objects of type "java.lang.String" but expected signature "org.apache.struts.upload.FormFile"
这一行清楚地说明了这一点,tform.getTeacherprofile();返回FormFile类型(这意味着您可以保存在FormFile数据类型中)。请检查FormFile类型。如果您仅使用字符串FormFile,则它应该像错误一样抛出
formFile = tform.getTeacherprofile();
试试这个
FormFile formFile = tform.getTeacherprofile();
预期签名“FormFile”
收到签名“String”试图诊断问题,首先放入一个creator构造函数,然后放入一个System.out.println(“调用的构造函数”);然后将System.out.println(“setter called”);在你的二传中。检查是否调用了构造函数和setters?调用了构造函数但未调用setters。您如何在该文本框中提供文件?您使用的是哪个struts版本。仅提供FormFile。错误不在此行
formFile = tform.getTeacherprofile();
FormFile formFile = tform.getTeacherprofile();