Created
April 16, 2014 07:16
-
-
Save elw00d/10822554 to your computer and use it in GitHub Desktop.
Model to Json serializer JSP tag (by Eugene Dolganov)
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
<%@ taglib tagdir="/WEB-INF/tags/util" prefix="util" %> | |
<%-- tag should be placed to Web-inf/tags/util/toJson.tag --%> | |
<script id="scheduleData" type="json-data"><util:toJson source="${schedule}"/></script> |
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
<%@tag import="org.slf4j.LoggerFactory"%> | |
<%@tag import="org.codehaus.jackson.map.ObjectMapper"%> | |
<%@ tag trimDirectiveWhitespaces="true" display-name="Конвертирование объекта в JSON" pageEncoding="UTF-8" language="java" %> | |
<%@ attribute name="source" required="true" type="java.lang.Object"%> | |
<%@ attribute name="catchException" type="java.lang.Boolean"%> | |
<% | |
String result = null; | |
try { | |
ObjectMapper mapper = new ObjectMapper(); | |
result = mapper.writeValueAsString(source); | |
}catch(Exception e){ | |
if( ! Boolean.FALSE.equals(catchException)){ | |
LoggerFactory.getLogger("toJson.tag").error("can't convert to json", e); | |
} else { | |
throw e; | |
} | |
} | |
if(result != null){ | |
jspContext.getOut().append(result); | |
} | |
%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep, it's my idea =)