java.lang.IllegalArgumentException:参数类型不匹配:将表单值存储在数据库中时

问题描述 投票:0回答:3

我想使用struts将表单值存储在数据库中 我的表单豆:

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

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

java jsp struts
3个回答
0
投票

teacherRegisterForward() 需要返回 ActionForward 对象,因此返回 Object

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;

            }

0
投票

您的构造函数没有被表单调用,这意味着您的 Struts 表单和提供的 bean 类传递的参数类型不同。 Bean类有一个FormFile类型的teacherfile的声明,而表单值中的文本字段值被视为字符串。

也许这个教程可以帮助你:-Struts1 中的文件上传示例


0
投票

哪一行抛出异常?

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 类型。如果您仅使用 String formFile。它应该像错误一样抛出。

formFile = tform.getTeacherprofile();

试试这个,

FormFile formFile = tform.getTeacherprofile();

预期签名“FormFile”

收到签名“String”

© www.soinside.com 2019 - 2024. All rights reserved.