grails - 如何在grails中调用函数

标签 grails groovy

我是一个初学者。所以,请忍受我。我有一个域类和一个 Controller 和一项服务。我创建了两种方法;一种是获取列表,另一种是获取参数并返回参数以检查其是否接受值。但我不知道如何调用该函数。正如我曾经键入localhost:8080 / projectname / controllername / methodname。它返回404 View 未找到。
这是代码
包是testone

域:ReadData.groovy

package testone
import grails.rest.*;
class ReadData {
String customername;
    String fathername;
    static constraints = {
        customername blank: false;
        fathername blank:false;
    }
}

Controller :ReadDataController.groovy
package testone

import grails.converters.JSON

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class ReadDataController {

    static responseFormats = ['json', 'xml']
    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def ReadDataService readDataService;

    def index(Integer max) {

        params.max = Math.min(max ?: 10, 100)
        respond ReadData.list(params), [status: OK]
    }


    @Transactional
    def save(ReadData readDataInstance) {
        if (readDataInstance == null) {
            render status: NOT_FOUND
            return
        }

        readDataInstance.validate()
        if (readDataInstance.hasErrors()) {
            render status: NOT_ACCEPTABLE
            return
        }

        readDataInstance.save flush:true
        respond readDataInstance, [status: CREATED]
    }

    @Transactional
    def update(ReadData readDataInstance) {
        if (readDataInstance == null) {
            render status: NOT_FOUND
            return
        }

        readDataInstance.validate()
        if (readDataInstance.hasErrors()) {
            render status: NOT_ACCEPTABLE
            return
        }

        readDataInstance.save flush:true
        respond readDataInstance, [status: OK]
    }

    @Transactional
    def delete(ReadData readDataInstance) {

        if (readDataInstance == null) {
            render status: NOT_FOUND
            return
        }

        readDataInstance.delete flush:true
        render status: NO_CONTENT
    }
    def readList(){
        readDataService.testList();
    }
    def getName(String name){
        render name;
    }
}

服务:ReadDataService.groovy
package testone

import grails.transaction.Transactional

@Transactional
class ReadDataService {

    def serviceMethod() {

    }
    def testList(){
        return ReadData.list();
    }
}

bootstrap :
import testone.ReadData

class BootStrap {

    def init = { servletContext ->
         new ReadData(customername: 'NameOne',fathername: 'FnameOne').save();
        new ReadData(customername: 'NameTwo',fathername: 'FnameTwo').save();

    }
    def destroy = {
    }
}

请帮助我调用这些函数,或告诉我直接使用服务方法的方式。我想在Android应用中使用Grails服务。

最佳答案

您确定已经为 readList 方法创建了 View 吗?

关于grails - 如何在grails中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27604928/

相关文章:

java - 从 Maven 调用 groovy 'main' 方法

java - groovy + 如何匹配参数中的行

unit-testing - 如何在 Grails 单元测试中使用 Spock 模拟 passwordEncoder

groovy - 从 Groovy 脚本访问 Jenkins 插件包

groovy - 如何使 ThresholdFilter 在 logback 中工作

grails - 如何在Grails中将 Controller 设置为以纯文本形式响应

java - 从 URL 读取图像

grails - 将 Grails 应用程序转换为插件

grails - 在 Grails 中, "property"是保留字吗?

grails - Intellij中的AST浏览器