簡體   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