SpringMVC中传值有些值为null使用@RequestParam(“name“)解决

时间:2022-07-28
本文章向大家介绍SpringMVC中传值有些值为null使用@RequestParam(“name“)解决,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

代码

package controller;

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

/**
 * zt
 * 2020/10/9
 * 20:44
 */
@Controller
public class MyController {
    @RequestMapping("/addUser")
    public String addUser(@RequestParam("name") String username, String password, String email,Integer age){
        System.out.println("username = " + username + ", password = " + password + ", email = " + email + ", age = " + age);
        return "hello2";
    }

}

hello.jsp

<%--
  Created by IntelliJ IDEA.
  User: 49841
  Date: 2020/10/9
  Time: 20:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>
        <form method="post" action="addUsers">
            username:<input type="text" name="name" ><br>
            password:<input type="text" name="password"><br>
            email:<input type="text" name="email"><br>
            age:<input type="text" name="age"><br>
            <input type="submit" value="提交">
        </form>
</body>
</html>

此时前端jsp页面中定义的属性名与controller中定义的不一样,一个叫name一个叫username,需要@RequestParam(“name”)解决