grails项目关于域名更新

时间:2015-11-13 09:34:12

标签: grails dom

class UserController {

  def userService;

  def execute() {

        try{
            User user=User.get(params.id.toLong());
            if(user){
              user.name="kevin";
              userService.updateUser(user);
            }
        }catch(Exception e){
            def map = ['exceptionmsg' : e.getMessage()];
            render map as JSON;
        }
   }

}
-------------------
class UserService {

    static transactional = true
    def updateUser(User user)throws Exception{
        user.desc="I'm a boy"
        throw new Exception("this test");
        if(!user.save()){
           throw new RuntimeException(user.error.toString());
        }
    }
}

当然,这不是真正的代码,就像这种格式一样,最终结果是抛出异常而用户是更新。 你知道为什么吗?

1 个答案:

答案 0 :(得分:0)

检查出来:

class UserService {

    static transactional = true
    def updateUser(User user)throws Exception{
        user.desc="I'm a boy"
        //throw new Exception("this test"); 
        if(!user.save()){
           throw new RuntimeException(user.error.toString());
        }
    }
}

在6行中你抛出异常。删除此行。