How to simply return JSON from a JSP

后端 未结 3 1330
情话喂你
情话喂你 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条回答
  • 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" %>
    
        <json:object>
            <json:property name="section" value="${section.name}"/>
            <json:property name="itemCount" value="${fn:length(items)}"/>
            <json:array name="items" var="cArticle" items="${items}">
                <article:use name="cArticle">
                    <json:object>
                    <wf-custom-tags:encodeString 
                        inputString="${article.fields.title.value}" 
                        var="encodedTitle"/>
                    <json:property name="title" value="${encodedTitle}"/>
                    <c:remove var="encodedTitle" scope="page"/>
                    </json:object>
                </article:use>
            </json:array>
        </json:object>
        <c:remove var="items" scope="page"/>
    
    0 讨论(0)
  • 2020-12-08 13:02

    Did you try to invoke the page yourself from a web browser? Is the output what you expected? Also, use Firebug or Chrome Debugger to inspect the response headers/payload and verify that everything is correct.

    Update I think I nailed it - take that damned semi-colon away.

    0 讨论(0)
  • 2020-12-08 13:02

    This is the code:

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
        </head>
        <body>
            <h1>Hello World!</h1>
            <label for="autocomplete">Enter some text here</label>
            <input id="autocomplete" name="autocomplete" />
            <script type="text/javascript">
                $(document).ready(function() {
                    $("#autocomplete").autocomplete({
                        source: 'json.jsp',
                        minLength: 2
    
                    });                
                });
            </script>
        </body>
    </html>
    

    and this is the JSON:

    [
       {"label":"item 1", "value":"item 1", "id": 1},
       {"label":"item 2", "value":"item 2", "id": 2},
       {"label":"item 3", "value":"item 1", "id": 3}
    ]
    
    <%
       // Returns all employees (active and terminated) as json.
       response.setContentType("application/json");
    %>
    
    0 讨论(0)
提交回复
热议问题