必需的字符串参数“ firstdropdown”不存在

时间:2019-05-24 10:05:19

标签: html spring-boot

我是SpringBoot的新手。我正在尝试制作一个小型应用程序,该程序可以在我选择的目录中上传csv文件。为此,用户将使用两个字段(问题类型和粒度)填写一个小表格。根据用户选择的内容,文件将被上传到该目录中。例如,如果用户选择要借出的问题而选择每周要选择的粒度,则文件将存储在C:\Users\loan\weekly中。

这是我的代码:

UploadingController.java。

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UploadingController {

    @RequestMapping(value = "/dropdown", method = RequestMethod.GET)
    public ModelAndView user() { 
       Dropdown dropdown = new Dropdown();
       dropdown.setIssue("Loan");
       dropdown.setGranularity("Daily");
      ModelAndView modelAndView = new ModelAndView("dropdown", "command", dropdown);
      return modelAndView;
    }
    @RequestMapping(value = "/addDropdown", method = RequestMethod.POST)
    public String addUser(@ModelAttribute("SpringWeb")Dropdown dropdown, 
       ModelMap model) {
       model.addAttribute("issue", dropdown.getIssue());
       model.addAttribute("granularity", dropdown.getGranularity());   
       return "dropdown";
    }
    @ModelAttribute("firstdropdown")
    public Map<String, String> getFirstDropdown() {
       Map<String, String> firstdropdown = new HashMap<String, String>();
       firstdropdown.put("Loan", "Loan");
       firstdropdown.put("Lease", "Lease");
       firstdropdown.put("Other", "Other");
       return firstdropdown;
    }

    @ModelAttribute("seconddropdown")
    public Map<String, String> getSecondDropdown() {
       Map<String, String> seconddropdown = new HashMap<String, String>();
       seconddropdown.put("Daily", "Daily");
       seconddropdown.put("Weekly", "Weekly");
       seconddropdown.put("Monthly", "Monthly");
       return seconddropdown;
    }

    public static String uploadingDir = System.getProperty("user.home");

    @GetMapping("/uploading")
    public String uploading(Model model) {
        File file = new File(uploadingDir);
        model.addAttribute("files", file.listFiles());
        return "uploading";
    }

    @PostMapping("/uploading-post")
    public String uploadingPost(@RequestParam("uploadingFiles") MultipartFile uploadingFiles, 
                                @RequestParam("firstdropdown") String firstdropdown, 
                                @RequestParam("seconddropdown") String seconddropdown) throws IOException {

        File file = new File(uploadingDir + uploadingFiles.getOriginalFilename());
        uploadingFiles.transferTo(file);

        return "redirect:/dropdown"; // redirect accordingly
    }

}

而且,这就是我通过添加以下代码块来调整uploading.ftl的方式。作为HTML代码,它可以很好地工作:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Uploading Files Example with Spring Boot, Freemarker</title>
    </head>

    <body onload="updateSize();">
        <form name="uploadingForm" enctype="multipart/form-data" action="/" method="POST">
        <select name="mydropdown">
<option value="Blank"> </option>
<option value="Loan">Loan</option>
<option value="Lease">Lease</option>
<option value="Other">Other</option>
</select>

<select name="seconddropdown">
<option value="Blank"> </option>
<option value="Daily">Daily</option>
<option value="Weekly">Weekly</option>
<option value="Monthly">Monthy</option>
</select>
            <p>
                <input id="fileInput" type="file" name="uploadingFiles" onchange="updateSize();" multiple>
                selected files: <span id="fileNum">0</span>;
                total size: <span id="fileSize">0</span>
            </p>
            <p>
                <input type="submit" value="Upload files">
            </p>
        </form>
        <div>
            <div>Uploaded files:</div>
            <#list files as file>
            <div>
            ${file.getName()}
            </div>
            </#list>
        </div>
        <script>
            function updateSize() {
                var nBytes = 0,
                        oFiles = document.getElementById("fileInput").files,
                        nFiles = oFiles.length;
                for (var nFileId = 0; nFileId < nFiles; nFileId++) {
                    nBytes += oFiles[nFileId].size;
                }

                var sOutput = nBytes + " bytes";
                // optional code for multiples approximation
                for (var aMultiples = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"], nMultiple = 0, nApprox = nBytes / 1024; nApprox > 1; nApprox /= 1024, nMultiple++) {
                    sOutput = nApprox.toFixed(3) + " " + aMultiples[nMultiple] + " (" + nBytes + " bytes)";
                }
                // end of optional code

                document.getElementById("fileNum").innerHTML = nFiles;
                document.getElementById("fileSize").innerHTML = sOutput;
            }
        </script>
    </body>
</html>

Dropdown.java类:

package com.hellokoding.uploadingfiles;

public class Dropdown {
    private String issue;
    private String granularity;
    public String getIssue() {
        return issue;
    }
    public void setIssue(String issue) {
        this.issue = issue;
    }
    public String getGranularity() {
        return granularity;
    }
    public void setGranularity(String granularity) {
        this.granularity = granularity;
    }

}

和Application.Java:

package com.hellokoding.uploadingfiles;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.io.IOException;

@SpringBootApplication
public class Application {
    public static void main(String[] args) throws IOException {
        new File(UploadingController.uploadingDir).mkdirs();
        SpringApplication.run(Application.class, args);
    }
}

DropdownController.java类:

package com.hellokoding.uploadingfiles;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller("/dropdown")
public class DropDownController {

    @RequestMapping(value = "/issueTypes", method = RequestMethod.GET)
    public Map<String, String> getFirs() {
        Map<String, String> firstdropdown = new HashMap<String, String>();
        firstdropdown.put("issueTypes", "Loan, Lease, Ranl");
        return firstdropdown;
    }

    @RequestMapping(value = "/granularities", method = RequestMethod.GET)
    public Map<String, String> getGranularity() {
        Map<String, String> granularities = new HashMap<String, String>();
        granularities.put("granularities", "Daily, Weekly ,Monthly");
        return granularities;
    }

}

Application.properties文件:

spring.freemarker.template-loader-path: /
spring.freemarker.suffix: .ftl
issue.type=Loan,Leas,Rank
report.granularity=Daily,Weekly,Monthly

我得到的错误:

javax.servlet.ServletException: Circular view path [dropdown]: would dispatch back to the current handler URL [/dropdown] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
    at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:209) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:147) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:316) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
    at java.base/java.lang.Thread.run(Thread.java:835) ~[na:na]

1 个答案:

答案 0 :(得分:1)

您的问题是您的HTML代码。您有两种不同的表单用于发布表单。 像这样纠正它:

    this.svg.append('g')
.attr('transform', 'translate(0,' + this.height + ')')
.call(this.xAxis.tickFormat(()=>{this.multiFormat()}));

更新

按如下方式更新您的控制器:

    <form action="/" method="GET">

        <input list="firstdropdown" name="firstdropdown">
        <datalist id="firstdropdown">
            <option value="Loan">Loan</option>
            <option value="Lease">Lease</option>
            <option value="Other">Other</option>
        </datalist>

        <input list="seconddropdown" name="seconddropdown">
        <datalist id="seconddropdown">
            <option value="Daily">Daily</option>
            <option value="Weekly">Weekly</option>
            <option value="Monthly">Monthy</option>
        </datalist>

        <button type="submit">SEND DATA</button>

    </form>

更改HTML,例如:

@Controller
public class UploadingController {

    public static String vary;
    public static String uploadingDir = System.getProperty("user.home") + vary;

    @GetMapping("/dir")
    @ResponseBody
    public String dir(@RequestParam("firstdropdown") String firstdropdown, 
                      @RequestParam("seconddropdown") String seconddropdown) {
        vary = "/" + firstdropdown + "/" + seconddropdown + "/";
        return vary;
    }

    @GetMapping("/uploading")
    public String uploading(Model model) {
        File file = new File(uploadingDir);
        model.addAttribute("files", file.listFiles());
        return "uploading";
    }

    @PostMapping("/uploading-post")
    public String uploadingPost(@RequestParam("uploadingFiles[]") MultipartFile[] uploadingFiles, 
                                @RequestParam("firstdropdown") String firstdropdown, 
                                @RequestParam("seconddropdown") String seconddropdown) throws IOException {

        for(MultipartFile uploadedFile : uploadingFiles) {
            File file = new File(uploadingDir + uploadedFile.getOriginalFilename());
            uploadedFile.transferTo(file);
        }

        return "redirect:/uploading"; // redirect accordingly
    }
}

更新

在您的<p> <input id="fileInput" type="file" accept=".csv,text/csv" name="uploadingFiles[]" onchange="updateSize();"> selected files: <span id="fileNum">0</span>; total size: <span id="fileSize">0</span> </p> 文件中应用这些设置

application.properties

如果您使用的是Spring Security,请忽略该映射,例如:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
spring.http.multipart.enabled=true

对地图的某些更改

@Override
protected void configure(HttpSecurity http) throws Exception {

    String[] ignoredCsrfMappings = {"/file-test"};
    http.csrf().ignoringAntMatchers(ignoredCsrfMappings);
    // rest of your settings
}

现在应该可以了。