2016-08-19 20 views
0

我想在一個複選框上進行ajax調用,該複選框會在單擊複選框時將用戶角色保存或刪除到數據庫。Ajax調用保存來自grails show.gsp的數據接收「Session is Closed!」異常

AJAX:

$('.roleCheckbox').click(function() { 
var checked = $(this).is(':checked'); 

//var id = url.substring(url.lastIndexOf('/') + 1); 

//alert($(this).attr("context") + "/userRole/addUserRole/"); 
var inputData = {userid: $(this).attr('userid'), 
        roleid: $(this).val()}; 

if(checked){ 
    //alert($(this).attr("context") + "/userRole/addUserRole/"); 
    //ajax add userRole 
    $.ajax({ 
     url: $(this).attr("context") + "/userRole/addUserRole/", 
     method:"POST", 
     contentType:"application/json; charset=utf-8", 
     data: JSON.stringify(inputData), 
     cache: false, 
     success: function(data) { 
      alert("success updating role " + $(this.val())); 
     }, 
     error: function(request, status, error) { 
      //alert("error updating role "); 
     }, 
     complete: function() { 
      //alert("complete"); 
     } 
    }); 
}else{ 
    //ajax remove userRole 
    alert("in unchecked condition: " + $(this).attr("context") + "/userRole/addUserRole/"); 
    $.ajax({ 
     url: $(this).attr("context") + "/userRole/removeUserRole/", 
     data: JSON.stringify(inputData), 
     cache: false, 
     success: function(data) { 
      alert("success deleting role " + $(this.val())); 
     }, 
     error: function(request, status, error) { 
      alert("error deleting role " + $(this.val()) + " " + error); 
     }, 
     complete: function() { 

     } 
    }); 
} 
}); 

控制器:

@Transactional 
def addUserRole() { 

    def jsonObj = request.JSON 

    UserService.addUserRole(jsonObj) 


} 

服務:

@Transactional 
def addUserRole(JSONObject jsonObj) { 

    def currentSession = sessionFactory.getCurrentSession() 

    UserRole.withTransaction{ status -> 
     def user = User.get(jsonObj.userid) 
     def role = Role.get(jsonObj.roleid) 

     def userRole = new UserRole(); 

     userRole.user = user 
     userRole.role = role 

     userRole.validate(); 
     if(userRole.hasErrors()){ 
      flash.message = userRole.errors 
      respond user 
     }else{ 
      currentSession.save(userRole) 
     } 
    } 

    currentSession.flush() 

} 

的問題是,當控制器函數返回我得到一個錯誤:

組織.hibernate.S essionException:會話已關閉!

Grails的3.2.0.M2

我看了遍,不能弄清楚如何正確管理這些會話。

感謝,

編輯:

添加渲染(狀態:200)控制器年底取得成功的AJAX調用返回,但是這只是掩蓋「會議是關閉!」我收到異常。

我嘗試了在服務中使用.withTransaction而不是.withNewSession 的建議,結果相同。

我認爲這可能需要做更多的spring-security-plugin,因爲即使是grails腳手架創建的UserRoleController方法save()也會發出「Session is Closed!」保存時例外。

您可以在運行時更新UserRole(PersonAuthority)域嗎?

+0

嘗試在您的操作中在'UserService.addUserRole(jsonObj)'之後添加'render(status:200)'。 –

回答

0

請勿手動刷新會話。讓Grails/Hibernate爲你做這件事。考慮更改此代碼:

@Transactional 
def addUserRole(JSONObject jsonObj) { 
    UserRole.withTransaction { status -> 
     def user = User.get(jsonObj.userid) 
     def role = Role.get(jsonObj.roleid) 

     def userRole = new UserRole(); 

     userRole.user = user 
     userRole.role = role 

     userRole.validate() 
     if (userRole.hasErrors()) { 
      flash.message = userRole.errors 
      return 
     } else { 
      currentSession.save(flush: true) 
     } 
    }  
} 

而且,在你的行動在最後添加render(status: 200)

相關問題