java - Spring MVC session Attribute set intially -


i have used spring mvc. set session attribute value

@requestmapping(value = "/home", method = requestmethod.get)         public string inithome(model model) {             if (!model.containsattribute("clientobject")) {                 model.addattribute("clientobject", createdefaultclient());             }             return "homemenu";         } 

it working fine if click home menu url(/home). if did not go home means says error 'session attribute clientobject required'

so decided set sessionattibutes in constructor of controller

 @autowired     public mycontroller(model model) {        if (!model.containsattribute("clientobject")) {             model.addattribute("clientobject", createdefaultclient());         }     } 

it says error

org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean name 'mycontroller'

i tried set using requestmapping like

 @requestmapping(value = "/", method = requestmethod.get)     public void initcontroller(model model) {        if (!model.containsattribute("clientobject")) {                     model.addattribute("clientobject", createdefaultclient());                 }      } 

this method not called intially cointroller like

@requestmapping("/sample") public class mycontroller {     ..     .. 

is possible set sessionattribute value in constructor of controller? or other way set session attribute initially?

thanks in advance help.

assuming createdefaultclient in controller add @modelattribute annotation it.

@modelattribute("clientobject") public clientobject createdefaultclient() { ... } 

this method called before request handling method (as explained in reference guide)

if combine @sessionattribute annotation on class (which might have). should able achieve want.

in request handling methods (methods annotated @requestmapping) can inject client object method argument.

@requestmapping(value = "/home", method = requestmethod.get) public string inithome(@modelattribute("clientobject") clientobject clientobject) {     // clientobject     return "homemenu"; } 

this work consistenly within same controller, if need clientobject used somewhere else (another controller instance), isn't going work (nor @sessionattributes designed that).


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -