Java Spring启动控制器@RequestParam给出错误;必需的字符串参数';名称';“不存在”;

Java Spring启动控制器@RequestParam给出错误;必需的字符串参数';名称';“不存在”;,java,spring,forms,spring-boot,model-view-controller,Java,Spring,Forms,Spring Boot,Model View Controller,我正在使用Spring boot构建一个应用程序。当我试图将一个文件上传到服务器时,问题开始了,我认为这是一个多部分文件问题。但后来我尝试用POST方法向Spring@Controller提交一个简单的表单,它得到了相同的错误。经过几个小时的搜索,我找不到任何类似的东西 HTML 当我检查browser developer tools时,我可以看到FormData中有一个名为“name”的参数,它具有输入的值 当我使用GET方法时,我在使用@RequestParam时没有问题 这可能与我的项目配

我正在使用Spring boot构建一个应用程序。当我试图将一个文件上传到服务器时,问题开始了,我认为这是一个多部分文件问题。但后来我尝试用POST方法向Spring@Controller提交一个简单的表单,它得到了相同的错误。经过几个小时的搜索,我找不到任何类似的东西

HTML

当我检查browser developer tools时,我可以看到FormData中有一个名为“name”的参数,它具有输入的值

当我使用GET方法时,我在使用@RequestParam时没有问题

这可能与我的项目配置有关,还是我完全遗漏了什么

谢谢


试试这个?

您使用的
@RequestParam
注释用于
GET
参数(通过URL传递),为了检索POST参数的值,您必须使用
@RequestBody
注释

因此,您的代码应该如下所示:

@Controller
public class TestController {

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@RequestBody("name") String name) {
        return "redirect:/";
    }
}

如果重定向并希望使用从重定向地址中的“表单”获取的@Requestparam值。。 您需要使用重定向属性

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@RequestParam("name") String name,RedirectAttributes redirectAttributes) {
    redirectAttributes.addFlashAttribute("name","name");
    return "redirect:/";
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String homeMethod(@ModelAttribue("name") String name){
    return "name"; 
}

ModelAttribute将帮助您获得重定向FlashAttributes..

@RequestParam
用于映射URL参数,例如在
get
中,使用带有DTO的
@RequestBody
。您好,我看您是新来的。如果您认为答案解决了问题,请单击绿色复选标记将其标记为“已接受”
package com.tantsurepertuaar.tantsurepertuaar.controller;

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

@Controller
public class TestController {

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@RequestParam("name") String name) {
        return "redirect:/";
    }
}
    package com.tantsurepertuaar.tantsurepertuaar.controller;

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

@Controller
public class TestController {

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@ModelAttribute String name) {
        return "redirect:/";
    }
}
@Controller
public class TestController {

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@RequestBody("name") String name) {
        return "redirect:/";
    }
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@RequestParam("name") String name,RedirectAttributes redirectAttributes) {
    redirectAttributes.addFlashAttribute("name","name");
    return "redirect:/";
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String homeMethod(@ModelAttribue("name") String name){
    return "name"; 
}