How to simply return JSON from a JSP

后端 未结 3 1348
情话喂你
情话喂你 2020-12-08 12:26

Can anyone give me an example of how to return the following json simply from a jsp without any external libraries (except the ones that come standard with Oracle Java)?

3条回答
  •  猫巷女王i
    2020-12-08 12:57

    From a JSP file, simplest way to create JSON output is using "json-taglib" library.

    http://json-taglib.sourceforge.net/

    All you'll have to do is:

    1) Include the library jar file. You can either include it by downloading the jar file directly or by adding the pom dependency. Maven repo of this taglib can be found here; http://maven.nuxeo.org/nexus/content/repositories/public/atg/taglib/json/json-taglib/0.4.1/

    2) Add following line in the taglib definition

    3)Make sure the page output content type is json

    4) Then just use the taglib

    Here is a sample code

    <%@page language="java" contentType="application/json;charset=UTF-8" %>
    
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    <%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
    
        
            
            
            
                
                    
                    
                    
                    
                    
                
            
        
        
    

提交回复
热议问题