将JSON数据呈现给播放框架模板 [英] Present JSON data to a play framework template
问题描述
在没有任何JQuery详细知识的情况下尝试使其工作.我真的很难找到一个易于理解的示例,说明如何从我传递给String对象内部的json的JSON中创建未编号列表.
Trying to get it to work without any detail knowledge of JQuery. I am really having a hard time finding an comprehensible example of how i would create an unnumbered list out of some json that i am passing to the front inside a String object.
我正在使用Play!框架.我的应用程序有一个方法,该方法返回一个包含项的json数组的字符串.
I am using the Play! Framework. My Application has a method that returns a string holding an json array of items.
GET /items controllers.Application.items()
方法如下:
public static Result items() {
return ok(Json.toJson(Item.all()));
}
您将如何处理这些数据,以使模板以未编号列表的形式显示?
How would you process this data in order to have your template present it as an unnumbered list?
数据,例如:
@Entity
public class Item {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int id;
public String title;
public String type;
public int quantity;
public BigDecimal unitPrice;
public Item() {}
public static List<Item> all() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("defaultPersistenceUnit");
EntityManager entityManager = entityManagerFactory.createEntityManager();
TypedQuery<Item> query = entityManager.createQuery("SELECT i FROM Item i", Item.class);
return query.getResultList();
}
推荐答案
您需要使用JavaScript ajax请求调用items()操作.然后,您可以使用javascript和jQuery创建列表.
You need to call the items() action with a javascript ajax request. Then you can use javascript and jQuery to create your list.
类似这样的东西:
<script type="text/javascript">
$(function(){
$.getJSON('/items', function(items){
var ul = $('<ul>');
$.each(items, function(item){
var li = $('<li>').text(item.title);
ul.append(li);
});
$('body').append(ul);
});
});
</script>
这篇关于将JSON数据呈现给播放框架模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!