简体   繁体   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

I am using play 2.4 framework to create a very basic web app. 我正在使用play 2.4框架来创建一个非常基本的Web应用程序。 Here are my codes: 这是我的代码:

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

Data gets saved/retrieved from DB perfectly when user inputs from the web form. 当用户从Web表单输入时,可以完美地从DB保存/检索数据。 But while getting the data from JSON to HTML, it shows nothing (empty list). 但是,当将数据从JSON获取到HTML时,它什么也不显示(空列表)。

I really tried to use JS and other coffee scripts but none worked, so not sure where I am going wrong. 我确实试图使用JS和其他咖啡脚本,但没有一个起作用,所以不确定我要去哪里。

Any help will be appreciated. 任何帮助将不胜感激。 Thanks 谢谢

It seems you have a typo. 看来您有错字。 Instead of 代替

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

it should be 它应该是

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

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

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

相关问题 如何使用PHP脚本和JSON从MySQL获取数据并将其存储到Java程序 - How to get data from MySQL and store it to Java program using PHP script and JSON Play Framework-java 2.4中的数据验证 - Data Validation in play Framework-java 2.4 Java Web App-从MySQL表获取结果并在Web上显示的最佳方法 - java web app - best way to get results from a MySQL table and display on web 获取从Java Controller到Coffee Script的会话值 - Get the session value from Java Controller to Coffee Script 如何使用Java Web服务从mysql数据库获取数据? - How to get data from mysql databse using java web services? Java播放! -显示格式化的JSON - Java Play! - display formatted JSON 无法根据 Android Studio 中的加速度计数据更新应用程序 - Can't get app to update based on accelerometer data in Android Studio 使用Spring引导游戏2.4 Java应用程序的方法是什么? - What's the way to bootstrap a play 2.4 Java app with Spring? Calling json data from external api to be entered into mysql database- Java Spring Boot Web app - Calling json data from external api to be entered into mysql database- Java Spring Boot Web app 如何在Java Play 2.4.x中使用TestNG? - How can I use TestNG with Java Play 2.4.x?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM