开发的时候,有时候报错:
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
字面上是参数异常, 在response已经提交之后 不能发送错误请求。
1.
- @Test
- public void testSave() {
- ApplicationContext cxt = new ClassPathXmlApplicationContext("/applicationContext.xml");
- buyerService = (BuyerService)cxt.getBean("buyerService");
- Buyer buyer = new Buyer("dengmingti","123456","test@sina.com");
- ContactInfo contactInfo = new ContactInfo();
- contactInfo.setAddress("百色市朝阳区左家庄");
- contactInfo.setMobile("13671323507");
- contactInfo.setPhone("010-64469090-9");
- contactInfo.setPostalcode("531500");
- buyer.setContactinfo(contactInfo);
- buyer.setGender("男");
- buyer.setRealname("刘德华");
- buyer.setRegTime(new Date());
- buyerService.save(buyer);
- }
解决方法:
(1)从你的文件来看,你应该是使用了spring;
(2)你说用getHibernateTemplate().save(o)可以成功,而getSession.save(o)却不行。有理由怀疑你是在spring的配置文件中配置了事务,而getHibernateTemplate()是Spring的方法,支持声明式事务管理,所以如果你配置了事务,它会自动调用事务并在操作完毕后自动关闭session;而单独的getSession()却与spring没有关系,它是由hibernate控制的,并不支持声明式事务管理,所以你必须调用session.flush()或transaction.commit()才可以成功保存。
(3)我怎样才能使用buyerService.save(buyer)存到数据库中呀?不妨把你的applicationContext.xml文件贴出来,看看你的service和dao关联起来了没(就是在service里有没有引用你配置的dao).现在你的dao里到底是使用哪种方式保存的,都没有说清楚
2.下面看个例子就一目了然了:
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- out.print("上传成功!上传文件为:"+fileName+"<br/>保存的地址为"+filePath+ "!!");
- out.close();
- response.sendRedirect("index.jsp");
首先,利用reponse.getWrite()获得输出流对象,close()之后,这里reponse其实已经提交了。注释下面的sendRedirect代码,执行之后发现response已经进行已经跳转了,只不过url没有发生改变,并且页面上已经有输出上面指定的字符串。
所以当执行上面代码之后 ,reponse 会提交两次,服务器就不知道该怎么办了,所以抛出异常。
解决方案: 去掉out.close() 这里不会因为PrintWriter 输出对象没有关闭而占用资源的。
相关推荐
在Java的Web开发中,`java.lang.IllegalStateException: Cannot call sendError() after the response has been committed` 是一个常见的错误,通常发生在尝试在HTTP响应已经发送到客户端之后调用`sendError()`方法...
"Cannot forward after response has been committed" 是一个在Java Web开发中常见的错误,通常与Servlet、Filter或控制器逻辑有关。这个错误表明服务器已经完成了对HTTP响应的处理,并将其发送到客户端,然后试图...
this document will outline the organisational structure (including role descriptions and communication lines), discuss the methodology model and after presenting the tools used for process control, ...
使用@SessionAttributes 和@ResponseBody 或者@RestController 注解配合使用时,可能会报 Cannot create a session after the response has been committed 错误。解决方法是在 return 之前自己创建 session。 本...
* The "git fast-export/import" pair has been taught to handle commits with log messages in encoding other than UTF-8 better. * In recent versions of Git, per-worktree refs are exposed in refs/...
Returns a boolean indicating whether the named response header has already been set. contextDestroyed(ServletContextEvent) - Method in interface javax.servlet.ServletContextListener Notification ...
For example, a second transaction traversing the doubly linked list mentioned above would see the list before or after the insert, but it will see only complete changes. Durability After a ...
If you attempt to access a reserved address that has not yet been committed (backed by memory or disk) you will cause an access violation. Committed Memory Committed pages are those pages that when ...
curl for windows10 x64 , version Version 7.56.1 (23 Oct 2017) Daniel Stenberg (23 Oct 2017) - RELEASE-NOTES: 7.56.1 ...- imap: if a FETCH response has no size, don't call write callback
What if a fix hasn't been committed yet? 366 What if a ticket has been closed without a fix? 367 Tracking down unreported problems 368 Where to ask questions 369 Tips on asking questions that will...
Section=ResponseHeader Detail=CR 后面必须是 LF The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF 主体意思是微软没有容忍不符合RFC 822...
These dummy transactions get committed when everything is idle (after there has been some activity).
(Committed memory is the physical memory for which space has been reserved on the disk paging file). TOTAL_RESERVED_MEM_COUNTER_009_NAME=# Total reserved Bytes TOTAL_RESERVED_MEM_COUNTER_009_HELP=...
released the first version of the XtraGrid Suite, our focus has been to deliver ground-breaking new capabilities for Visual Studio .NET®. With XtraGrid Suite v2, you have at your disposal a ...
java.lang.IllegalStateException: Can't sendRedirect() after data has been committed to the client. ``` 解决办法是在进行任何输出之前先检查是否需要重定向。 #### 二、`response.setHeader("Location", "")`...
To make edits to changes after they have been uploaded, you should use a tool like git rebase -i or git commit --amend to update your local commits. After your edits are complete: Make sure the ...
1. **cancel** - 取消,废除:用于取消计划或约定,如 "The flight has been cancelled due to bad weather." 2. **variable** - 易变的,可变的:形容词,表示事物可能发生变化,如 "Weather conditions are highly...
Ekman is a leading authority on the study of the facial expressions and their relation to emotion, and this book is a methodical and thorough (for the layperson, at least) introduction to the field, ...
Ekman is a leading authority on the study of the facial expressions and their relation to emotion, and this book is a methodical and thorough (for the layperson, at least) introduction to the field, ...