javascript - jsp 代码未在 backbone.js View 中执行

标签 javascript jquery jsp backbone.js backbone-views

我正在尝试从主干 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/

相关文章:

javascript - 如何将日期与当前日期进行比较以应用逻辑

javascript - 悬停并单击 Jquery

javascript - 带有数据的初始化表仅适用于断点

eclipse - 我无法使用 eclipse 将 jsp 与 sql server 连接

java - 如何处理渲染 View 时抛出的异常

jsp - SpringBoot应用程序: Accessing Common application properties in a JSP page

javascript - 仅使用最后一个数组项动态生成的内容

javascript - document.getElementById.innerHTML 只工作一次 - 如何配置全局复制元素?

javascript - 在页面加载时进行初始验证后验证 if 语句

jquery - 如果我更改其在代码中的位置,该链接将不起作用