java - what's the right way to override Controller Methods behaviour in Roo 2 (RC1)? -
my project roo generated crud working ok, need change way entities saved (for example have "user" attribute want set dynamically user logged)
at moment i'm moving save() method aspect .java , modifying needed. worked far roo console don't seem it, recreate method in aspect change method return type or other things.
i don't need specific answer example, instead i'm looking know if best approach modify/override out-of-the-box create/show functionality entities provided roo.
edit: added example
one of entities "servicio", have "servicioscollectionthymeleafcontroller_roo_thymeleaf.aj" "servicioscollectionthymeleafcontroller.create" method. proceeded push in .aj "servicioscollectionthymeleafcontroller.java"
then made minor changes in create method , saved it. worked, when opened roo console consoled generated pushed aj again, method edited earlier.
original create method on aspect:
/** * todo auto-generated method documentation * * @param servicio * @param result * @param model * @return modelandview */ @postmapping(name = "create") public modelandview servicioscollectionthymeleafcontroller.create(@valid @modelattribute servicio servicio, bindingresult result, model model) { if (result.haserrors()) { populateform(model); return new modelandview("/servicios/create"); } servicio newservicio = getservicioservice().save(servicio); uricomponents showuri = getitemlink().to(serviciositemthymeleaflinkfactory.show).with("servicio", newservicio.getid()).touri(); return new modelandview("redirect:" + showuri.touristring()); }
the same method pushed in .java, , modifications:
/** * todo auto-generated method documentation * * @param servicio * @param result * @param model * @return modelandview */ @postmapping(name = "create") public modelandview create(@valid @modelattribute servicio servicio, bindingresult result, model model, principal principal, pageable pageable) { if (result.haserrors()) { populateform(model); return new modelandview("/servicios/create"); } prestador current = (prestador) personaservice.findbyusername(principal.getname(), pageable).getcontent().get(0); if (current == null) { populateform(model); return new modelandview("/servicios/create"); } servicio.setprestador(current); servicio newservicio = getservicioservice().save(servicio); return new modelandview("redirect:/ver-servicio/" + newservicio.getid()); }
thanks.
roo checks if method included in java file looking method signature (the method's name , parameter types), way supported in java overloading methods.
in case, once change create method parameters, no longer same method signature, , that's why roo generates again.
in general not problem, have change clients of method use new one. example, if add new method service, change implementation of controller use new method, , 1 generated roo not affect you.
in case of controller methods problem related mapping. in case end 2 methods, 1 added , 1 generated roo, same request mapping. solve have add method generated roo without mapping annotation.
in case code following one:
public modelandview create(@valid @modelattribute servicio servicio, bindingresult result, model model) { throw new unsupportedoperationexception(); } @postmapping(name = "create") public modelandview create(@valid @modelattribute servicio servicio, bindingresult result, model model, principal principal, pageable pageable) { if (result.haserrors()) { populateform(model); return new modelandview("/servicios/create"); } prestador current = (prestador) personaservice.findbyusername(principal.getname(), pageable).getcontent().get(0); if (current == null) { populateform(model); return new modelandview("/servicios/create"); } servicio.setprestador(current); servicio newservicio = getservicioservice().save(servicio); return new modelandview("redirect:/ver-servicio/" + newservicio.getid()); }
as have create method default signature, won't regenerated roo. also, doesn't have postmapping annotation, ignored spring mvc call create method new signature.
additional note:
also have change how link method generated. thymeleaf controllers generated roo have companion class (ending linkfactory) used generate links controller methods, avoiding use of hardcoded uris in thymeleaf pages controller redirects. linkfactory classes generated using spring's mvcuricomponentsbuilder.frommethodcall utility uses fake method call generate link controller method.
as have new method signature, have change default implementation of servicioscollectionthymeleafcontroller touri method. push-in touri method java file , change implementation this.
public uricomponents touri(string methodname, object[] parameters, map<string, object> pathvariables) { ... if (methodname.equals(create)) { return springletsmvcuricomponentsbuilder.frommethodcall(springletsmvcuricomponentsbuilder.on(getcontrollerclass()).create(null, null, null, null, null)).buildandexpand(pathvariables); } ... }
note i've added 2 additional null parameters create method call use new method signature. change, uris generated thymeleaf pages point new method.
Comments
Post a Comment