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

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条回答 默认 最新

相关推荐 更多相似问题