简体   繁体   English

POST方法中的Spring MVC模型为空

[英]Spring MVC Model within POST Method is empty

I´m developing a WebApp with Spring MVC. 我正在使用Spring MVC开发WebApp。 The Problem is that my model which I pass through with @ModelAttribute, in my POST Method of my Controller seems to be empty, despite I have filled out the form on the web page. 问题是,尽管我已经在网页上填写了表单,但在控制器的POST方法中使用@ModelAttribute传递的模型似乎为空。

This is the GET Method of my Controller: 这是我的控制器的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;
}

This is the POST Method of my Controller: 这是我的控制器的POST方法:

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

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

Here the "_circleUpModel" does not have any values. 此处的“ _circleUpModel”没有任何值。

This is my form in the related .jsp File with the needed script: 这是我在相关.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);
    }
}

Of course the default values of the CircleUpModel are presented but if I enter some other values in the form of the web page this entrys are not considered in the POST Method, despite I added the modelAttribute="circleUpForm" in my form. 当然,会显示CircleUpModel的默认值,但是如果我以网页的形式输入其他值,尽管我在modelAttribute="circleUpForm"添加了modelAttribute="circleUpForm" ,但在POST方法中不会考虑modelAttribute="circleUpForm"

CircleUpModel: 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;
    }

}

If you remove file upload stuff, you will be able to see the data in your model. 如果删除文件上传内容,则可以查看模型中的数据。 But if you want to do file upload, you have to tweak your code as follows. 但是,如果要上传文件,则必须按照以下方式调整代码。

  1. First, remove member 'file' and its setter and getter methods from model. 首先,从模型中删除成员'file'及其setter和getter方法。
  2. Second, replace the below tag in your JSP 其次,在您的JSP中替换以下标记

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

with simple HTML tag. 使用简单的HTML标签。

    <input name="file" type="file"/>
  1. Modify the signature of your POST method in controller to as follows. 如下修改控制器中POST方法的签名。

     public ModelAndView circleUpPost(HttpServletRequest _request, @ModelAttribute("circleUpModel") CircleUpModel _circleUpModel, @RequestParam("file") MultipartFile _file) { //do your processing } 

Here the last parameter '_file' is of MultipartFile type and contains the file you upload from JSP. 在这里,最后一个参数“ _file”具有MultipartFile类型,并且包含您从JSP上传的文件。

  1. Last and most important step is to register a multipart resolver in Spring configuration file. 最后也是最重要的一步是在Spring配置文件中注册一个多部分解析器。

     <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <beans:property name="maxUploadSize" value="100000" /> </beans:bean> 

Hope this helps. 希望这可以帮助。

This problem could be resolved by changing the type of the file in the model. 通过更改模型中文件的类型,可以解决此问题。

Previously it was a pdfclown file and now I have changed it into a MultipartFile. 以前它是pdfclown文件,现在我将其更改为MultipartFile。 In my POST Method I have converted the file from MultipartFile into pdfclown File to be able to work with it like before. 在我的POST方法中,我已将文件从MultipartFile转换为pdfclown File,以便能够像以前一样使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM