Korean Controllers

Last updated by admin 4 years ago

Grails ????

????? ????

????? HTTP ??? ???? HTTP ??? ?????? ??? ??? ???? ???? ?? ???. ?????? ?? HTTP ??? ?? ?? ??, ??? ??? ??? ?? ????. ????? ???? ??? "Controller"? ??? ???? ??? ?? "grails-app/controllers" ????? ???? ???.

???? ??? ? ??? URI? ???? ?????? ??? ??? ??? ???? ??? ??? URI? ???? ?????.

????? HTTP ?? ??? ?????. ??? ? ??? ?? ??? ????? ?????.

Grails ???? ???

????? ?? ???? "create-controller" ??? ???? ????? ??? ????? ?? ???:

grails create-controller
?? ?? ???? ???? "book"? ????? ??? ?? ????? ???? ????:
class BookController { … }
BookController? <...>/book URI? ?????. ? ??? ??? ? Grails? ??? ??? ????? ?? ???? ?? ?????. ??? ????? ? ?? ?? ??????? ???? ??? ??? ? ????.

?? ???

????? ?? ?? ??? ??? ?? ? ???, ??? ??? ???? URI? ?????:

class BookController {
    def list = {

// do controller logic // create model

return model }; }

? ???? list ??? <...>/book/list URI? ?????. ??? ??? ???? ?? ?? ? ??? ?? URI? ?????. ?? URI? ??? ??? ???? ??? ??? "index" ??? ??? ????. ?? ??? <...>/book ?? URI? ??? ???? ?? ??? ?????.

?? ??(default action) ????

?? ??? URI? ???? ??? ???? ?? ?? ??? ???? ?? ??? ???? ?????. ?? ??? ???? ??? ? ??? ????. ?? ??? ??? "index"?? ??? ??? ??? ????:

def index = {
   redirect(action:list)
}
??? ??? ????? "defaultAction" ??? ???? ????:
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()
}
? ??? ??, ? ??? ??? HTTP ?? ??? Book ????? ???? ????. ?? ???? Command Object ? ?? ??? ???? ???? ?????? ???? "bindData" ???? ???? ? ????:
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()
    }
}
? ????? ?? "books"? "authors" ??? ??? ????. ? ??? ??? Spring? ModelAndView ????? ???? ????.

HTTP ?? ??(render)??

?? ?????? HTTP ???? ???? ??? ??? ?? ???? ?? ? ?? ??? ????(Ajax ?????? ?). ??? ??? ?? ??? ??? ???? "render" ???? ??? ? ????:

render "Hello World!"
? ??? "Hello World!"?? ???? HTTP ???? ?????. ??? ?? ?????:
// 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" ???? ???? ??? ????? ???? ?? ??? ?? URI ???? ??? ? ????:
redirect(action:login)
// or
redirect(action:"/another/action")
? ???? ?? ???? ???? ??? ?? ?? params ??? ???? ???:
redirect(action:"/another/action", params:["myparam":"myvalue"])
??? ??? ???? "params" ?? ??? ?? ??? ? ????. "params" ?? ??? HTTP ?? ??? ?? ???? ?? ??? ??? ??? ??? ? HTTP ?? ??? ??? ??? ?? ?? ??? HTTP ?? ??? ??? ??? ??? ????.

??? ?? ?? ???? ???? ?? ????. ???? ???? ??? ???? ???? ??? ?? ???? ?? ? ????. ????, ?? ???? "firstInChain" ??? ????:

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]
The model can be accessed in subsequent controller actions in the chain via the "chainModel" map. This dynamic property only exists in actions following the call to "chain":
class ChainController {
    def nextInChain = {
        def model = chainModel["myModel"]
        .....
    };
}
"redirect" ?????? ????? ??? ??? ??? ?? ?? ????:
chain(action:"/another/action", model:["step1":object1], params:["myparam":"param1"])

?? ????(interceptor)

??

??? HTTP ?? ?? ??, ?????? ??? ??? ? ????(intercept)? ??? ??? ????. ????? ?? ????? ??? ? ????. ?????? before? after?? ? ?? ??? ????.

Before ????

'before' ????? ??? ?? ?? ?????. ????? 'false'? ???? ? ??? ??? ????? ??? ???? ????. ????? ?? ??? ?? ??? ?? ??? ? ????:

def beforeInterceptor = {
       println 'Tracing action ${actionUri}'
}
? ??? ?? ??? ???? ?? ?????. before ????? ??? ?? ?? ??? ??? ? ?? ?????:
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
}
? ??? auth ?? ???? ?????. 'beforeInterceptor'? 'login' ??? ??(except)? ?? ??? ?? "auth"? ?? ????? ???? ????. ? ? 'auth' ???? Groovy? ??? ??? ??? ???? ???? ????. auth ?????? ?? ??? ??? ??? ??? ???? ?? ?? login ???? ????? ??? ? false? ???? ?? ????? ?? ??? ???? ??? ?? ????.

After ????

??? ??? ??? ????? ? ????? 'afterInterceptor' ???? ?????:

def afterInterceptor = { model ->
       println 'Tracing action ${actionUri}'
}
after ????? ??? ?? ??? ???? ??? ??? ????. ??? ?? ?? HTTP ??? ?? ???? ? ? ????.

???? ??

Rails ????? ? ?? ??? ??? ???, ????? ??? ??? 'except' ??? ??? ????? ?? ?? ?? ????. ????? Rails?? '??'?? ??? ?????. ??? ? ??? ??? ??? ??? ???? ????? Grails??? ?????? ??? ?????:

def beforeInterceptor = [action:this.&auth,except:'login']
? ??? 'login' ??? ??? ?? ??? ???? ?? auth ???? ????? ???? ????. ????? ??? ?? ??? ??? ?? List? ?? ?? ????:
def beforeInterceptor = [action:this.&auth,except:['login','register']]
'except' ?? ? ?? ????? 'only'? ????. 'only'? ????? ??? ??? ???? ????? ?????:
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>

? ?? ???? ??? ????? ????. ??? ??? Spring? MultipartFile ? ???? ????. ? ??? Spring ????? ?? ?????:

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')
}
}

??? ?? ??? ??? ????? ??, ? ??? ?? ?????(????? InputStream? ? ?? ????. ??? ??? Javadoc ??? ?????). ??? ??? ??? ??? ???? ??? ??? ??? ??? ?? ?? ??(command bean)? ??? ?? ????. "Image"?? ??? ??? ???? ??? ???? ?? ??? ?????:

class Image {
def byte[] myFile
}

?? ??? ?? ??? ??? ??? ?????:

def img = new Image()
img.properties = params

??? ???? ???? byte (+) ? ???? Image ??? ??? ????? ??? ?????. Image ??? ???? myFile ??? byte (+) ?? String?? ???? ??? ???? String?? ?????.:

class Image {
def String myFile
}

??? ???? ??? ??? ?? ??(command ojbect0? ?? ??? ? ?? 'bindData' ???? ???? ????. ??? ?? ?? ??? ??? ?????:

class UploadCommand {
def byte[] myFile
}

?? ?????? ??? ?? ? ? ????: :

def upload = {
def uc = new UploadCommand()
bindData(uc, params)
assert uc.myFile != null
}

???? ????? ????

????? ???? ??? ?? ?? ???? HTTP ??? ??? ? ????. ??? ????? ????? ??(inject)??? ??? ?? ????? ??? ????? ???:

def CountryService countryService