spring mvc - How can I get RedirectAttributes in HandlerInterceptor.postHandle() method -
background:
web project usesspringmvc
framework required front-back seperation reconstruct. frontend going usereact
, , of backend'scontrollers
used returnjsp
view need returnjsonobject
. data passedjsp
throughmodelmap
needed transferredjson
, write throughresponse
. , avoid modify everycontroller
, came idea of usinginterceptor
job down. workscontrollers
pass datamodelmap
, dosen't pass dataredirectattributes
.problem:
of controllers returnredirect
, useredirectattributes
pass attributes. sinceposthandler()
has 4 params:request,response,handler
,modelandview
, can hardly attributes ofredirectattributes
these.
controller code:
@requestmapping(value="save") public string save(carinfoentity entity, httpservletrequest request, httpservletresponse response, redirectattributes redirectattributes, model model){ redirectattributes.addflashattribute("message", "success!"); return "redirect:/demo/carinfo/list"; }
interceptor code:
@override public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception { if(modelandview != null) { map<string, object> map = modelandview.getmodel(); string json = json.tojsonstring(map, serializerfeature.disablecircularreferencedetect); logger.debug(json); if (null != json) { try { response.setheader("content-type", "application/json;charset=utf-8"); response.setcharacterencoding("utf-8"); printwriter pw = response.getwriter(); pw.write(json); pw.flush(); pw.close(); } catch (ioexception e) { } } modelandview.clear(); } }
question 1: can redirectattributes
in interceptors?
question 2: how, if can?
i tried use aop
, perfect solved problem. found aop
more powerful intercepter
. can define cut points
more flexibly , arguments getargs()
method of proceedingjoinpoint
.
Comments
Post a Comment