我想使用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 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>
在配置文件中:
<form-bean name="teacherregistorform" type="com.centris.campus.forms.TeacherForm" />
<action path="/teacherregistration"
type="com.centris.campus.actions.TeacherRegistrationAction" input="/JSP/Teacher/TeacherRegistration.jsp" parameter="method"
name="teacherregistorform" scope="request">
<forward name="teacherLogin" path="inAdminTeacherRegistration"/>
</action>
行动中类:
import org.apache.struts.upload.FormFile;
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());
}
这里我使用log4j也当我点击提交按钮,我会得到下面的错误:
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
请给予我解决这个问题的方法,这对我很有帮助。
3条答案
按热度按时间mm5n2pyu1#
teacherRegisterForward()需要返回ActionForward对象,因此返回对象
sauutmhj2#
构造函数未被表单调用意味着Struts表单传递的参数类型与提供的Bean类不同。Bean类具有FormFile类型teacherfile的decalation,而表单值中的textfields值被视为String。
也许这个教程可以帮助你:-File Uplaod Example in Struts1
7fhtutme3#
哪一行抛出异常?
这行代码清楚地告诉我们,tform.getTeacherprofile();返回表格文件类型(这意味着你可以保存在表格文件数据类型)。检查表格文件类型。如果你只使用字符串表格文件。它应该抛出类似的错误。
试试这个,
需要签名“FormFile”
接收签名“字符串”