How to change the posted values with a spring mvc interceptor -
does know how change posted values spring mvc interceptor ? have seen examples none subject. know how them don't know how modify them.
@component public class cultureinterceptor implements handlerinterceptor { @override public void aftercompletion(httpservletrequest arg0, httpservletresponse arg1, object arg2, exception arg3) throws exception { } @override public void posthandle(httpservletrequest arg0, httpservletresponse arg1, object arg2, modelandview arg3) throws exception { } @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object arg2) throws exception { // posted values string culture = request.getparameter("culture"); string = request.getparameter("a"); string b = request.getparameter("b"); system.out.println(string.format("[cultureinterceptor culture=%s, a=%s, b=%s]", culture, a, b)); if (culture != null && != null && b != null && "fr-fr".equals(culture)) { = a.replace(",", "."); b = b.replace(",", "."); } system.out.println(string.format("[cultureinterceptor culture=%s, a=%s, b=%s]", culture, a, b)); return true; }
above, have created copy of posted values [a] , [b] haven't modified them in request. idea ?
i answer own question. in fact rather complex , took me time find working solution. first, created filter in spring configuration class (spring boot environment exactly) :
@configuration @componentscan({ "istia.st.springmvc.config", "istia.st.springmvc.controllers", "istia.st.springmvc.models" }) @enableautoconfiguration public class config extends webmvcconfigureradapter { @bean public filter culturefilter() { return new culturefilter(); } }
here declare filter (by default) filter every request before attains final handler. created filter :
public class culturefilter extends onceperrequestfilter { @override protected void dofilterinternal(httpservletrequest request, httpservletresponse response, filterchain filterchain) throws servletexception, ioexception { // next handler filterchain.dofilter(new culturerequestwrapper(request), response); } }
[onceperrequestfilter] spring class. trick replace actual request new 1 [culturerequestwrapper(request)]. created culturerequestwrapper :
public class culturerequestwrapper extends httpservletrequestwrapper { public culturerequestwrapper(httpservletrequest request) { super(request); } @override public string[] getparametervalues(string name) { // posted values et b if (name != null && (name.equals("a") || name.equals("b"))) { string[] values = super.getparametervalues(name); string[] newvalues = values.clone(); newvalues[0] = newvalues[0].replace(",", "."); return newvalues; } // other cases return super.getparametervalues(name); } }
i redefined [getparametervalues] of [httpservletrequest] depends on final servlet manage request. have redefine [httpservletrequest] methods used servlet.
Comments
Post a Comment