Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery post can't get entity object as json
    primarykey
    data
    text
    <p>I need to get my 'Comment' (stored in DAO) object from my controller and display it in my JSP but every time I see the error message from the error block.</p> <p>Why is this happenning and what should I do?</p> <p>Logic of my code is the next:</p> <ol> <li>After clicking on the 'Reply' button data from the form is sent to my controller.</li> <li>Controller saves data in DB and returns 'Comment' entity.</li> <li>I get this 'Comment' entity in my JSP page and use it for publishing on the page.</li> </ol> <p>But I get the error msg from error block instead of msg from the success block.</p> <p>Here is my form: </p> <pre><code>&lt;form id="comment_${comment.getCommentId()}"&gt; &lt;textarea class="textarea" rows="10" name="text"&gt;&lt;/textarea&gt; &lt;input type="hidden" name="bookId" value="${book.getBookId()}" /&gt; &lt;input type="hidden" name="parentId" /&gt; &lt;input type="hidden" name="commentId" value="${comment.getCommentId()}" /&gt;&lt;br /&gt; &lt;input type="button" class="button" value="Reply" id="submit_${comment.getCommentId()}" onclick="ajaxsubmit(this.id)"/&gt; &lt;/form&gt; </code></pre> <p>Here is my script:</p> <pre><code>&lt;script type="text/javascript"&gt; function ajaxsubmit(buttonId){ var formId = document.getElementById(buttonId).parentNode.id; var dataString = $("#" + formId).serialize(); $.ajax( { url: "ajaxcomment.htm", type: "post", data: dataString, dataType: "json", success: function(data) { alert(data.getCommentAdded()); }, error: function() { alert("error"); } } ); } </code></pre> <p>Here is my controller</p> <pre><code>@RequestMapping(value = "ajaxcomment.htm", method = RequestMethod.POST) public @ResponseBody Comment ajaxcomment( HttpServletRequest httpServletRequest, @RequestParam(value = "bookId", required = false) Long bookId, @RequestParam(value = "parentId", required = false) Long parentId, @RequestParam(value = "commentId", required = false) Long commentId, @RequestParam(value = "text", required = true) String text) { String username = httpServletRequest.getRemoteUser(); User user = userDao.getUserByUsername(username); Comment comment = new Comment(); // DAO logic commentDao.addComment(comment); return comment; } </code></pre> <p>Here is my 'Comment' entity:</p> <pre><code>@Entity @Table(name = "COMMENT") public class Comment implements Serializable { @Id @GeneratedValue @Column(name = "COMMENT_ID", nullable = false) private Long commentId; @Column(name = "COMMENT_TEXT", length = 512, nullable = true) private String commentText; @Column(name = "COMMENT_ADDED", length = 128, nullable = true) @Temporal(TemporalType.TIMESTAMP) private Date commentAdded; @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER) @JoinColumn(name = "BOOK_ID") private Book book; @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER) @JoinColumn(name = "PARENT_ID") private Comment parentComment; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "parentComment") @OrderBy("commentAdded") private Collection&lt;Comment&gt; subcomments; public void setCommentText(String commentText) { this.commentText = commentText; } public String getCommentText() { return this.commentText; } // other getters and setters are public too </code></pre> <p>And here is my applicationContext.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt; &lt;mvc:annotation-driven /&gt; &lt;context:component-scan base-package="com.demo"/&gt; &lt;context:component-scan base-package="com.demo.service"/&gt; &lt;context:annotation-config /&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt; &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt; &lt;property name="suffix" value=".jsp"/&gt; &lt;/bean&gt; &lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/&gt; &lt;bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt; &lt;property name="messageConverters"&gt; &lt;list&gt; &lt;ref bean="jacksonMessageConverter" /&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="exceptionMessageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"&gt; &lt;property name="messageConverters"&gt; &lt;list&gt; &lt;!-- Support JSON --&gt; &lt;ref bean="jacksonMessageConverter" /&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload