Question

I am using Spring MVC 3.4.0. When i tried CRUD example I get this error in my browser console;

HTTP Status 400 - The request sent by the client was syntactically incorrect.

My codes:

EditBlog.jsp;

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>

<form  action="/testhibernate/edit/${blogID}/" method="POST" name="BLOG">
    <h3>TITLE</h3>
    <input name="TITLE" type="text" value="${blogList.title}" size="30"/>
     <h3>CREATE_DATE</h3>
     <input name="CREATE_DATE" type="date" value="${blogList.createDate}" size="30"/>
     <h3>TEXT_CONTENT</h3>
     <input name="TEXT_CONTENT" type="text" value="${blogList.textContent}" size="30"/>
     <br>

    <input name="action" type="submit" value="Edit" size="30"/> 
</form> 

editController ;

package com.basari.testhibernate;

import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.basari.blog.Blog;
import com.basari.blog.User_Blog_App;

@Controller
public class editController {

    @Autowired
    private User_Blog_App deneme1;

    @RequestMapping(value = "/edit/{blogID}", method = RequestMethod.GET)
    public ModelAndView findBlog(@PathVariable Integer blogID)
     {
      ModelAndView mav = new ModelAndView("editBlog");
      Blog blog= deneme1.findBlog(blogID);
      mav.addObject("blogList", blog);
      return mav;
     }

     @RequestMapping(value = "/edit/{blogID}/", method = RequestMethod.POST)
     public ModelAndView editBlog(@PathVariable Integer blogID

                  , @RequestParam( value = "TITLE", required=false) String title
                  , @RequestParam( value = "TEXT_CONTENT", required=false) String content
                  , @RequestParam( value = "CREATE_DATE", required=false) Date date


     ) {
           Blog blog= deneme1.findBlog(blogID);


             blog.setTitle( title );
             blog.setTextContent( title );
             blog.setCreateDate(date);

             this.deneme1.editBlog( blog );

             ContactController.logger.debug( "contactPost action called ");

             try{


             }finally{

             }
             ModelAndView mav = new ModelAndView("editBlog");
             mav.addObject("blogList", blog);
             return mav;


     }

}

edit Function ;

public void editBlog(Blog blog){

    Session session =sessionFactory.openSession();
    Transaction tx= null;

    try{
        tx= session.beginTransaction();
        session.update(blog);
        tx.commit();
    }
    catch(HibernateException e){

        if (tx!=null) tx.rollback();
         e.printStackTrace(); 

    }finally{
        session.close();
    }       

}

And there is no error in my JAVA console. What is wrong?

Was it helpful?

Solution 2

The error is almost certainly that Spring cannot convert the date you enter in this <input> field

 <input name="CREATE_DATE" type="date" value="${blogList.createDate}" size="30"/>

into an argument to pass to

 @RequestParam( value = "CREATE_DATE", required=false) Date date

As a result, the request is a Bad Request and Spring returns you a 400 status code.

Add a @DateTimeFormat annotation to your request parameter. For example,

 @RequestParam( value = "CREATE_DATE", required=false) @DateTimeFormat("dd-MM-yyyy") Date date

And then make sure your client sends the appropriate format of date. For example

14-01-2014

OTHER TIPS

Try this It will work for you In your controller

@InitBinder

protected void initBinder(WebDataBinder binder) {

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat,false));

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top