如何将域中的布尔值 false 更改为控制器中的布尔值 true

How to change boolean false in a domain to boolean true in a controller

我有一个带有布尔变量“false”的域 class。现在我想在我的控制器中的一个方法中将这个布尔值更改为“true”,我该如何解决这个问题?

谢谢!

域:

class Test {
 
  Boolean status = false

  static constraints = {
   
  }
}

控制器方法:

def setTestDone(){
        def setTestDone = Test.get(params.id)
        try{
            setTestDone.status = true
        } catch (RuntimeException runtimeException) {
            flash.error = runtimeException.message
        }
        redirect(controller:"test", action: "index")
    }

您需要调用 save() 方法将更改保存到数据库:

 def setTestDone = Test.get params.id
 setTestDone.status = true
 if( !setTestDone.save() )
   println setTestDone.errors

有关其他选项的详细信息,请参阅 ref-doc

如果您使用的是 grails,请尝试以下操作

class Test {
 
  boolean status // by default is false

  static constraints = {
   
  }
}

控制器

def setTestDone(){
   def setTestDone = Test.get(params.id)
   setTestDone.status = true
   setTestDone.save(flush:true)
   redirect(controller:"test", action: "index")
}

flush flag 会立即调用事务,但首先我建议有一个服务可以实现这种工作,服务层默认是事务性的,所以你可以有这样的东西:

服务

@Transactional
class TestService {
     Test save(id){
        Test test = Test.get(id)
        test.status = true
        test.save(flush:true)
     }
}

你的控制器应该是这样的:

控制器

class TestController {

  def testService // injected by convention

  def updateTest() {
     testService.save(params.id)
     redirect(controller:"test", action: "index")
  }
}

希望这对你有用。

https://github.com/jeffbrown/mrcreatesavestatus 查看项目。

https://github.com/jeffbrown/mrcreatesavestatus/blob/95ac4b7f2826b94746bc8825fc94dbaa6812ee66/grails-app/domain/mrcreatesavestatus/Test.groovy

package mrcreatesavestatus

class Test {
    Boolean status
}

https://github.com/jeffbrown/mrcreatesavestatus/blob/95ac4b7f2826b94746bc8825fc94dbaa6812ee66/grails-app/services/mrcreatesavestatus/TestService.groovy

package mrcreatesavestatus

import grails.gorm.services.Service

@Service(Test)
interface TestService {
    Test get(Serializable id)

    Test save(Boolean status)

    Test update(Serializable id, Boolean status)
}

https://github.com/jeffbrown/mrcreatesavestatus/blob/95ac4b7f2826b94746bc8825fc94dbaa6812ee66/grails-app/controllers/mrcreatesavestatus/TestController.groovy

package mrcreatesavestatus

class TestController {

    TestService testService

    def updateStatusTrue(Long id) {
        respond testService.update(id, true)
    }

    def updateStatusFalse(Long id) {
        respond testService.update(id, false)
    }

    def show(Long id) {
        respond testService.get(id)
    }
}

https://github.com/jeffbrown/mrcreatesavestatus/blob/95ac4b7f2826b94746bc8825fc94dbaa6812ee66/grails-app/init/mrcreatesavestatus/BootStrap.groovy

package mrcreatesavestatus

class BootStrap {

    TestService testService
    def init = { servletContext ->
        // save a Test instance with
        // status set to false
        testService.save false
    }
    def destroy = {
    }
}

这些更新效果很好:

 $ http :8080/test/show/1.json
HTTP/1.1 200 
Connection: keep-alive
Content-Type: application/json;charset=UTF-8
Date: Fri, 11 Dec 2020 13:22:50 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked

{
    "id": 1,
    "status": false
}

 $ http :8080/test/updateStatusTrue/1.json
HTTP/1.1 200 
Connection: keep-alive
Content-Type: application/json;charset=UTF-8
Date: Fri, 11 Dec 2020 13:23:02 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked

{
    "id": 1,
    "status": true
}

 $ http :8080/test/updateStatusFalse/1.json
HTTP/1.1 200 
Connection: keep-alive
Content-Type: application/json;charset=UTF-8
Date: Fri, 11 Dec 2020 13:23:09 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked

{
    "id": 1,
    "status": false
}