【问题标题】:jquery load function not invoking spring boot controller to replace thymeleaf template fragmentjquery加载函数不调用spring boot控制器来替换thymeleaf模板片段
【发布时间】:2018-12-12 16:05:24
【问题描述】:

我正在尝试将内容加载到以下片段中。当我对 html 模板进行基本映射时,我已确认此片段有效。

<div id="fbtresult" th:if="${not #lists.isEmpty(fbts)}" th:fragment="fbtList">
    <h2>Frequent Bought Together List</h2>
    <table class="table table-striped">
        <tr>
            <th>Id</th>
            <th>Main Product Id</th>
            <th>FBT 1 Product ID </th>
            <th>FBT 2 Product ID</th>
            <th>BSR</th>
            <th>View</th>
        </tr>
        <tr th:each="fbt : ${fbts}">
            <td th:text="${fbt.id}"><a href="/fbt/${fbt.id}">Id</a></td>
            <td th:text="${fbt?.mainProduct?.asin}">main product asin</td>
            <td th:text="${fbt.sproductFbt1 != null} ? ${fbt.sproductFbt1.asin} : ''">product fbt1 asin</span>
            <td th:text="${fbt.sproductFbt2 != null} ? ${fbt.sproductFbt2.asin} : ''">product fbt2 asin</span>
            <td th:text="${fbt.bsr}">bsr</td>        
            <td><a th:href="${ '/fbt/' + fbt.id}">View</a></td>
        </tr>       
    </table>
</div>

但是,我正在尝试使用 canvasjs click 函数中的以下查询加载语句。我已经确认 jquery 正在加载,控制台将在单击 canvasjs 图表时记录 url。

                    click: function (e) {
                        var asin = /*[[${asin}]]*/ 'null';
                        var url = "/FBTChartfbtrequest?fbt=2&asin=" + asin + "&collectdate=" + e.dataPoint.x;
                        console.log(url);
                        $("#fbtresult").load(url);
                    },

但是 $("#fbtresult").load(url); 没有执行以下控制器从日志中可以清楚地看到。

    @GetMapping(value = "/FBTChartfbtrequest")
        public String FBTChartfbtrequest(@RequestParam("fbt") String fbt,
                @RequestParam("asin") String asin, @RequestParam("collectdate") String collectdate, Model model) {
            System.out.println("ZZZZ requestFBTCHar with asin " + asin + " and collectdate " + collectdate);

...
                    model.addAttribute("fbts", fbtService.findByASINInFBT1andDateRange(asin, convertUtilToSql(date), convertUtilToSql(date)));
....
            return "results :: fbtList";
        }

我已确认在使用 window.location.href = 进行重定向时,我可以使用 /FBTChartfbtrequest?fbt=2&asin=" + asin + "&collectdate=" + e.dataPoint.x; url 加载新页面 =网址;

我的 pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.4</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

http://localhost:8086/FBTMetricsRequest 甚至在我单击图表之前就在网络选项卡中可见,并且单击图表不会在网络视图中进行更改。

知道为什么 jquery 加载函数没有调用控制器函数吗?

【问题讨论】:

  • 你能检查你的网络日志吗,jquery 向服务器发送请求。根据我的假设,它应该向服务器发送请求。请发布网络日志截图。
  • @swarooppallapothu 我已经添加了屏幕截图

标签: javascript java jquery spring-boot thymeleaf


【解决方案1】:

Ajax Centric 解决方案如下。不过,我仍然希望运行以模板为中心的版本。

$.ajax({
    url: url,
    method: "GET",
    success: function (res) {
        $("#fbtresult").html(res);
    },
    fail: function (err) {
        console.log(err);
    }

});

通过以下控制器调整

@GetMapping(value = "/FBTChartfbtrequest")
public String FBTChartfbtrequest(@RequestParam("fbt") String fbt,
@RequestParam("asin") String asin, @RequestParam("collectdate") String collectdate, Model model) {
    model.addAttribute("fbts", fbtService.findByASINInFBT1andDateRange(asin, convertUtilToSql(date), convertUtilToSql(date)));
    return "fbtList";
}

和HTML修改

<div id="fbtresult">
     <!-- Dynamic content using ajax -->
</div>

这不如引用对象来创建新链接的能力那么可取。

Id 主要产品asin 产品 fbt1 asin 产品 fbt2 asin bsr
查看

我想我可以在包含这些链接的 java 应用程序中生成 html。我想我更喜欢在模板视图中创建动态 html。

【讨论】:

    【解决方案2】:

    原来应该把load函数调整成如下这样才能使用fragments逻辑

    var url = "/FBTChartfbtrequest?";
    $('#fbtresult').load(url, "fbt=1&asin=" + asin + "&collectdate=" + e.dataPoint.x);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2017-02-02
      • 2019-04-11
      • 2021-03-03
      • 1970-01-01
      • 2020-03-14
      相关资源
      最近更新 更多