Spring MVC:必需的字符串参数'代码不存在

Spring MVC: Required String parameter 'code is not present

我收到以下错误:

Required String parameter 'code' is not present.

我想在输入电子邮件和密码数据时检索此人的代码,即我有以下查询:

SELECT code FROM authentication WHERE email=? AND password=?

控制器:

@RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView customerLogin(@RequestParam("code") String code,@RequestParam("email") String email, @RequestParam("password") String password) {

        ModelAndView mv = new ModelAndView();

        Customer customer = new Customer();
        customer.setCode(code);
        customer.setEmail(email);
        customer.setPassword(password);

        String name = customerDao.loginCustomer(customer);
        String cf=customer.getCode();

        if (name != null) {

            mv.setViewName("redirect:/update/" +cf);

        } else {

            mv.addObject("msg", "Invalid user id or password.");
            mv.setViewName("login");
        }

        return mv;

    }

道:

@Override
        public String loginCustomer(Customer customer) {
            
            String sql = "SELECT code FROM authentication WHERE email=? AND password=?";
            
            List<String> customers = jdbcTemplate.queryForList(sql, new Object[] {customer.getCode(), customer.getEmail(), customer.getPassword() },String.class);
            if (customers.isEmpty()) {
                return null;
            } else {
                return customers.get(0);
            }
        }

login.jsp

<%@ page isELIgnored="false"%>
<html>
<head>
<title></title>
</head>
<body>

    <form action="login" method="post">
        <pre>
        
        Email: <input type="text" name="email" />
    
        Password: <input type="password" name="password" />

        <input type="submit" value="Login" />
        </pre>
    </form>
    ${msg}
</body>
</html>

该异常发生在控制器级别。您没有在 URL.

中传递代码参数

原因很明显:您没有将 code 参数传递给您的控制器方法:

   @RequestMapping(value = "/login", method = RequestMethod.POST)
   public ModelAndView customerLogin(@RequestParam("code") String code,
        @RequestParam("email") String email, @RequestParam("password") String password) {
    
    }

为了解决这个问题,您有两种方法:

一个。不需要 code,因此我们需要在控制器方法中添加 required=false

   @RequestMapping(value = "/login", method = RequestMethod.POST)
   public ModelAndView customerLogin(@RequestParam(value = "code", required=false) String code,
        @RequestParam("email") String email, @RequestParam("password") String password) {


    }

b。在 html 表单中添加 code 字段:

<%@ 页面 isELIgnored="false"%>

<form action="login" method="post">
    <pre>
    
    Code: <input type="text" name="code" />

    Email: <input type="text" name="email" />

    Password: <input type="password" name="password" />

    <input type="submit" value="Login" />
    </pre>
</form>
${msg}