java - Spring Boot object between controller in different class -


i trying object sent spring-boot controller in 1 class available in model of controller.

it seems @sessionattributes objects global (like logged on user).

i have tried on controller 1

@component @controller @sessionattributes(value = { "user"}) public class formcontroller {      @getmapping("/occurence/{occno}") public string findocc(@pathvariable string occno, occviewmodel occviewmodel, model model) {      occ occ = occrepository.findbyoccno(occno);     occviewmodel.setocc(occ);     occviewmodel.setpersons(occpersonrepository.findoccpersonbyocc(occ.getid()));     list<occperson.occrole> occroles = arrays.aslist(occperson.occrole.values());     model.addattribute("occroles", occroles);     model.addattribute("occviewmodel", occviewmodel);     model.addattribute("countries", countries);     return "occ"; } 

i have button on form sends user endpoint - same occviewmodel available endpoint on controller 2

@component @controller @sessionattributes(value = { "user" }) public class plancontroller {  @getmapping("/newplan") public string newplan(model model, occviewmodel occviewmodel, httpsession session) {     // create planviewmodel dto     occ occ = new occviemodel.getocc();     planviewmodel planviewmodel = new planviewmodel;     planviewmodel.setocc(occ);     model.addattribute(planviewmodel);     //etc } 

i see there @sessionattributes not understand in first controller how load session if dont know occ repo appears need @modelattribute prior handler - uri gives occno?

i looked here appeared deal same class , wasn't clear how apply id passed in on uri.

i think have working correctly retrieve object database, put in session , send via view. view later post data handler , object coming view hydrated against object in session (before being passed hibernate).

there post , controller occviewmodel. @sessionattributes means between , post object held in session , not need pass object id in hidden tags.

@component @controller @sessionattributes(value = { "user", "occviewmodel" }) public class formcontroller {  @postmapping("newocc") public string occsubmit(@valid @modelattribute("occviewmodel") occviewmodel occviewmodel, bindingresult result, model model httpservletrequest request, sessionstatus status) {         // new occ         if( occviewmodel.getocc().getid() == null ) {         occservice.savenewocc(occviewmodel.getocc(), occviewmodel.getpersons());         }         //update occ         if( occviewmodel.getocc().getid() != null ) {             occservice.updateocc(occviewmodel.getocc(), occviewmodel.getpersons());         }     }     status.setcomplete();     return "redirect:/dashboard"; }  @getmapping("/occ/{occno}") public string findocc(@pathvariable string occno, occviewmodel occviewmodel, model model) {      // populate occ view model     occ occ = occrepository.findbyoccno(occno);     occviewmodel.setocc(occ);     model.addattribute("occviewmodel", occviewmodel);     return "occ"; } 

i believe occviewmodel must passed parameter post , handlers.

in terms of passing object handler in controller done sake of keeping project tidy:

@component @controller @sessionattributes(value = { "user", "occplanwork" }) public class plancontroller {      @getmapping("/occ/{occno}/plan") public string findplan(@pathvariable string occno, occplanwork occplanwork, model model) {      // occ occno     occ occ = new occ();     occ = occrepository.findbyoccno(occno);      // set ept data     occplanwork.setoccno(occ.getoccno());     occplanwork.setstart_date(occ.getstart_date());     occplanwork.setoccid(occ.getid());      // create new plan if blank     if (occ.getplan()== null) {         plan plan = new plan();         occplanwork.setplan(plan);     }     if (occ.getplan()!=null) {         plan plan = new plan();         plan = occ.getplan();         occplanwork.setplan(plan);     }      // set existing works (and convert set<work> list<list> th compatability)     if (occplanwork.getplan().getid()!=null) {         list<work> works = new arraylist<>(occplanwork.getplan().getworks());         collections.sort(works);         occplanwork.setworks(works);     } // create new work list if plan new     if (occplanwork.getplan().getid()==null) {         list works = new arraylist();         occplanwork.setworks(works);     }      // return ept     model.addattribute("occplanwork", occplanwork);     return "newplan"; } 

that achieved passing occurrence number in uri other controller. again object backing view (occplanwork) in session attributes on controller level.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -