java - In Spring MVC, how can RequestMapping be specified to depend on session attribute? -
in response client requests, write 2 different methods, can't seem able that... both methods mapped same request pattern, difference between 2 methods, 1 called when attribute in session, , other when attribute not in session.
in first attempt wrote single method handled both cases:
@requestmapping(<some pattern>) public modelandview loaddata( @sessionattribute("data") data data, @requestparam("action") string action) { if (data == null) { data data = new data(); session.setattribute("data", data); } //additional processing using data return ... }
but, when request did not have attribute data, spring failed map above method, , documented way...
so in second attempt created 2 different methods same request mapping, different signature:
//i want method called when data in not in session. //this methods creates data object , sets session @requestmapping(<some pattern>) public modelandview loaddatanew( httpsession session, @requestparam("action") string action) { data data = new data(); session.setattribute("data", data); return loaddata(data, action); } //i want method called when data in session. //this methods maps data object session parameter data @requestmapping(<some pattern>) public modelandview loaddatacontinue( @sessionattribute("data") data data, @requestparam("action") string action) { return loaddata(data, action); } private modelandview loaddata( data data, string action) { //additional processing return ... }
but problematic spring , complains: "ambiguous mapping", because both have same request mapping.
is bug in spring? have expected know how map right method, based on method parameters (since in first case data argument null not allowed, spring should know use second matching method)...
is there way have spring map session method parameter if exists? can have have single method signature obtain or sets data, in:
@requestmapping(<some pattern>) public modelandview loaddatanew( httpsession session, @requestparam("action") string action) { data data = session.getattibute("data"); if (data == null) { data = new data(); session.setattribute("data", data); } //additional processing using data return ... }
but hoping more elegant solution using spring's mapping... (i can potentially introduce new request param, , mapping based on (or existence) that's not elegant)
Comments
Post a Comment