Last updated by admin 4 years ago
Grails ????
????? ????
????? HTTP ??? ???? HTTP ??? ?????? ??? ??? ???? ???? ?? ???. ?????? ?? HTTP ??? ?? ?? ??, ??? ??? ??? ?? ????. ????? ???? ??? "Controller"? ??? ???? ??? ?? "grails-app/controllers" ????? ???? ???.???? ??? ? ??? URI? ???? ?????? ??? ??? ??? ???? ??? ??? URI? ???? ?????.????? HTTP ?? ??? ?????. ??? ? ??? ?? ??? ????? ?????.Grails ???? ???
????? ?? ???? "create-controller" ??? ???? ????? ??? ????? ?? ???:grails create-controller
class BookController { … }?? ???
????? ?? ?? ??? ??? ?? ? ???, ??? ??? ???? URI? ?????:class BookController {
def list = { // do controller logic
// create model return model
};
}?? ??(default action) ????
?? ??? URI? ???? ??? ???? ?? ?? ??? ???? ?? ??? ???? ?????. ?? ??? ???? ??? ? ??? ????. ?? ??? ??? "index"?? ??? ??? ??? ????:def index = {
redirect(action:list)
}def defaultAction = "list"HTTP ?? ??, ?? ?? ????
?? ?????? ???? ???? ? ?? ???? ??(inject)???. ??? ???? ?? HTTP ??, ?? ?? ??? ? ????. ??? ???? ?? ????? ???? ?? ??? ???? ? ?????.class BookController {
def find = {
def findBy = params["findBy"]
def appContext = servletContext["appContext"]
def loggedUser = session["logged_user"] // do stuff
// return model
return model
};
}??? ???(Flash Scope) ????
Rails? ?? ?? ??? ??? ????? ???, ?? HTTP ????? ????? ?? ???? ??? ??? ? ?? ?? ?? ?????. ??? ??? ????(redirection) ??? ???? ???? ?? ?? ?? ?????:def delete = {
def b = Book.get( params['id'] )
if(!b) {
flash['message'] = "User not found for id ${params['id']}"
redirect(action:list)
}
… // remaining code
}HTTP ?? ???? ??? ??(bind)
HTTP ?? ??? ? ??????? ???? ???? ??? ??? ? ???? ??? ??? ????? ??? ?????. Grails ??? ?????? "properties"?? ??? ?? ? ??? ???? ? ????:def save = {
def b = new Book()
b.properties = params
b.save()
}def sc = new SaveCommand()
bindData(sc, params)?? ????
??? ?? ??? ??(render)? ? ??? ??? ?? ?? Map ???. ??? ???? ??? ????? ????. ??? ??? Map ????? ????? ???? ????:def show = {
def b = Book.get( params['id'] )
return [ book : b ]
}class BookController {
def List books
def List authors
def list = {
books = Book.list()
authors = Author.list()
}
}HTTP ?? ??(render)??
?? ?????? HTTP ???? ???? ??? ??? ?? ???? ?? ? ?? ??? ????(Ajax ?????? ?). ??? ??? ?? ??? ??? ???? "render" ???? ??? ? ????:render "Hello World!"// write some markup
render {
for(b in books) {
div(id:b.id, b.title)
}
}
// render a specific view
render(view:'show')
// render a template for each item in a collection
render(template:'book_template', collection:Book.list())
// render some text with encoding and content type
render(text:"<xml>some xml</xml>",contentType:"text/xml",encoding:"UTF-8")?? ????(redirection) ? ???(chaining)
?? ??? ????? "redirect" ???? ???? ????? ? ????:class OverviewController {
def login = {} def find = {
if(!session["logged_user"])
redirect(action:login)
.....
};
}redirect(action:login)
// or
redirect(action:"/another/action")redirect(action:"/another/action", params:["myparam":"myvalue"])
class ChainController {
def firstInChain = {
chain(action:secondInChain,model:["step1":new Object()])
}
def secondInChain = {
chain(action:thirdInChain,model:["step2":new Object()])
};
def thirdInChain= {
return ["step3":new Object()])
};
}["step1":object1, "step2":object2, "step3":object3]
class ChainController {
def nextInChain = {
def model = chainModel["myModel"]
.....
};
}chain(action:"/another/action", model:["step1":object1], params:["myparam":"param1"])
?? ????(interceptor)
??
??? HTTP ?? ?? ??, ?????? ??? ??? ? ????(intercept)? ??? ??? ????. ????? ?? ????? ??? ? ????. ?????? before? after?? ? ?? ??? ????.Before ????
'before' ????? ??? ?? ?? ?????. ????? 'false'? ???? ? ??? ??? ????? ??? ???? ????. ????? ?? ??? ?? ??? ?? ??? ? ????:def beforeInterceptor = {
println 'Tracing action ${actionUri}'
}def beforeInterceptor = [action:this.&auth,except:'login'] // defined as a regular method so its private def auth() { if(!session.user) { redirect(action:'login') return false } } def login = { // display login page }
After ????
??? ??? ??? ????? ? ????? 'afterInterceptor' ???? ?????:def afterInterceptor = { model ->
println 'Tracing action ${actionUri}'
}???? ??
Rails ????? ? ?? ??? ??? ???, ????? ??? ??? 'except' ??? ??? ????? ?? ?? ?? ????. ????? Rails?? '??'?? ??? ?????. ??? ? ??? ??? ??? ??? ???? ????? Grails??? ?????? ??? ?????:def beforeInterceptor = [action:this.&auth,except:'login']def beforeInterceptor = [action:this.&auth,except:['login','register']]def beforeInterceptor = [action:this.&auth,only:['secure']]?? ??? ????
??? ??? ??? ?? ???? ?? ???? ???:Upload Form: <br /> <g:form action="upload" method="post" enctype="multipart/form-data"> <input type="file" name="myFile" /> <input type="submit" /> </g:form>
def upload = {
def f = request.getFile('myFile')
if(!f.empty) {
f.transferTo( new File('someotherloc') )
}
else {
flash.message = 'file cannot be empty'
redirect(action:'uploadForm')
}
}class Image {
def byte[] myFile
}def img = new Image()
img.properties = paramsclass Image {
def String myFile
}class UploadCommand {
def byte[] myFile
}def upload = {
def uc = new UploadCommand()
bindData(uc, params)
assert uc.myFile != null
}???? ????? ????
????? ???? ??? ?? ?? ???? HTTP ??? ??? ? ????. ??? ????? ????? ??(inject)??? ??? ?? ????? ??? ????? ???:def CountryService countryService



