0

我是MVC和Play框架(Java)的新手。我沒有爲動態HTML使用Groovy,而是使用靜態HTML創建了自己的頁面,我的意思是我們沒有任何Groovy表達式。在這裏,我有一個控制器「客戶」,生成JSON對象,該對象必須發送到視圖中的ajax調用。我嘗試使用render()方法,似乎我沒有正確使用。你能否給我一些想法從這裏轉發。謝謝。在播放框架中從視圖到控制器的ajax調用

public static void customer(){ 
    WordAPI objWordAPI=new WordAPI(); 
    List<WordInfo> listObjWord= objWordAPI.MakeAPIObject(nSurveyId); 
    JSONSerializer modelSerializer=new JSONSerializer().exclude("NSpontanity","NWordRepresentativity","NWordValue","NWordFrequency","class").rootName("Words"); 
    render("Application/wordcloud.html",modelSerializer.serialize(listObjWord)); 
    } 

,並鑑於Ajax調用 「wordcloud.html」

$.ajax({ 
    url: "/customer", 
    dataType : 'json', 
    success: function (data) { 
     alert(data); 
      } 
     }) 
+0

如果你有一個叫做Customer的控制器,並且有一個叫做customer的方法,那麼url應該是/ customer/customer。如果你想使用url/customer,那麼你必須明確地在conf/routes文件中定義它 – seePatCode

回答

0

我認爲這應該工作:

public static void customer(){ 
    WordAPI objWordAPI=new WordAPI(); 
    List<WordInfo> listObjWord= objWordAPI.MakeAPIObject(nSurveyId); 
    JSONSerializer modelSerializer=new JSONSerializer().exclude("NSpontanity","NWordRepresentativity","NWordValue","NWordFrequency","class").rootName("Words"); 
    renderJSON(modelSerializer.serialize(listObjWord)); 
    } 

我以前從未使用過ROOTNAME,我通常只是做一些事情更多類似這樣:

public static void refreshNotifications() 
    { 
     JSONSerializer notifySerializer = new JSONSerializer().include("message","notifyId","class").exclude("*"); 
     List<Notification> notificationList = user.getNotifications(); 
     renderJSON(notifySerializer.serialize(notificationList)); 
    } 

側注意:使用refreshNotifications,我有一個我運行的安全方法,在此之前驗證並填充用戶對象。

相關問題