我正在尝试从主干 View 执行 jsp 代码。 我正在从我的 Html 调用服务器端 api,如下所示
index.jsp(sample.js包含在这个jsp中)
%@ page contentType="text/html;charset=UTF-8"
import="foo.*,java.util.List"
%>
<%
Foo foo = new Foo();
List<XYZ> xyzList = foo.getList();
%>
我正在使用 Backbone 框架。我的js代码如下。
示例.js
SampleView = Backbone.ModalView.extend(
{
name: "SampleView",
model: SomeModel,
templateHtml : "<div ><span>Search </span>" +
"<table border='1'>"+
"<thead>"+
"<tr>"+
"<td></td>"+
"<th>header1</th>"+
"<th>header2</th>"+
"<th>header3</th>"+
"</tr>"+
"</thead>"+
"<tr>"+
"<% for(XYZ xyz: xyslist ){ %>"+
"<td><input type='checkbox' value='<%= xyz.getName()%>'></td>"+
"<td name='selected'><%= xyz.getName()%></td>"+
"<td></td>"+
"<td></td>"+
"<% } %>"+
"</tr>"+
"<tr>"+
"<td><input type='checkbox' value='test'></td>"+
"<td name='selected'>test</td>"+
"<td></td>"+
"<td></td></tr>"+
"</table><button id='select'>Select</button></div>",
initialize: function(){
_.bindAll( this, "render");
this.template = _.template( this.templateHtml);
},
events: {
"click #select": "select"
},
select: function(){
//implementation
},
render: function(){
$(this.el).html( this.template());
return this;
}
});
问题是,它不执行jsp代码。它只显示硬编码的“测试”复选框,不从服务器端检索列表。如果我在这里遗漏了什么,你能告诉我吗?谢谢。
最佳答案
JSP 中使用的 <% 和 %> 标签也被下划线模板引擎使用。当您没有任何变量可提供时,为什么要在您的 View 中使用模板?
当尝试运行您的代码时,下划线模板引擎给了我一个“意外的标识符”,因为 <% 和 %> 不包含它要处理的有效 JavaScript 代码。
代替
$(this.el).html( this.template());
你应该像这样直接插入 HTML 字符串
$(this.el).html(this.templateHtml);
关于javascript - jsp 代码未在 backbone.js View 中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12684390/