Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
js-data in JSP
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="su.elwood"/>
<mvc:annotation-driven />
<mvc:interceptors>
<bean class="su.elwood.JsDataInjectionInterceptor"/>
</mvc:interceptors>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" session="false" %>
<%--@elvariable id="__js_data__" type="java.lang.String"--%>
<c:if test="${not empty __js_data__}">
<script type="text/javascript">
var JS_DATA = ${__js_data__};
</script>
</c:if>
package su.elwood;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
* @author igor.kostromin
* 14.04.2014 12:21
*/
public class JsDataInjectionInterceptor extends HandlerInterceptorAdapter
{
private final static Log log = LogFactory.getLog( JsDataInjectionInterceptor.class );
@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( SerializationFeature.INDENT_OUTPUT );
String jsData = mapper.writeValueAsString( jsObjects );
log.debug( "JsData: " + jsData );
modelAndView.getModelMap().addAttribute( "__js_data__",
jsData
);
}
}
}
...
<%@include file="js-data.jsp"%>
...
<script type="text/javascript">
$(function() {
var articleId = JS_DATA['js_article_id'];
// ...
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment