POST方法中的Spring MVC模型为空

时间:2017-10-20 09:38:29

标签: java forms spring-mvc modelattribute

我正在使用Spring MVC开发WebApp。问题是我在我的控制器的POST方法中使用@ModelAttribute传递的模型似乎是空的,尽管我已在网页上填写了表单。

这是我的控制器的GET方法:

@RequestMapping(value = "/CircleUp", method = RequestMethod.GET)
public ModelAndView circleUpGet(Model _model) {
    ModelAndView modelAndView = new ModelAndView("CircleUp");

    CircleUpModel circleUpModel = new CircleUpModel();
    _model.addAttribute("circleUpModel", circleUpModel);

    return modelAndView;
}

这是我的控制器的POST方法:

@RequestMapping(value = "/CircleUp", method = RequestMethod.POST)
public ModelAndView circleUpPost(HttpServletRequest _request, Model _model, @ModelAttribute("circleUpModel") CircleUpModel _circleUpModel) {

    return this.doCircleUp(_request, _model, _circleUpModel);
}

这里“_circleUpModel”没有任何值。

这是我在相关的.jsp文件中的表单,其中包含所需的脚本:

<form:form method="post" modelAttribute="circleUpModel" id="circle_form" action="" enctype="multipart/form-data" >


    <form:input path="file" id="upload" type="file" />

    <form:checkbox path="mergeSameTickmarks" checked="true" />

    <form:checkbox path="deleteHighlights" checked="true" />

    <form:checkbox path="generateFigureLog" checked="true" />

    ...

    <input type="button" onclick="javascript:submitForm()"
        value="Start Circle Up">

function submitForm() {
    if (checkIfPdf() && checkSettings()) {
        $("#circle_form").submit()
        setTimeout(progress, 100);
    }
}

当然会显示CircleUpModel的默认值,但是如果我以网页的形式输入其他一些值,那么在POST方法中不会考虑这个值,尽管我在表单中添加了modelAttribute="circleUpForm"

CircleUpModel:

import org.pdfclown.files.File;


public class CircleUpModel {

    // File
    private File    file;

    // FileName
    private String  fileName;

    private boolean mergeSameTickmarks;
    private boolean deleteHighlights;
    private boolean generateFigureLog;

    // Settings
    private double  circleBorderWidth;

    private double  customizeCircleUp;
    private double  customizeCircleDown;

    private double  gapLeft;
    private double  gapRight;
    private double  maxGap;

    private boolean tickmarkAlwaysUpperCase;

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public boolean isMergeSameTickmarks() {
        return mergeSameTickmarks;
    }

    public void setMergeSameTickmarks(boolean mergeSameTickmarks) {
        this.mergeSameTickmarks = mergeSameTickmarks;
    }

    public boolean isDeleteHighlights() {
        return deleteHighlights;
    }

    public void setDeleteHighlights(boolean deleteHighlights) {
        this.deleteHighlights = deleteHighlights;
    }

    public boolean isGenerateFigureLog() {
        return generateFigureLog;
    }

    public void setGenerateFigureLog(boolean generateFigureLog) {
        this.generateFigureLog = generateFigureLog;
    }

    public double getCircleBorderWidth() {
        return circleBorderWidth;
    }

    public void setCircleBorderWidth(double circleBorderWidth) {
        this.circleBorderWidth = circleBorderWidth;
    }

    public double getCustomizeCircleUp() {
        return customizeCircleUp;
    }

    public void setCustomizeCircleUp(double customizeCircleUp) {
        this.customizeCircleUp = customizeCircleUp;
    }

    public double getCustomizeCircleDown() {
        return customizeCircleDown;
    }

    public void setCustomizeCircleDown(double customizeCircleDown) {
        this.customizeCircleDown = customizeCircleDown;
    }

    public boolean isTickmarkAlwaysUpperCase() {
        return tickmarkAlwaysUpperCase;
    }

    public void setTickmarkAlwaysUpperCase(boolean tickmarkAlwaysUpperCase) {
        this.tickmarkAlwaysUpperCase = tickmarkAlwaysUpperCase;
    }

    public double getGapLeft() {
        return gapLeft;
    }

    public void setGapLeft(double gapLeft) {
        this.gapLeft = gapLeft;
    }

    public double getGapRight() {
        return gapRight;
    }

    public void setGapRight(double gapRight) {
        this.gapRight = gapRight;
    }

    public double getMaxGap() {
        return maxGap;
    }

    public void setMaxGap(double maxGap) {
        this.maxGap = maxGap;
    }

}

2 个答案:

答案 0 :(得分:0)

如果删除文件上传内容,您将能够看到模型中的数据。但是如果你想进行文件上传,你必须按如下方式调整你的代码。

  1. 首先,删除会员文件&#39;以及来自模型的setter和getter方法。
  2. 其次,替换JSP中的以下标记

    <form:input path="file" id="upload" type="file" />
    
  3. 带有简单的HTML标记。

        <input name="file" type="file"/>
    
    1. 在控制器中修改POST方法的签名,如下所示。

      public ModelAndView circleUpPost(HttpServletRequest _request, @ModelAttribute("circleUpModel") CircleUpModel _circleUpModel, @RequestParam("file") MultipartFile _file) {
          //do your processing
      }
      
    2. 这里是最后一个参数&#39; _file&#39;是 MultipartFile 类型,包含您从JSP上传的文件。

      1. 最后也是最重要的一步是在Spring配置文件中注册一个多部分解析器。

        <beans:bean id="multipartResolver"    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
             <beans:property name="maxUploadSize" value="100000" />
        </beans:bean>
        
      2. 希望这有帮助。

答案 1 :(得分:0)

可以通过更改模型中文件的类型来解决此问题。

以前它是一个pdfclown文件,现在我已将其更改为MultipartFile。 在我的POST方法中,我已将文件从MultipartFile转换为pdfclown文件,以便能够像以前一样使用它。