简体   繁体   English

使用PrototypeJs,如何异步加载jsp页面

[英]Using PrototypeJs, how to load a jsp page asynchronously

We're using Spring and PrototypeJs in our app. 我们在应用程序中使用Spring和PrototypeJs。 Basically I want to have a template page and then fill the template page with some values. 基本上,我想要一个模板页面,然后用一些值填充模板页面。 I found a JQuery.load method but we're not using JQuery. 我找到了一个JQuery.load方法,但我们没有使用JQuery。

Closest thing I found was prototype Template object which is just a string. 我发现的最接近的东西是原型Template对象,它只是一个字符串。 I can make a template in Javascript like the following: 我可以使用Javascript制作如下模板:

var template1 = new Template("user id: #id name is <span class name='userName'>#name</span>");

and then once I have all the data as json fill in the blanks but this is not enough. 然后一旦我将所有数据作为json填入空白,但这还不够。 I want to display tags etc and be more complex and ideally just load this template file once and just fill in the values later. 我想显示标签等,并且要更复杂一些,理想情况下,只需加载一次此模板文件,以后再填写值即可。 Is that even possible without using backbone.js or angularjs? 如果不使用骨干.js或angularjs,这是否有可能?

I guess my question is, how do I construct a page better? 我想我的问题是,如何更好地构建页面? Currently I'm creating my entire page in javascript (eg new Element("div").addClassName("something")) so I was hoping to create my page 'template' in a jsp or some other form and then just fill in the blanks. 当前,我正在用javascript创建我的整个页面(例如new Element(“ div”)。addClassName(“ something”)),所以我希望以jsp或其他形式创建页面“模板”,然后填写空白。

I have all the data to display on the page as json. 我所有的数据都以json的形式显示在页面上。

The Template class is quite flexible about how you create its members. Template类在创建成员方面非常灵活。 What I would do, for readability, is code your template as an actual page element in the HTML, and then remove it on page load before using it to fill in your JSON. 为了便于阅读,我要做的是将模板编码为HTML中的实际页面元素,然后在页面加载时将其删除,然后再使用它来填充JSON。

<div id="template-1" class="#{classname}">
  <h1>#{headline}</h1>
  <p>#{description}</p>
</div>
<div id="articles"></div>

<script type="text/javascript">
var tmp = $('template-1').remove();
tmp.writeAttribute('id',false);
var template = new Template(tmp.outerHTML);
// do whatever with it
$('articles').insert(template.evaluate(myJSON));
</script>

That should get you started. 那应该让您开始。 The major benefit of working this way is that you don't have to keep backslashing quotes and writing everything in one line, or bashing it together with + signs. 以这种方式工作的主要好处是,您不必保留反斜杠引号并将所有内容写在一行中,也不必将其与+号一起打击。

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

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