【问题标题】:how to call function in grails如何在grails中调用函数
【发布时间】:2014-12-22 14:44:56
【问题描述】:

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

域:ReadData.groovy

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

控制器: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();
    }
}

引导:

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 服务。

【问题讨论】:

  • 您如何称呼您的控制器?您能否包含您正在使用的确切 URL(注意 CapiTaLiZaTiOn)以及您发送的任何 HTTP 标头?
  • 控制器类定义应该是class ReadDataController extends RestfulController {

标签: grails groovy


【解决方案1】:

你确定,你已经为 readList 方法创建了一个视图?

【讨论】:

    【解决方案2】:

    您正在从控制器 readList action 返回一个列表。尝试传递像

    这样的地图
    ['testList':readDataService.testList()]
    

    正如前人所说,检查您是否有观点。否则,如果您想将 JSON 传递给您的 android 应用程序,您应该添加

     render readDataService.testList() as JSON 
    

    在您的 readList 操作结束时。

    【讨论】:

      【解决方案3】:

      你已经设置了允许调用的方法: static allowedMethods = [保存:“POST”,更新:“PUT”,删除:“DELETE”] 基本方法

      • 可以通过帖子保存(例如提交表单)
      • 更新将通过 http PUT 方法提供(在浏览器中不可用)
      • 可通过 DELETE 方法删除(与之前相同)

      所以你应该添加所有你想调用的方法,例如

      allowedMethods = [readList:["GET","POST"],保存:"POST",更新:"PUT",删除:"DELETE"]

      参考:https://grails.github.io/grails-doc/latest/ref/Controllers/allowedMethods.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-18
        • 1970-01-01
        相关资源
        最近更新 更多