java.lang.IllegalArgumentException : argument type mismatch:when store the form values in Database 异常

标签 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 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>

在 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

请给我任何解决方案如何解决这个问题。 这对我很有帮助。

最佳答案

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;

            }

关于java.lang.IllegalArgumentException : argument type mismatch:when store the form values in Database 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24262370/

相关文章:

javascript - 如何在表单提交时将表中选定的行作为请求参数传递?

jsp - struts,包含jsp页面

java - filter和findFirst后map抛出异常需要继续过滤Java流

java - 差异。 b/w 2 种获取连接管理器实例的方法

java - 依赖注入(inject)不起作用

java - 如何登录 Undertow 嵌入式服务器中的文件?

javascript - 如何删除表单中的按钮格式以便将按钮用作切换按钮?

javascript - 我的谷歌图表 x 轴和 y 轴未正确显示

java - 防止用户打开同一个 Web 应用程序的多个窗口

java - 更改下拉选项