English 中文(简体)
使用 java 服务器的 brastestrapping keep.js 模型
原标题:Bootstrapping backbone.js models with java server

我正计划用主干键Js和 Java 后端运行球衣的单页网页应用程序。

当您的应用程序首次加载时, 通常都会有一套您知道您需要的初始模型, 以便创建页面。 与其另外发布 AJAX 请求来获取这些模型, 不如将其数据嵌入页面中。

我认为我需要做的示例,请见 rel=“不跟随 norefererr”>>backbone-paterns ,但对于红宝石和php来说,我并没有将这个点与这个点在java中如何运作联系起来。我是否需要使用像mashache.java(mashache.java)这样的临时图书馆,并在页面送达前预处理该页面?有人能举一个例子吗?

谢谢,马克

最佳回答

是的, 您需要某种形式的微调, 才能将动态数据与 HTML 页面的静态版式混在一起。 但是, 如果您只想要一个页面, 您可能不想投资建立整个临时图案。 您可以简单地将您的页面分成两部分, 并服务于您的主页, 将第一个部分放出, 然后是您的 JSON 演示, 然后是第二部分 。 那种丑陋, 但取决于您是否值得为这个案例添加微调。 如果您选择使用微调, 您可以使用胡子, 或者我建议“ shref=” http://trevel. ache. org/ engine/index. html" rel=“ no follow” 。 模板。 < / a >

从泽西的代码读取, 看起来你需要拨打"http://wiki.fasterxml.com/JacksonHome" rel="no follow">Jackson , 明确将你的靴状模型从爪哇转换为JSON。

问题回答

我并不熟悉泽西岛, 但似乎主要是一个休息的网络服务框架。 您似乎可以使用@ productedes annatation 提供“ text/ html” 的内容类型 。 但是, 我看不到一种显而易见的方法来使用一个纹理系统来表达观点。 因此, 您也许应该先从一个 Java 网络框架开始, 如 < a href=" http://www.springsource.org/" rel="nofollow " > Spring 或 < a href=" "http://www. playframework.org/" rel=“nofollower” >Play 来为最初的 HTML 服务, 其中包括通过查看模板( 如果您喜欢的话, mustache ) 收集数据。 您仍然可以使用泽西语为随后的 Ajax/Restful 电话服务, 但是 Spring 和 Play 都有恢复的概念。

根据模板引擎的不同, 解决方案是不同的。 我不太了解这个 JSP 示例。 使用 Jackson 进行 JSON 序列化, 这是 JSP 标签文件 :

public class JsonTag extends SimpleTagSupport {

/** the instance to serialize as JSON JSON */
private Object value;

@Override
public void doTag() throws JspException, IOException {
    String json = new ObjectMapper().writeValueAsString(value);
    getJspContext().getOut().print(json);
}

public void setValue(Object value) {
    this.value = value;
}

}

您的 HTML 页面 :

<script>
var router = new App.TabRouter({
    model: new App.MyModel(<k:json value="${it}"/>)
});
</script>

对于一个严肃的应用程序, 您应该重复使用对象Mapper 实例, 而不是每次创建一个新的实例 。

将模型 JSON 转换成标签。 http:// ricostacruz. com/backbone- patterns/" rel=“ nofollow” >backbone- patters 链接 :

<script>
  // app data, rendered on server
  var photosJSON = [
    { id: 2, name: "My dog", filename: "IMG_0392.jpg" },
    { id: 3, name: "Our house", filename: "IMG_0393.jpg" },
    { id: 4, name: "My favorite food", filename: "IMG_0394.jpg" },
    { id: 5, name: "His bag", filename: "IMG_0394.jpg" },
  ];
 </script>      
 ...

 <script>
  var Photo = Backbone.Model.extend({
  // photo attributes (from above)
  });

  var PhotoCollection = Backbone.Collection.extend({
     model:Photo
  });

  // build collection model without AJAX call using
  // server rendered JSON above
  var photosCollection = new Photos(photosJSON);
</script>

您可以在服务器上使用任何适当的技术, 使用 JSON 数据来制作脚本 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签