2012-01-07 58 views
0

我需要從我的控制器中獲取我的'Comment'(存儲在DAO中)對象,並將其顯示在我的JSP中,但每次看到錯誤塊的錯誤消息。jQuery post無法獲得實體對象爲json

這是爲什麼發生,我該怎麼辦?

邏輯的我的代碼是下一個:

  1. 點擊從表單「回覆」按鈕的數據之後被髮送到我的控制器。
  2. 控制器將數據保存在數據庫中並返回'Comment'實體。
  3. 我在我的JSP頁面中獲得這個'Comment'實體並將其用於在頁面上發佈。

但是,我從錯誤塊中獲取錯誤消息,而不是來自成功塊的消息。

這裏是我的形式:

<form id="comment_${comment.getCommentId()}"> 
    <textarea class="textarea" rows="10" name="text"></textarea> 
    <input type="hidden" name="bookId" value="${book.getBookId()}" /> 
    <input type="hidden" name="parentId" /> 
    <input type="hidden" name="commentId" value="${comment.getCommentId()}" /><br /> 
    <input type="button" class="button" value="Reply" id="submit_${comment.getCommentId()}" onclick="ajaxsubmit(this.id)"/> 
</form> 

這裏是我的腳本:

<script type="text/javascript"> 
    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"); 
     } 
    }); 
} 

這裏是我的控制器

@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; 
} 

這裏是我的 '評論' 實體:

@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<Comment> subcomments; 

public void setCommentText(String commentText) { 
    this.commentText = commentText; 
} 
public String getCommentText() { 
    return this.commentText; 
} 

// other getters and setters are public too 

這裏是我的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?><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"> 

<mvc:annotation-driven /> 
<context:component-scan base-package="com.demo"/> 
<context:component-scan base-package="com.demo.service"/> 
<context:annotation-config /> 


<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
    <property name="prefix" value="/WEB-INF/jsp/"/> 
    <property name="suffix" value=".jsp"/> 
</bean> 

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> 

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="messageConverters"> 
     <list> 
      <ref bean="jacksonMessageConverter" /> 
     </list> 
    </property> 
</bean> 

<bean id="exceptionMessageAdapter" 
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"> 
    <property name="messageConverters"> 
     <list> 
      <!-- Support JSON --> 
      <ref bean="jacksonMessageConverter" /> 
     </list> 
    </property> 
</bean> 

回答

0

它的工作原理。

我已經更新了我的控制器方法(處理):

@Autowired private DAOComment commentDao; 

@RequestMapping(value = "ajaxcomment.htm", method = RequestMethod.POST) 
public @ResponseBody String 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 "{\"id\":\"" + comment.getCommentId() + "\"," + 
    "\"username\":\"" + comment.getUser().getUsername() + "\"," + 
    "\"text\":\""  + comment.getCommentText() + "\"," + 
    "\"added\":\""  + comment.getFormattedDate() + "\"}"; 
} 

我用\」,而不是「,因爲JSON.parse()不解析字符串如'param':'value'"param":"value"只有

更新腳本:

<script type="text/javascript"> 
    function ajaxsubmit(buttonId, className){ 
     var formId = document.getElementById(buttonId).parentNode.id; 
     var dataString = $("#" + formId).serialize(); 
     $.ajax({ 
      url: "ajaxcomment.htm", 
      type: "post", 
      data: dataString, 
      dataType: "text", 
      success: function(data) { 
       var response = JSON.parse(data); 

       //This's it. 
       alert(response.id); 

      }, 
      error: function() { 
       alert("Error has occured."); 
      } 
     }); 
    } 
</script> 
0

這可能是在你的表格你喜歡value="${book.getBookId()}",而不是value="${book.bookId}"設定值。

換句話說,您必須通過引用bean中的字段名稱(實體或其他)來使用表達式語言,而不是直接使用該字段的getter。

+0

我知道,但'評論'實體中的所有字段都是私人的。 – Dmytro 2012-01-11 11:23:23

+0

但您可以擁有公共獲得者 – Fortunato 2012-01-11 15:59:56

+0

所有獲得者和接受者仍然是公共的。 – Dmytro 2012-01-11 16:26:20