【问题标题】:The request sent by the client was syntactically incorrect. using @RequestParam in spring客户端发送的请求在语法上不正确。在春天使用@RequestParam
【发布时间】:2016-04-30 17:30:33
【问题描述】:

我是 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;
    }
}

学生表格:

<%@ 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>

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

【问题讨论】:

    标签: java spring jsp spring-mvc


    【解决方案1】:

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

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

    那么你应该在你的 from 中传递那些具有完全相同名称的参数。目前,您正在传递 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 documentation

    【讨论】:

      猜你喜欢
      • 2019-05-14
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 2019-02-20
      相关资源
      最近更新 更多