我正在使用 Spring MVC 3.4.0。当我尝试CRUD示例时,我在浏览器控制台中收到此错误;
HTTP状态400 - 客户端发送的请求是语法上的 不正确。
我的代码:
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;
}
}
编辑功能;
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();
}
}
我的JAVA控制台没有错误。有什么问题?
答案 0 :(得分:3)
试试这个适用于你 在您的控制器中
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat,false));
}
答案 1 :(得分:1)
错误几乎可以肯定,Spring无法转换您在此<input>
字段中输入的日期
<input name="CREATE_DATE" type="date" value="${blogList.createDate}" size="30"/>
进入传递给
的参数 @RequestParam( value = "CREATE_DATE", required=false) Date date
因此,请求是错误请求,Spring会返回400状态代码。
在您的请求参数中添加@DateTimeFormat
注释。例如,
@RequestParam( value = "CREATE_DATE", required=false) @DateTimeFormat("dd-MM-yyyy") Date date
然后确保您的客户端发送适当的日期格式。例如
14-01-2014