Creating a json object in jsp and using it with JQ

2019-01-12 04:44发布

I've created a JSP application, which gets results based on a user search (using lucene). I store the results in a Bean.

I'm also using Jquery Ajax to display the results.

$.ajax({
    url : "search.jsp",
    data : "search=test",
    success : function(html) {
        ("#search_results").hide().html(html).fadeIn(1500);
    }
});

search.jsp

for (int i = 0; i < size; i++) {
    out.println(searchResult.get(i).getHTML());
}

This is working fine, however I want to change it so it returns a JSON object to JQuery and then let JQuery parse the objects and display the results

I am not sure how to do this as I'm new to JSON objects and JSP. I could possibly do something like

JSONObject json = new JSONObject();
json.put("title", "TITLE_TEST");
json.put("link", "LINK_TEST");

but I dont know how to return json to jquery then let jquery parse the objects

Any help is appreciated :)

标签: jquery json jsp
4条回答
Juvenile、少年°
2楼-- · 2019-01-12 05:17

This worked for me:

%>
String json = "{ \"title\": \"testTitle\", \"link\" : \"testLink\"}";
response.getWriter().write(json);
response.getWriter().flush();
response.getWriter().close();
<%

I used it to feed an easyui-datagrid. response.getWriter().write(json) worked, but out.println(json) did not though he didn't throw any exceptions. Also the inner quotes must be double as well, so it becomes necessary to mask them with `\".

查看更多
疯言疯语
3楼-- · 2019-01-12 05:22

Pretty simple approach would be to use taglib - json something like this :

<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>

Then you can use json tags to create it out of list:

<json:array items="${someObject.someList}" var="oneRow">
<json:object>
    <json:property name="username" value="${oneRow.username}"/>
    <json:property name="password" value="${oneRow.password}"/>
    <json:property name="email" value="${oneRow.email}"/>
</json:object>

Above jsp when executes will O/P following :

[
 {"username":"varun","password":"*****","email":"johndoe@sssdotcom"},
 {"username":"ved","password":"*****","email":"johndoe1@sssdotcom"},
 {"username":"von","password":"*****","email":"johndoe2@sssdotcom"}
]

Thats all folks!

查看更多
Evening l夕情丶
4楼-- · 2019-01-12 05:31

Here's an example you may take a look at. Basically your JSP page might look like this:

<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONObject"%>
<%
    JSONObject json = new JSONObject();
    json.put("title", "TITLE_TEST");
    json.put("link", "LINK_TEST");
    out.print(json);
    out.flush();
%>

and on the client:

$.ajax({
    url : 'search.jsp',
    data : { search: 'test' },
    dataType: 'json',
    success : function(json) {
        alert(json.title);
    }
});

And here are even more examples.

查看更多
Fickle 薄情
5楼-- · 2019-01-12 05:31

Ultimately its being trasnferred over http. So, creating a json object wont do much help.

I am not a java expert but you can create a simple string which matches with json structure and then parse it on client side.

Like

string s =  { "title": "testTitle", "link" : "testLink"}
out.println(s)

This will do the trick.

Edit: by seeing Darin's answer,

Include this on you java code,

<%@page contentType="application/json; charset=UTF-8"%>
查看更多
登录 后发表回答