Skip to content

Instantly share code, notes, and snippets.

@edolganov
Forked from elw00d/example.jsp
Created April 16, 2014 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edolganov/10823518 to your computer and use it in GitHub Desktop.
Save edolganov/10823518 to your computer and use it in GitHub Desktop.
<%@ 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>
<%@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