Java Spring 引导控制器@RequestParam给出错误"Required String parameter 'name' is not present"



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

.HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="/webjars/bootstrap/4.1.3/css/bootstrap.min.css"/>
<script type="text/javascript" src="/webjars/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="/webjars/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<title>Title</title>
</head>
<body>
<div class="container">
<div class="card">
<div class="card-body">
<div class="offset-3 col-6">
<form method="post"
action="/save">
<input type="text"  class="form-control" name="name">
<input type="submit" class="btn btn-primary" value="Save">
</form>
</div>
</div>
</div>
</div>
</body>
</html>

绒球.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tantsurepertuaar</groupId>
<artifactId>tantsurepertuaar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>tantsurepertuaar</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-security</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.security</groupId>-->
<!--<artifactId>spring-security-test</artifactId>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

控制器

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:/";
}
}

当我检查浏览器开发人员工具时,我可以看到在 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:/";
}
}
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:/";
}
}

试试这个?

如果您重定向并想使用从重定向地址中的"form"获得@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"; 
}

模型属性将帮助您获得重定向闪存属性..

相关内容