Controller层中方法写法:
class TestController {
def testService
def index() { }
def main(){//方法名称与views中test文件夹下gsp名称一致时,可直接返回页面
//如url:localhost:8080/test/main,可直接跳转到main.gsp
}
def addTable(){
redirect(action: "addNewTable")//执行addNewTable方法
//或redirect(action: "addNewTable", id: params.id)携带参数。
}
def addNewTable(){
render "success"//返回前端。如果是通过js中post跳转到该方法,则该值返回至post。如果是form,则只在新页面显示"success"
}
def getList(){
def lists = []
def tests = Test.findAll()//获取数据库Test表中的所有数据
for(test in tests){
def list = [name: test.name, id: test.id]
lists << list
}
[testList : lists]//将list传到前端
//或者 render lists as JSON 转为json传到前端进行解析。引入加包:import grails.converters.JSON
}
def getInfo(){
def testInfo = testService.getInfo(params.id)//调用Service层方法,通过前端传入的id获取某个表信息
render testInfo
}
}
import grails.transaction.Transactional
@Transactional
class TestService {
def serviceMethod() {
}
def getInfo(id){
def test = Test.findById(id)
def testInfo
if(!test){
testInfo = [name: test.name, id: test.id]
}
return testInfo
}
}
后续补充……