【问题标题】:Json array don't print value in jspJson数组不在jsp中打印值
【发布时间】:2018-12-15 01:32:25
【问题描述】:

我正在 jsp 中进行动态搜索。 它从映射为 filter.json 的 Filter.java 开始。使用两种不同的方法,我正确地将 SQL 搜索结果保存在 2 ArrayList 上。 这是Filter.java

class Filter() {
    ...
    ArrayList<News> newsListCat = new ArrayList<>();
    ArrayList<News> newsListAut = new ArrayList<>();
    NewsFactory newsFactory = NewsFactory.getInstance();
    String str = request.getParameter("q");

    if (str.equals("search")) {

        String toSearch = request.getParameter("toSearch");

        if (toSearch == null) {
            newsListCat = newsFactory.getNews();
        } else {
            newsListCat = newsFactory.searchNewsbyCat(toSearch);
            newsListAut = newsFactory.searchNewsbyAut(toSearch);
            //System.out.println(newsListAut);

        }
    }

    request.setAttribute("newsListCat", newsListCat);
    request.setAttribute("newsListAut", newsListAut);

    response.setContentType("application/json");
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");

    request.getRequestDispatcher("data.jsp").forward(request, response);
    ...
}

它在管理json数组的data.jsp中发送请求

<json:array>
<c:forEach var="news" items="${newsListAut}">
    <json:object>
        <json:property name="type" value="author"/>
        <json:property name="newsID" value="${news.getID()}"/>
        <json:property name="authorID" value="${news.getAuthor().getID()}"/>
        <json:property name="name" value="${news.getAuthor().getName()}"/>
        <json:property name="surname" value="${news.getAuthor().getSurname()}"/>
    </json:object>
</c:forEach>
<c:forEach var="news" items="${newsListCat}">
    <json:object>
        <json:property name="type" value="category"/>
        <json:property name="authorID" value="${news.getAuthor().getID()}"/>
        <json:property name="name" value="${news.getAuthor().getName()}"/>
        <json:property name="surname" value="${news.getAuthor().getSurname()}"/>
    </json:object>
</c:forEach>

最后 search.js 应该运行请求,动态创建一个带有查询结果的无序列表。 请求的响应是正确的,但是jsp页面没有做任何事情。

搜索.js

    function stateSuccess(data) {

        var ResultCat = $("newsListCat");
        $(ResultCat).empty();

        var ResultAut = $("newsListAut");
        $(ResultAut).empty();

        for (var instance in data) {
            if (data[instance].type === "category") {
                $(ResultCat).append("<li><a href='notizie.html?cat=" + data[instance].category + "'>" + data[instance].category + "</a></li>");
            } else if (data[instance].type === "author")
                $(ResultAut).append("<a href='profilo.html?id=" + data[instance].authorID + "}'><li>" + data[instance].name + " " + data[instance].surname + "</li></a>");
        }
    }

    function stateFailure(data, state) {
        console.log(state);
    }

    $(document).ready(function () {
        $("#search").keyup(function (event) {
            //$("input").css("background-color", "rgba(255, 0, 0, 0.8)");
            $.ajax({
                url: "filter.json",
                data: {
                    q: "search",
                    toSearch: event.target.value
                },
                dataType: 'json',

                success: function (data, state) {
                    stateSuccess(data);
                },
                error: function (data, state) {
                    stateFailure(data, state);
                }
            });

        });
    });

我做错了吗?我想是的,但不知道是什么。 This is how the search request result

【问题讨论】:

    标签: java json jsp


    【解决方案1】:

    当您单击“搜索”时,jsp 页面已加载并且为空。

    点击搜索时,ajax调用为你获取数据,但是jsp页面没有加载,还是空的。

    使用你的 stateSuccess 函数将数据放入 jsp 页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-17
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      相关资源
      最近更新 更多