java - Effective way how to handle ModelMap between Controllers, using forwarding in Spring MVC -
what elegant/effective way, how handle model between controllers in spring mvc 3.2. redirecting controller use forward method, there not necessary new instance of request , model data should accessible (if not wrong). there way how catch model, added in first controller?
(i know redirectattributes, may better/easier method)
example:
@controller public class webpagecontroller{ @requestmapping( value = { "/{code}" } ) public string handlefirstlevel(@pathvariable string code, modelmap modelmap) throws pagenotfoundeception{ final webpage webpage = getwebpage(code); modelmap.put(webpage_model_key, preparemodel(webpage)); return "forward:some-url"; } private map<string, object> preparemodel(webpage webpage){ map<string, object> model = new hashmap<string, object>(); model.put("webpage", webpage); return model; } // other code } @controller public class specialwebpagecontroller{ @requestmapping( value = { "/some-url" } ) public string handlefirstlevel(@pathvariable string code, modelmap modelmap) throws pagenotfoundeception{ // need access appended model add other data return "specialviewname"; } } thank you
when have handler method returns string, string considered view name. prefix of forward, spring requestdispatcher specified path , forward it. part of process include taking model modelandview created request handling cycle , putting attributes httpservletrequest attributes.
the servlet container take requestdispatcher#forward(..) , again use dispatcherservlet handle it. dispatcherservlet create new modelandview new model handling cycle. therefore model doesn't contain of attributes before httpservletrequest attributes do.
in case, this
modelmap.put(webpage_model_key, preparemodel(webpage)); will end being in
httpservletrequest request = ...; request.getattribute(webpage_model_key);
Comments
Post a Comment