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

时间:2018-10-04 12:39:45

标签: java spring forms spring-boot model-view-controller

我正在使用Spring Boot构建一个应用程序。当我尝试将文件上传到服务器并认为这是MultipartFile问题时,问题开始了。但是后来我尝试使用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>

pom.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中有一个名为“名称”的参数,它具有输入的值。

当我使用GET方法时,使用@RequestParam不会出现任何问题。

这可能与我的项目配置有关,还是我在这里完全错过了一些东西?

谢谢

3 个答案:

答案 0 :(得分:1)

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

尝试一下?

答案 1 :(得分:1)

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

所以您的代码应如下所示:

@Controller
public class TestController {

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

答案 2 :(得分:1)

如果您重定向并想使用从重定向地址中的“ form”获取的@Requestparam值。 您需要使用Redirectattributes。

@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将帮助您获得redirectflashattributes ..