我是初學者。所以請耐心等待。我有一個域類和一個控制器和一個服務。我創造了兩種方法;一個獲取列表,另一個獲取參數並返回參數以檢查它是否接受值。但我不知道如何調用該函數。因爲我輸入localhost:8080/projectname/controllername/methodname。它返回404視圖找不到。 下面是代碼 包是testone如何在grails中調用函數
域: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頭文件嗎? –
http:// localhost:8080/TestOne/readData/readList –
控制器類的定義應該是'class ReadDataController extends RestfulController {' – SeattleStephens