Skip to content

Instantly share code, notes, and snippets.

@blvp
Created July 9, 2015 15:39
Show Gist options
  • Select an option

  • Save blvp/5f494a738809f7e987c7 to your computer and use it in GitHub Desktop.

Select an option

Save blvp/5f494a738809f7e987c7 to your computer and use it in GitHub Desktop.
public class JsDataInjectionInterceptor extends HandlerInterceptorAdapter {
@Override
public void postHandle( HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView ) throws Exception {
//
if (null == modelAndView) return;
Map<String, Object> jsObjects = null;
for ( Map.Entry<String, Object> entry : modelAndView.getModelMap().entrySet() ) {
if (entry.getKey().startsWith( "js_" )) {
if (null == jsObjects)
jsObjects = new HashMap<>( );
jsObjects.put( entry.getKey(), entry.getValue() );
}
}
if (jsObjects != null) {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationConfig.Feature.INDENT_OUTPUT);
String jsData = mapper.writeValueAsString( jsObjects );
modelAndView.getModelMap().addAttribute( "__js_data__",
jsData
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment