简体   繁体   English

重定向到JSP而不是来自REST调用的JSON响应

[英]Redirecting to a JSP instead of the JSON response from a REST call

I am working on an assignment to create a simple blog, where a user can ask questions(posts) and answers can be given as comments. 我正在做一个作业,以创建一个简单的博客,用户可以在其中提出问题(帖子),并可以将评论作为评论给出。 With my limited knowledge on Java, REST and Jquery, I have managed to fetch the list of posts and am displaying them as a table. 凭借对Java,REST和Jquery的有限了解,我设法获取了帖子列表并将其显示为表格。 Now whenever a user clicks on any post, he should be redirected to another page where the corresponding comments of the question can be displayed. 现在,每当用户单击任何帖子时,都应将其重定向到另一个页面,在该页面上可以显示问题的相应注释。 I have implemented a REST method in Java which returns the JSON response with the comments relevant to the post_id passed. 我已经在Java中实现了REST方法,该方法返回JSON响应以及与传递的post_id相关的注释。 So whenever a user clicks on any post, he is redirected to a REST URL(.../services/comments?postID=1) and gets following response: 因此,每当用户单击任何帖子时,他都会被重定向到REST URL(.../services/comments?postID=1)并得到以下响应:

[
  {
    "postID": 1,
    "commentID": 8,
    "comment": "These are the answers getting added",
    "commenterID": 7,
    "commentDate": 1442671662000,
    "commentVote": 0
  }
]

Here is my JAVA method serving the REST call: 这是我的JAVA方法,用于REST调用:

    @GET
    @Produces({ MediaType.APPLICATION_JSON })
    public List<Comments> getUserComments(@QueryParam("postID") Integer postID) {
    Session ses = HibernateUtil.currentSession();
    ses.flush();
    List<Comments> comments = null;
    if(postID != null)
    {
        comments =  ses.createQuery("select c from Comments c where c.postID="+postID+" order by c.commentDate desc").list();
    }

    HibernateUtil.closeSession();
    return comments;
    }

What is the "simplest" way to simultaneously redirect to a JSP where I can parse this JSON and display it? 同时重定向到JSP并在其中解析此JSON并显示它的“最简单”方法是什么?

You don't need to redirect the page to jsp. 您无需将页面重定向到jsp。

What you need is a general template (jsp) of the page to display the post and its answers from the json. 您需要的是页面的通用模板(jsp),以显示json中的帖子及其答案。 This page should make an ajax call to get the json for corresponding post (and its answers) and render it in the page. 此页面应进行ajax调用,以获取对应帖子(及其答案)的json并将其呈现在页面中。

Now the problem becomes how to do you pass the post id to this page. 现在,问题就变成了如何将帖子ID传递到此页面。
You could do this by setting a non-html tag like this. 您可以通过这样设置非html标签来实现。 <post_id id="post_id" value=1> . <post_id id="post_id" value=1> After the page is loaded the jquery will this tag (and extract the value attribute) to form the url for ajax call. 页面加载后,jquery将使用此标记(并提取value属性)来形成ajax调用的url。


Example

Lets assume you have a page to display the list of all posts. 假设您有一个页面来显示所有帖子的列表。 This page will look like 该页面看起来像

<a href="post.jsp?post_id=1"> post1</a>
<br>
<a href="post.jsp?post_id=2"> post2</a>

Another jsp page to display the post and its comments. 另一个jsp页面显示帖子及其评论。 Lets call this post.jsp . 让我们称之为post.jsp This page should get a parameter post_id in the url. 该页面应在网址中获取参数post_id This page will set the tag <post_id> and use an ajax request to load the corresponding comments from REST url .../services/comments?postID="+post_id . 该页面将设置标签<post_id>并使用ajax请求从REST url .../services/comments?postID="+post_id加载相应的注释。

<body>
<script type="text/javascript">
$(document).ready(
    function () {
        //extract the post_id from tag value
        var post_id = $("post_id").attr("value");
        //form the rest url using post_id
        var post_url = ".../services/comments?postID="+post_id;
        $.ajax(
            {
                url: post_url,
                success: function(data, status, jqXHR, json) {
                    json_data = JSON.parse(data);
                    html="";
                    for(var i=0; i<json_data.length; i++) {
                        //Comments rendering logic
                        html+= "<h5>"+json_data[i].comment +"</h5>";
                    }
                    $("#container").html(html);
                }
            }
        );
    }
);
</script>
<post_id value="<%= request.getParameter("post_id") %>" > </post_id>
<div id="container">
</div>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM