[grailsunion:1700] delele action 中的 redirect 無效?

43 views
Skip to first unread message

derekypp peng

unread,
May 10, 2010, 1:57:30 AM5/10/10
to grail...@googlegroups.com
各位好,好久沒有上來跟大家交流,最近遇了一個問題,一直找不到原因
我有一個delete atction (是從acegi plugin 挑出來的)

  /**
     * Person delete action. Before removing an existing person,
     * he should be removed from those authorities which he is involved.
     */
    def delete = {

        def person = CmUser.get(params.id)
        if (person) {
            def authPrincipal = authenticateService.principal()
            //avoid self-delete if the logged-in user is an admin
            if (!(authPrincipal instanceof String) && authPrincipal.username == person.username) {
                flash.message = "You can not delete yourself, please login as another admin and try again"
            }
            else {
                //first, delete this person from People_Authorities table.
            
                try{
                    CmRole.findAll().each { it.removeFromPeople(person) }
                    person.delete(flush:true)
                    println "test!!!"
                    flash.message = "CmUser $params.id deleted."
                    redirect action: list
                }
                catch(org.springframework.dao.DataIntegrityViolationException e){
                     flash.message = "訪視員不能被刪除"
                     flash.args = [params.id]
                     redirect(action: "show", id: params.id)
                     return
                }
            }
        }
        else {
            flash.message = "CmUser not found with id $params.id"
            redirect action: list
        }
       
    }

就是刪除一個User 但這個user 當了別的foreign key 所以不能刪除
所以我加了一個try catch這段execption 可是網頁卻一直導向到錯誤頁面
可是使用者應該不想看這麼多密密麻麻錯誤訊息

我有寫
                     flash.message = "訪視員不能被刪除"
                     flash.args = [params.id]
                     redirect(action: "show", id: params.id)
但是始終導不過去,請問各位前輩能否指點迷津 謝謝

Grails Runtime Exception

Error Details

Error 500:
Servlet: default
URI: /childminder/cmUser/index
Exception Message: Cannot delete or update a parent row: a foreign key constraint fails (`childminder`.`cm_meical_check`, CONSTRAINT `FK508FE45B2F6A8D41` FOREIGN KEY (`create_user_id`) REFERENCES `cm_user` (`id`))
Caused by: Executing action [delete] of controller [CmUserController] caused exception: org.springframework.dao.DataIntegrityViolationException: could not delete: [CmUser#1]; SQL [delete from cm_user where id=? and version=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [CmUser#1]
Class: CmUserController
At Line: [68]
Code Snippet:
67: redirect(action: "show", id: params.id)
68: return
69: }

Stack Trace

org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException: Executing action [delete] of controller [CmUserController]  caused exception: org.springframework.dao.DataIntegrityViolationException: could not delete: [CmUser#1]; SQL [delete from cm_user where id=? and version=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [CmUser#1]

at java.lang.Thread.run(Thread.java:619)
Caused by: org.codehaus.groovy.runtime.InvokerInvocationException: org.springframework.dao.DataIntegrityViolationException: could not delete: [CmUser#1]; SQL [delete from cm_user where id=? and version=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [CmUser#1]
... 1 more
Caused by: org.springframework.dao.DataIntegrityViolationException: could not delete: [CmUser#1]; SQL [delete from cm_user where id=? and version=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [CmUser#1]
at CmUserController$_closure4.doCall(CmUserController:68)
at CmUserController$_closure4.doCall(CmUserController)
... 1 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not delete: [CmUser#1]
at $Proxy10.flush(Unknown Source)
... 3 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`childminder`.`cm_meical_check`, CONSTRAINT `FK508FE45B2F6A8D41` FOREIGN KEY (`create_user_id`) REFERENCES `cm_user` (`id`))
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1016)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)


 

--
您收到此邮件是因为您订阅了 Google 网上论坛的“Grails联盟-GrailsUnion”论坛。
要向此网上论坛发帖,请发送电子邮件至 grail...@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 grailsunion...@googlegroups.com
若有更多问题,请通过 http://groups.google.com/group/grailsunion?hl=zh-CN 访问此网上论坛。
349.gif

Tigerf

unread,
May 10, 2010, 9:46:43 AM5/10/10
to grail...@googlegroups.com
被throw出来的Exception并不是DataIntegrityViolationException ,而是又被包装了一层的一个Exception。建议你直接catch (Exception e)看看究竟是一个什么类再考虑如果修改。


Thanks & Best Regards
Tiger Feng


2010/5/10 derekypp peng <dere...@gmail.com>
349.gif

derekypp peng

unread,
May 10, 2010, 11:22:42 AM5/10/10
to grail...@googlegroups.com
我已經改成catch (Exception e) 而且也有跑到catch裡面去
    catch(Exception e){                 
                     flash.message = "訪視員不能被刪除"
                     flash.args = [params.id]
                     redirect(action: "show", id: params.id)
                     return
                }
但仍然是顯示下面的錯誤頁面,而不是導到show這個gsp 去


Error 500:
Servlet: default
URI: /childminder/cmUser/index
Exception Message: Cannot delete or update a parent row: a foreign key constraint fails (`childminder`.`cm_meical_check`, CONSTRAINT `FK508FE45B2F6A8D41` FOREIGN KEY (`create_user_id`) REFERENCES `cm_user` (`id`))
Caused by: Executing action [delete] of controller [CmUserController] caused exception: org.springframework.dao.DataIntegrityViolationException: could not delete: [CmUser#1]; SQL [delete from cm_user where id=? and version=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [CmUser#1]
Class: CmUserController
At Line: [69]
Code Snippet:



349.gif

Tigerf

unread,
May 10, 2010, 11:29:02 AM5/10/10
to grail...@googlegroups.com
哦,这个有点没头绪了。试试把redirect那一行写成:
return redirect(action: "show", id: params.id)
349.gif

derekypp peng

unread,
May 10, 2010, 11:40:58 AM5/10/10
to grail...@googlegroups.com
仍然不行呢 :(
349.gif

Ford Guo

unread,
May 10, 2010, 11:43:04 AM5/10/10
to grail...@googlegroups.com
根据提示,这个应是Grails提前已经将Exception捕获了,建议你自己增加逻辑进行判断.

Ford Guo


2010/5/10 derekypp peng <dere...@gmail.com>
349.gif

Tigerf

unread,
May 10, 2010, 11:50:50 AM5/10/10
to grail...@googlegroups.com
如果catch到了,怎么会还被其他代码捕获到呢?不解。


Thanks & Best Regards
Tiger Feng


2010/5/10 Ford Guo <agil...@gmail.com>
349.gif

derekypp peng

unread,
May 10, 2010, 11:53:05 PM5/10/10
to grail...@googlegroups.com

請問一下 ford guo
你說的『根据提示,这个应是Grails提前已经将Exception捕获了』

根据提示是從哪裡看出來的,我也想多知道一點 從Exception中所能了解的事情

謝謝
349.gif

Ford Guo

unread,
May 11, 2010, 12:09:53 AM5/11/10
to grail...@googlegroups.com
详细的grails源代码,没有看过,只是感觉,因为这是比较底层的JDBC抛出的代码,因此在Hibernate,Grails等层次上,应该做过处理了.

Ford Guo


2010/5/11 derekypp peng <dere...@gmail.com>
349.gif

haitao_gao

unread,
May 11, 2010, 6:06:54 AM5/11/10
to grail...@googlegroups.com
倾向于认同原帖作者redirect无效的观点(数据库有外键但不能级联删除时现象是这样)
person.delete(flush:true)
去掉fiush:true的提示更直接一些:

Grails Runtime Exception

Error Details

Error 500: 
Servlet: default
URI: /webproject/country/index
Exception Message: Cannot issue a redirect(..) here. The response has already been committed either by another redirect or by directly writing to the response. 

可以通过查找grails源码中的“500”看下,还没有理顺,就不多说了^_^



网易为中小企业免费提供企业邮箱(自主域名)

Ford Guo

unread,
May 11, 2010, 6:17:20 AM5/11/10
to grail...@googlegroups.com
这个是跟DB/Grails的事务有关系的,所以最好把这个问题提到maillist中.

Ford Guo


2010/5/11 haitao_gao <haita...@163.com>
349.gif

derekypp peng

unread,
May 11, 2010, 12:03:46 PM5/11/10
to grail...@googlegroups.com
剛剛google一下

發現也有人在討論
http://grails.1312388.n4.nabble.com/1-2M3-delete-catching-database-errors-no-redirect-but-still-error500-with-exception-td1355752.html#a1355753

而且這個問題 已經也有人提交了
http://jira.codehaus.org/browse/GRAILS-5277

不過上面說   1.2-M4   就會被修正
可是我的project目前已經是1.2版了

>______< 怎麼還會這樣阿 (難得因為是我從1.1.1 升級過來的原因嗎)
349.gif
Reply all
Reply to author
Forward
0 new messages