Grails删除方法

问题描述

Grails删除方法似乎无效。 我想知道是否有什么问题。 我自己解决不了。 如果您能帮助我,我会很高兴。 我是日语不佳的日语。如果我用英语输入错误,对不起。 X(

我提到了教程站点。 https://koji-k.github.io/grails-tutorial/tutorials/todo-application/delete.html

您想做的事 我想使用delete方法删除具有指定ID的元素。

当前状态 可以保存,但不能删除。

开发环境 MacOS Mojave 智能 Grails版本:4.0.1 JVM版本:11.0.2

package hoge

class Hoge {
    String name
    String text
    static constraints = {
    }
}

控制器

    package hoge
    
    class HogeController {
    
        def index() {
            render(view: '/hoge/index',model: [hoges: Hoge.listOrderById()])
        }
    
        def save() {
            String text = params.text
            String name = params.name
            Hoge newHoge = new Hoge(name: name,text: text)
            // DB save
            if (newHoge.validate()) {
                newHoge.save()
                redirect(controller: "hoge",action: "index")
            } else {
                render(view: '/hoge/index',model: [errorHoge: newHoge,hoges: Hoge.listOrderById()])
            }
    
        }
        def delete() {
            Hoge hoge = Hoge.get(params.long("id"))
            if (hoge) {
                hoge.delete()
                flash.message = "deleted"

            } else {
                flash.error = "TodoId:${params.long("id")} is not found"
            }
        redirect(controller: "hoge",action: "index")
    }
}

查看

<!DOCTYPE html>
<html>
<head>
    <meta charaset="UTF-8">
    <title>create page</title>
</head>
<body>
    <g:if test="${flash.message}">
        <div style="background-color: #269abc">${flash.message}</div>
    </g:if>
    <g:if test="${flash.error}">
        <div style="background-color: #db7093">${flash.error}</div>
    </g:if>
    <g:renderErrors bean="${errorHoge}" as="list" field="hogeName"/>


    <p>Name and Text</p>
    <g:form controller="hoge" action="save">
        <g:textField name="name" placeholder="name"/>
        <g:textField name="text" placeholder="text"/>
        <g:submitButton name="Add">Add Todo</g:submitButton>
    </g:form>
    <ul>
    <g:each in="${hoges}" var="hoge">
        <li>${hoge.id} ${hoge.name} ${hoge.text}</li>
        <g:link controller="hoge" action="delete" params="['id':hoge.id]">
            Delete
        </g:link>
        <g:link controller="hoge" action="update" params="['id':hoge.id]">
            kousin
        </g:link>
    </g:each>
    </ul>
</body>

build.gradle

buildscript {
    repositories {
        maven { url "https://repo.grails.org/grails/core" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath "org.grails.plugins:hibernate5:7.0.0"
        classpath "gradle.plugin.com.github.erdi.webdriver-binaries:webdriver-binaries-gradle-plugin:2.0"
        classpath "com.bertramlabs.plugins:asset-pipeline-gradle:3.0.10"
    }
}

version "0.1"
group "hoge"

apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"com.github.erdi.webdriver-binaries"
apply plugin:"org.grails.grails-gsp"
apply plugin:"com.bertramlabs.asset-pipeline"

repositories {
    maven { url "https://repo.grails.org/grails/core" }
}

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

dependencies {
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    compile "org.grails:grails-core"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    compile "org.springframework.boot:spring-boot-starter-tomcat"
    compile "org.grails:grails-web-boot"
    compile "org.grails:grails-logging"
    compile "org.grails:grails-plugin-rest"
    compile "org.grails:grails-plugin-databinding"
    compile "org.grails:grails-plugin-i18n"
    compile "org.grails:grails-plugin-services"
    compile "org.grails:grails-plugin-url-mappings"
    compile "org.grails:grails-plugin-interceptors"
    compile "org.grails.plugins:cache"
    compile "org.grails.plugins:async"
    compile "org.grails.plugins:scaffolding"
    compile "org.grails.plugins:events"
    compile "org.grails.plugins:hibernate5"
    compile "org.hibernate:hibernate-core:5.4.0.Final"
    compile "org.grails.plugins:gsp"
    compileOnly "io.micronaut:micronaut-inject-groovy"
    console "org.grails:grails-console"
    profile "org.grails.profiles:web"
    runtime "org.glassfish.web:el-impl:2.1.2-b03"
    runtime "com.h2database:h2"
    runtime "org.apache.tomcat:tomcat-jdbc"
    runtime "javax.xml.bind:jaxb-api:2.3.0"
    runtime "com.bertramlabs.plugins:asset-pipeline-grails:3.0.10"
    testCompile "org.grails:grails-gorm-testing-support"
    testCompile "org.mockito:mockito-core"
    testCompile "org.grails:grails-web-testing-support"
    testCompile "org.grails.plugins:geb"
    testCompile "org.seleniumhq.selenium:selenium-remote-driver:3.14.0"
    testCompile "org.seleniumhq.selenium:selenium-api:3.14.0"
    testCompile "org.seleniumhq.selenium:selenium-support:3.14.0"
    testRuntime "org.seleniumhq.selenium:selenium-chrome-driver:3.14.0"
    testRuntime "org.seleniumhq.selenium:selenium-firefox-driver:3.14.0"
}

bootRun {
    jvmArgs(
        '-Dspring.output.ansi.enabled=always','-noverify','-XX:TieredStopAtLevel=1','-Xmx1024m')
    sourceResources sourceSets.main
    String springProfilesActive = 'spring.profiles.active'
    systemProperty springProfilesActive,System.getProperty(springProfilesActive)
}

webdriverBinaries {
    chromedriver '2.45.0'
    geckodriver '0.24.0'
}

tasks.withType(Test) {
    systemProperty "geb.env",System.getProperty('geb.env')
    systemProperty "geb.build.reportsDir",reporting.file("geb/integrationTest")
    systemProperty "webdriver.chrome.driver",System.getProperty('webdriver.chrome.driver')
    systemProperty "webdriver.gecko.driver",System.getProperty('webdriver.gecko.driver')
}


assets {
    minifyJs = true
    minifyCss = true
}

It remains even if I try to delete it

如果是todo.delete(刷新:true)

Error 500: Internal Server Error
URI: /hoge/delete/2
Class: javax.persistence.TransactionRequiredException
Message: null
Caused by: no transaction is in progress

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...