gpt4 book ai didi

java - 无法在基于 Play2.4 的 Web 应用程序和 Mysql/Java 中使用 CoffeeScript 显示 Json 数据

转载 作者:行者123 更新时间:2023-11-30 22:30:38 24 4
gpt4 key购买 nike

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

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

路线

# 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

当用户从网络表单输入时,数据可以完美地从数据库中保存/检索。但是在将数据从 JSON 转换为 HTML 时,它什么也没显示(空列表)。

我真的尝试过使用 JS 和其他 CoffeeScript ,但都没有用,所以不确定我哪里出错了。

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

最佳答案

你好像打错了。而不是

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

应该是

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

关于java - 无法在基于 Play2.4 的 Web 应用程序和 Mysql/Java 中使用 CoffeeScript 显示 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33989322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com