Created
July 9, 2015 15:39
-
-
Save blvp/5f494a738809f7e987c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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