How to pass params into Services that I get from the gsp and I have in my Controller in Grails -
i've got params user enter in g:textfield
in controller when call method save()
, , need use value in services. how can pass params have in controller services or possible pass data directly services gsp??
i'm trying pass params equal gsp-controller when i'm trying access params in services, error: no such property: params
gsp:
<g:textfield name="enteremail" required=""/>
controller:
save(){ def entermail = params.enteremail }
services:
def sendyouremail(emailtemplate emailtemplateinstance){ .... def entermail = params.enteremail log.info "sending email" string source = 'myemail' destination destination = new destination([entermail]) content subject = new content('test') body body = new body().withhtml(new content(output.tostring())) message message = new message(subject, body) amazonwebservice.ses.sendemail(new sendemailrequest(source, destination, message)) }
the params attribute using in controllers dynamic attribute grails added @ runtime. can't use service class.
it's bad idea params service, can using line:
org.springframework.web.context.request.requestcontextholder.currentrequestattributes().params
a idea use command: plain groovy/java object parameters bound request, , send command service. doing that, remove dependency between service class (business layer) , params map (web layer): easier test , reusable other parts (you can create command whatever want).
command:
class mailcommand { string enteremail }
controller:
def mailservice def save(mailcommand mailcommand){ mailservice.sendyouremail(mailcommand, mailtemplate) }
mailservice class:
def sendyouremail(mailcommand mailcommand, emailtemplate emailtemplateinstance){ def entermail = mailcommand.enteremail ... }
more information: http://grails.github.io/grails-doc/latest/guide/single.html#commandobjects
Comments
Post a Comment