繁体   English   中英

客户端发送的请求在语法上是不正确的。 在春天使用@RequestParam

[英]The request sent by the client was syntactically incorrect. using @RequestParam in spring

我是Spring MVC的新手。 单击按钮时,我创建了一个简单的表单,表单值发送到控制器并在第二个视图中设置。 但是当我点击按钮时它会给我错误

The request sent by the client was syntactically incorrect.

这是我的代码:

控制器:

@Controller
@RequestMapping(value="/admissionform")
public class StudentAdmissionController {

    @RequestMapping(value="/form.html", method = GET)
    public ModelAndView getStudentForm() {
        ModelAndView model = new ModelAndView("StudentForm");
        return model;
    }

    @RequestMapping(value="/submit.html", method = POST)
    public ModelAndView submitStudentForm(@RequestParam("name") String name, 
                                          @RequestParam("password") String password) {
        ModelAndView model = new ModelAndView("SubmitForm");
        model.addObject("msg", "Name is : "+name + " Password is : " + password);
        return model;
    }
}

StudentForm:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Student Form</title>
</head>
<body>          

        <div class="modal-body" id="main-body">
            <form action="submit.html" method="post">
                <div>
                        <label>Email address:</label>
                        <input class="form-control" id="email" name="email">
                </div>
                <div >
                        <label for="pwd">Password:</label>
                        <input id="pwd" name="password">
                </div>
                <input type="submit" value="Submit"/>
            </form>
        </div>      
</body>
</html>

提交表格:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Submitted</title>
</head>
<body>
<div>${msg}</div>
</body>
</html>

告诉我我在代码中犯的错误是什么。 我会很感激:)

默认情况下,使用@RequestParam注释的参数是必需的。 因此,这些参数应该存在于客户端请求中。 submitStudentForm方法中,您有两个名为namepassword必需参数:

@RequestMapping(...)
public ModelAndView submitStudentForm(@RequestParam("name") String name, 
                                      @RequestParam("password") String password) { ... }

然后,您应该传递具有完全相同名称的参数。 目前,您正在传递password参数:

<div>
    <label for="pwd">Password:</label>
    <input id="pwd" name="password">      
</div>

但是对于不幸的name参数没有这样做。 我想你应该将email参数重命名为name

div>
    <label>Email address:</label>
    <input class="form-control" id="email" name="name">
</div>

有关更多信息,您可以查看Spring文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM