【问题标题】:ajax call from view to controller in Play frameworkPlay框架中从视图到控制器的ajax调用
【发布时间】:2012-09-22 09:24:01
【问题描述】:

我是 MVC 和 Play 框架 (Java) 的新手。我没有将 Groovy 用于动态 HTML,而是使用静态 HTML 创建了我们自己的页面,我的意思是我们没有任何 Groovy 表达式。在这里,我有一个控制器“客户”,生成 JSON 对象,该对象必须发送到视图中的 ajax 调用。我尝试了 render() 方法,似乎我没有正确使用。你能给我一些想法从这里转发吗?谢谢。

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

视图“wordcloud.html”中的 ajax 调用

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

【问题讨论】:

  • 如果你有一个名为 Customer 的控制器和一个名为 customer 的方法,那么 URL 应该是 /customer/customer。如果您想使用 url /customer 那么您必须在 conf/routes 文件中明确定义它

标签: model-view-controller jquery playframework playframework-1.x


【解决方案1】:

我相信这应该可行:

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 我有一个安全方法,我在此之前运行它验证并填充用户对象。

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 2012-05-02
    • 2015-04-15
    • 2021-12-19
    • 2015-05-28
    • 2016-11-14
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多