簡體   English   中英

無法在jsp中讀取JSON文件

[英]Unable to read JSON file in jsp

我知道這個問題之前曾有人提出過,但即使在過去5個小時中回答了所有問題之后,我仍然沒有找到想要的東西。

我正在嘗試使用jstl通過jsp讀取JSON。 我已經在WEB-INF / lib中導入了json-taglib jar文件,但是我正在

在根本沒有原因的org.apache.jasper.JasperException:/index-test.jsp(行:7,列:0)導入的帶有前綴“ json”]的標簽庫中,未定義標簽“ parse”(行:7,列:0)在標簽中未定義標簽“ parse”使用前綴“ json”導入的庫

這是我的代碼

<%@ page contentType="text/html; charset=utf-8" language="java" %>
<%@ taglib tagdir="/WEB-INF/tags/layout" prefix="layout" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>

<c:import var="dataJson" url="http://localhost:10081/test.json"/>
<json:parse json="${dataJson}" var="parsedJSON" />
Fetch the name of the node at index 1 : ${parsedJSON.node[1]}

這是我從中讀取的JSON對象

{
    "firstName": "Joe"
    "lastName": "Bloggs"
}

該應用程序在角度上工作正常,但我想遷移實現,使其讀取json服務器端

請幫忙

我用了它,它為我工作:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.io.*, java.net.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Reading Text File</title>
</head>
<body>
<%
String fileName = "/WEB-INF/json/test.json";
InputStream ins = application.getResourceAsStream(fileName);
try
{
    if(ins == null)
    {
        response.setStatus(response.SC_NOT_FOUND);
    }
    else
    {
        BufferedReader br = new BufferedReader((new InputStreamReader(ins)));
        String data;
        while((data= br.readLine())!= null)
        {
            out.println(data+"<br>");
        }
    }   
}
catch(IOException e)
{
out.println(e.getMessage());
}
%>
</body>
</html>

¿10081端口是否與執行JSP的Web服務器相同? 也許您需要通過使用HTTP過濾器添加相關的HTTP標頭來啟用CORS:

https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM