spring mvc - @SessionAttributes and @ResponseBody do not work together -



spring mvc - @SessionAttributes and @ResponseBody do not work together -

i have created simple "test" controller keeps counter in session print how many times request handler called.

@controller @sessionattributes("counter") public class mycontroller { @modelattribute("counter") public counter addcounter(){ system.out.println("counter added model"); homecoming new counter(); } @requestmapping("printcounter") @responsebody public string printcounter(model model){ counter counter = (counter)model.asmap().get("counter"); int currval = counter.getvalue(); system.out.println("current value: " + currval); counter.increment(); homecoming "hello-view"; } }

every thing works fine, when seek utilize @responsebody on response fails next error:

java.lang.illegalstateexception: cannot create session after response has been committed

i have found several work around. know error happens because spring has flushed response when trying add together counter session. not know why spring doing so.

since issue has been there long imagine done on propose.

does body know motivation implementing way?

why can store info in session if method returning body content can if returns view name?

thanks in advance.

@sessionattribute not best designed concept in springframework (see other post caveat).

when forwards view, spring has time deal session attributes before calling view : nil has been written request @ time controller method returns. when utilize @responsebody, homecoming value of method straight written response , response committed. may late spring create session save model attributes. ok, have been designed in way not necessary. as can inquire eagerly creating session giving httpsession parameter controller method, suppose spring fellows think enough.

spring-mvc

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -