繁体   English   中英

无法使用Mysql / Java在基于Play2.4的Web应用程序中使用咖啡脚本显示Json数据

[英]Can't get Json data to display using coffee script in Play2.4 based web app with Mysql/Java

我正在使用play 2.4框架来创建一个非常基本的Web应用程序。 这是我的代码:

Application.java

package controllers;
import play.*;
import play.db.ebean.Transactional;
import play.mvc.*;
import views.html.*;
import models.Players;
import play.data.Form;
import java.util.List;
import com.avaje.ebean.Model;
import static play.libs.Json.toJson;


public class Application extends Controller {

@Transactional
public Result index() {
    return ok(index.render());
}
@Transactional
public Result addPlayer() {
    Players player = Form.form(Players.class).bindFromRequest().get();
    player.save();
    return redirect(routes.Application.index());
}

@Transactional
public Result getPlayer() {
    List<Players> players = Players.FIND.findList();
    return ok(toJson(players));
}}

Players.java

package models;
import javax.persistence.*;
import com.avaje.ebean.Model;


@Entity

@Table(name = "player_info")
public class Players extends Model {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int id;

    public String name;
   public static Model.Finder<Integer, Players> FIND = new Model.Finder<>(Players.class);
}

Index.scala.html

@main("Welcome to TSI") {

     <div>

     <script type='text/javascrips' src='@routes.Assets.versioned("javascripts/main.js")'></script>

     <ul id="players_list" ></ul>

      </div>

     <form action="@routes.Application.addPlayer()" method="post">
      <input type="text" name="name" />
      <button type="submit">Add player</button>
     </form>
}

Routes

# Home page
GET     /                           controllers.Application.index()

POST    /player                     controllers.Application.addPlayer()   

GET     /players                    controllers.Application.getPlayer()  

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)

Main.coffee

$ ->
   $.get "/players", (players) ->
      $.each players, (index, player) ->
         $('#players_list').append $("<li>").text player.name

当用户从Web表单输入时,可以完美地从DB保存/检索数据。 但是,当将数据从JSON获取到HTML时,它什么也不显示(空列表)。

我确实试图使用JS和其他咖啡脚本,但没有一个起作用,所以不确定我要去哪里。

任何帮助将不胜感激。 谢谢

看来您有错字。 代替

<script type='text/javascrips' ...

它应该是

<script type='text/javascript' ...

通过在脚本标签中使用defer = "defer"参数解决了问题,因为它无法识别列表标签,因此必须等待声明它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM