spring mvc - How can I get RedirectAttributes in HandlerInterceptor.postHandle() method -


  • background:
    web project uses springmvc framework required front-back seperation reconstruct. frontend going use react, , of backend's controllers used return jsp view need return jsonobject. data passed jsp through modelmap needed transferred json , write through response. , avoid modify every controller, came idea of using interceptor job down. works controllers pass data modelmap, dosen't pass data redirectattributes.

  • problem:
    of controllers return redirect , use redirectattributes pass attributes. since posthandler() has 4 params: request,response,handler , modelandview, can hardly attributes of redirectattributes 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

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -