Thymeleaf当我使用enctype =" multipart / form-data"时,在表单中映射对象不起作用

时间:2014-07-12 13:44:28

标签: spring-mvc thymeleaf

我正在尝试将文件和数据上传到jetty服务器。我正在使用gretty插件来调试我的应用程序

在表单中,如果我省略enctype =“multipart / form-data”,则对象会正确映射到字段。例如,当我提交表单时,我可以看到映射对象中的正确文本。 (当我这样做时,我还需要更改文件以在类中键入File.io,否则我会收到错误映射)

当我将enctype =“multipart / form-data”添加到我的表单时,突然映射停止工作并且所有映射的文本框都传递为null

有人可以看到我的申请有问题吗?

在我的应用程序主类中,我创建了一个multipartResolver bean和一个MultipartConfigElement bean

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(500000000);
    return multipartResolver;
}

@Bean
public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    factory.setMaxFileSize("9999KB");
    factory.setMaxRequestSize("9999KB");
    return factory.createMultipartConfig();
}

的build.gradle

buildscript {
repositories {
    maven { url "http://repo.spring.io/libs-release" }
    mavenLocal()
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.4.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'maven'

apply plugin: 'war'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'


jar {
baseName = 'killesk-language'
version =  '0.1.0'
}

repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-snapshot" }
}

dependencies {
compile("org.springframework.boot:spring-boot-starter-web:1.1.4.RELEASE")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.thymeleaf:thymeleaf-spring4:2.1.2.RELEASE")
compile("org.hibernate:hibernate-validator")
compile("org.apache.tomcat.embed:tomcat-embed-el")
runtime('mysql:mysql-connector-java:5.1.31')

testCompile("junit:junit")
}


task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

这是我的两个班级:

public class Vocabulary {
private int vocabularyID;
private MultipartFile fileImage;
private List<VocabularyTextAndAudio> listVocabularyTextAndAudio= new ArrayList<VocabularyTextAndAudio>();

public int getVocabularyID() {
    return vocabularyID;
}

public void setVocabularyID(int vocabularyID) {
    this.vocabularyID = vocabularyID;
}

public MultipartFile getFileImage() {
    return fileImage;
}

public void setFileImage(MultipartFile fileImage) {
    this.fileImage = fileImage;
}

public List<VocabularyTextAndAudio> getListVocabularyTextAndAudio() {
    return listVocabularyTextAndAudio;
}

public void setListVocabularyTextAndAudio(List<VocabularyTextAndAudio> listVocabularyTextAndAudio) {
    this.listVocabularyTextAndAudio = listVocabularyTextAndAudio;
}
}


public class VocabularyTextAndAudio {

private int vocabularyID;
private int languageID;
private String vocabularyText;
private MultipartFile fileAudio;

public int getVocabularyID() {
    return vocabularyID;
}

public void setVocabularyID(int vocabularyID) {
    this.vocabularyID = vocabularyID;
}

public int getLanguageID() {
    return languageID;
}

public void setLanguageID(int languageID) {
    this.languageID = languageID;
}

public String getVocabularyText() {
    return vocabularyText;
}

public void setVocabularyText(String vocabularyText) {
    this.vocabularyText = vocabularyText;
}

public MultipartFile getFileAudio() {
    return fileAudio;
}

public void setFileAudio(MultipartFile fileAudio) {
    this.fileAudio = fileAudio;
}
}

这是我的HTML表单:

<form id="myform"
  action="#"
  th:action="@{/admin/addvocabularydata.do}"
  th:object="${vocabulary}"
  method="POST"
  enctype="multipart/form-data">
<tr>
    <td>File to upload:</td>
    <input type="file"
           th:field="${vocabulary.fileImage}"
           name="file"/>
</tr>
<tr th:each="language, iterStat :  ${vocabulary.getListVocabularyTextAndAudio()}">
    <td><input type="hidden"
               th:id="test"
               th:name="test"
               th:field="${vocabulary.listVocabularyTextAndAudio[__${iterStat.index}__].languageID}"/></td>

    <td th:text="${T(com.killesk.language.enums.ENUM_LANGUAGES).getLocalTextPropertyStringFromInt({language.getLanguageID()})}"></td>

    <td><input type="text"
               th:id="test"
               th:name="test"
               th:field="${vocabulary.listVocabularyTextAndAudio[__${iterStat.index}__].vocabularyText}"/></td>


    <td><input type="file"
               th:id="${'languageAudioFile_id_' + language.getLanguageID()}"
               th:name="${'languageAudioFile_name_' + language.getLanguageID()}"
               th:field="${vocabulary.listVocabularyTextAndAudio[__${iterStat.index}__].fileAudio}"
               accept=".wma"/></td>
</tr>
<input type="submit" value="Upload"/>

这是捕获请求的控制器:

 @RequestMapping(value = "/admin/addvocabularydata.do", method= RequestMethod.POST )
public String addVocabularyValadate( @Valid @ModelAttribute("vocabulary") Vocabulary vocabulary,
                                     BindingResult bindingResult,
                                     Model model) {


    if(vocabulary.getFileImage().getName() != null)
    System.out.println("vocab file name " + vocabulary.getFileImage().getOriginalFilename());

    if(vocabulary.getListVocabularyTextAndAudio() != null){
        for (VocabularyTextAndAudio element : vocabulary.getListVocabularyTextAndAudio()) {
            if(element.getFileAudio() != null)
                System.out.println("VocabularyTextAndAudio file name " + element.getFileAudio().getOriginalFilename());

            if(element.getVocabularyText() == null) {
                System.out.println("VocabularyTextAndAudio text is NULL!!");
            }
        }
    }

    if(bindingResult.hasFieldErrors() == true)
        return "addvocabularydata";
    else
        return "addvocabularyfile";

}

当我运行我的应用程序并提交the following values

我得到了输出:

vocab file name apple.png
VocabularyTextAndAudio file name apple.wma
VocabularyTextAndAudio text is NULL!!
VocabularyTextAndAudio file name manzana.wma
VocabularyTextAndAudio text is NULL!!

如果我将MultipartHttpServletRequest mrequest添加到控制器方法:

@RequestMapping(value = "/admin/addvocabularydata.do", method= RequestMethod.POST )
public String addVocabularyValadate( @Valid @ModelAttribute("vocabulary") Vocabulary vocabulary,
                                     BindingResult bindingResult,
                                     Model model,
                                     MultipartHttpServletRequest mrequest

我可以看到传递的参数名称 enter image description here

但是当我尝试获取值时,它们是NULL。

我发现它们作为内容参数被埋没在请求中。看到这张照片 enter image description here

知道如何从那里获取参数值吗?

1 个答案:

答案 0 :(得分:1)

我从Gretty 9切换到Tomcat 8,我现在可以看到传递的值

的build.gradle

gretty {
    servletContainer = 'tomcat8'
}