The request sent by the client was syntactically incorrect.-Spring MVC + JDBC Template

前端 未结 6 1639
攒了一身酷
攒了一身酷 2020-11-29 05:52

I am newbie to Spring MVC. I was stuck by an error while running my project Error-The request sent by the client was syntactically incorrect. I have an enti

相关标签:
6条回答
  • 2020-11-29 06:32

    In my case, I try to create an object that has ID and NAME as attributes. ID is int, NAME is String. But my js set values like this ID = '', NAME = 'brabrabra...'

    After set ID = 0, problem is fixed.

    0 讨论(0)
  • 2020-11-29 06:41

    I had a similar issue recently and solved it by annotating my Date field with the @DateTimeFormat. In your case, you would edit your PatientInfo.java file to:

        import org.spring.framework.annotation.DateTimeFormat;
    
        @Column(name = "DateOfBirth")
        @Temporal(TemporalType.TIMESTAMP)
        @DateTimeFormat(pattern = ${pattern})
        private Date dateOfBirth;
    

    Make sure to replace ${pattern} with a string representing the format that will be received (e.g. "yyyy-MM-dd").

    0 讨论(0)
  • 2020-11-29 06:48

    I think the issue is that Spring doesn't know how to deserialize the date your browser client sends when submitting the following input field in

    <tr name="tstest">
        <td>Date Of Birth</td>
        <td><form:input path="dateOfBirth" name="timestamp" value=""/>
            <a href="javascript:show_calendar('document.tstest.timestamp', document.tstest.timestamp.value);"><img src="../images/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the timestamp"></a>
        </td>
    </tr>
    

    Spring doesn't know how to take the value that you enter into that field and convert it into a Date object. You need to register a PropertyEditor for that. For example, add the following to your @Controller class

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        sdf.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
    

    Obviously, change the SimpleDateFormat to whatever your client is sending.


    On a related note, you're sending a 302 response by sending a redirect

    return "redirect:/full-reg";
    

    Remember that request and model attributes only live for the duration of one request. So when your client send the request to full-reg, none of the form input parameters you sent originally exist any more. You should re-think how you do this.

    0 讨论(0)
  • 2020-11-29 06:52

    I came across the same error this morning. The problem with my code was that I had declared a variable as an integer in my form binding object but on the actual form I was capturing text. Changing the variable to the correct type worked out for me

    0 讨论(0)
  • 2020-11-29 06:53

    This happens when the defined binding does not match to what the user is sending. The most common issues are:

    • Missing PathVariable declaration
    • Incomplete PathVariable declaration (for example missing value="variableName")
    • Wrong data type, such as Sotirios Delimanolis answer above. If the Class of an input parameter cannot be serialized the request is not processable

    So, in general, be sure that:

    • Each PathVariable is declared
    • A value is assigned that corresponds to value to be match in the path - @PathVariable(value="myVariableName", String myVariable) where the path defines @RequestMapping(value = "/userInfo/myVariableName", method = RequestMethod.GET)
    • Each class declared for a PathVariable must be serializable.
    0 讨论(0)
  • 2020-11-29 06:56

    try with this (with /add-update2 instead of just add-update2) and replace modelAttribute by commandName

    <form:form commandName="patientInfo" method="POST" action="/add-update2">
    
    0 讨论(0)
提交回复
热议问题