I am having comment class, I want to show its property values to jsp page using json
Comments.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package example; import java.sql.Timestamp; public class Comments { private int id; private String name; private String pic; private String comment; private Timestamp commentDate; public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public Timestamp getCommentDate() { return commentDate; } public void setCommentDate(Timestamp commentDate) { this.commentDate = commentDate; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } } |
ListJson assigning values for comments.java. Here we fetch value of commentList and totalcomments value in our jsp page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package example; import com.opensymphony.xwork2.ActionSupport; import java.util.ArrayList; public class ListJson extends ActionSupport { private ArrayList commentList = new ArrayList<comments>(); private long totalComments; public String execute() { comments c = new comments(); c.setId(1); c.setName("Manish"); c.setComment("Hi how are you"); c.setPic("a.jpg"); c.setCommentDate(null); comments d = new comments(); d.setId(2); d.setName("Nita"); d.setComment("I am fine"); d.setPic("b.jpg"); d.setCommentDate(null); commentList.add(c); commentList.add(d); setTotalComments(commentList.size()); System.out.println("In action"); return SUCCESS; } public long getTotalComments() { return totalComments; } public void setTotalComments(long totalComments) { this.totalComments = totalComments; } public ArrayList getCommentList() { return commentList; } public void setCommentList(ArrayList commentList) { this.commentList = commentList; } } </comments> |
Struts.xml
1 2 3 4 5 | <package name="example1" extends="struts-default, json-default"> <action name="fetchComments" class="example.ListJson"> <result type="json"></result> </action> </package> |
In Jsp page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <%@page contentType="text/html" pageEncoding="UTF-8"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <input type="button" id="button1" value="Submit"> <div class="total-comments"></div> <div class="allcomments"> </div> <script> $("#button1").click(function() { $.getJSON('fetchComments', function(data) { $(".total-comments").html("<h2>Total comments are "+data.totalComments+"</div>"); var msg; $.each(data.commentList, function(j) { msg='<div class="comment">'; msg+= '<div class="id">'+data.commentList[j].id+'</div>' msg+= '<div class="name">'+data.commentList[j].name+'</div>'; msg+= '<div class="image">'+data.commentList[j].pic+'</div>' msg+= '<div class="image">'+data.commentList[j].comment+'</div>' msg+= '<div class="commentDate">'+data.commentList[j].commentDate+'</div>' msg+='</div>'; $(".allcomments").append(msg); }); return false; }); }); </script> |