kehu23628 2010-04-08 14:42
浏览 189
已采纳

struts1上传问题

最近遇到一个问题
input.jsp

[code="java"]
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="org.apache.struts.util.RequestUtils"%>
<%@page import="com.fulong.form.UserForm"%>



Insert title here div,span{ text-align: center; margin-bottom: 10px; } span { text-align: center; } .btn { margin-left: 15px; margin-right: 15px; }



<%UserForm userForm = new UserForm();
request.setAttribute("userForm",userForm);
%>


  ID  

姓名

年龄


多个附件1/html:file


多个附件2/html:file


多值String/html:text
多值String/html:text




[/code]
TestAction.java
[code="java"]
package com.fulong.action;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.fulong.form.UserForm;

public class TestAction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
throws Exception {
    UserForm userForm = (UserForm) form;
    Map<String,FormFile[]> files = userForm.getFileMap();
    Set set = files.keySet();
    for(Iterator it = set.iterator();it.hasNext();){
        FormFile[] file = files.get(it.next());
    }

    Map<String,String[]> strMap = userForm.getStrsMap();
    set = strMap.keySet();
    for(Iterator it = set.iterator();it.hasNext();){
        String[] strs = strMap.get(it.next());
        System.out.println(strs.length);
    }

    if(isCancelled(request)){
        return mapping.findForward("input");
    }
    System.out.println(request.getQueryString());

    UserForm user = (UserForm) form;
    System.out.println(user);
    ActionForward forward = mapping.findForward("success");
    return forward;
}

}

[/code]

UserForm.java
[code="java"]
package com.fulong.form;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts.upload.FormFile;
import org.apache.struts.validator.ValidatorForm;

public class UserForm extends ValidatorForm{

private String id;
private String name;
private int age;
private Map<String,FormFile[]> files;
private Map<String,String[]> strs;

public Map<String, String[]> getStrsMap() {
    return strs;
}

public String[] getStrs(String property) {
    return strs.get(property);
}

public void setStrs(String property,String[] strs) {
    this.strs.put(property, strs);
}

public FormFile[] getFiles(String property) {
    return files.get(property);
}

public void setFiles(String property,FormFile[] file) {
    files.put(property, file);
}

public Map<String,FormFile[]> getFileMap(){
    return files;
}

public UserForm() {
    super();
    files = new HashMap<String, FormFile[]>();
    strs = new HashMap<String, String[]>();
}



public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

@Override
public String toString() {
    return "id="+id+",name="+name+",age="+age;
}

}
[/code]

老是报错
java.lang.IllegalArgumentException: Cannot invoke com.fulong.form.UserForm.setFiles - argument type mismatch
org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1778)
org.apache.commons.beanutils.PropertyUtilsBean.setMappedProperty(PropertyUtilsBean.java:1547)
org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1019)
org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:805)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.fulong.filter.Encodingfilter.doFilter(Encodingfilter.java:23)

但是多个String值时又正常
知道错误出在哪,但不知道该怎么改?

  • 写回答

1条回答 默认 最新

  • mginobili 2010-04-08 17:59
    关注

    struts使用beanutils来对Form中的字段赋值

    此处setFiles对应field files

    客户端提供的files是list类型
    而lz的setFiles方法中提供的类型是Map类型

    当然会出现argument type mismatch的错误了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题