怎么把JSP转换成Thymeleaf?

时间:2019-06-19 18:14:41

标签: java spring jsp model-view-controller thymeleaf

我在Thymeleaf方面遇到问题,我不知道该怎么办。

<!-- chart.jsp-->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
window.onload = function() {

var dps = [[]];
var chart = new CanvasJS.Chart("chartContainer", {
    animationEnabled: true,
    exportEnabled: true,
    title: {
        text: "Simple Column Chart with Index Labels"
    },
    data: [{
        type: "column", //change type to bar, line, area, pie, etc
        //indexLabel: "{y}", //Shows y value on all Data Points
        indexLabelFontColor: "#5A5757",
        dataPoints: dps[0]
    }]
});

var xValue;
var yValue;
var indexLabel;

<c:forEach items="${dataPointsList}" var="dataPoints" varStatus="loop">
    <c:forEach items="${dataPoints}" var="dataPoint">
        xValue = parseInt("${dataPoint.x}");
        yValue = parseFloat("${dataPoint.y}");
        indexLabel = "${dataPoint.indexLabel}";
        dps[parseInt("${loop.index}")].push({
            x : xValue,
            y : yValue,
            indexLabel : indexLabel
        });
    </c:forEach>
</c:forEach>

chart.render();

}
</script>
</head>
<body>
    <div id="chartContainer" style="height: 370px; width: 100%;"></div>
    <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>

我刚刚开始使用Thymeleaf,并且出于未知原因,JSP格式无法正常工作。

1 个答案:

答案 0 :(得分:0)

此答案并不涵盖将jsp转换为thymeleaf的所有方面,但应该为您提供一些指导。

  1. 您需要创建一个html文件chart.html并将其保存在src/main/resource/templates
  2. 您的html应该以

    开头

    <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org">

  3. 您的循环将更改为

    <c:forEach items="${dataPointsList}" var="dataPoints" varStatus="loop">

    th:each="dataPoints:${dataPointsList}"

    <c:forEach items="${dataPoints}" var="dataPoint">

    th:each="dataPoint:${dataPoints}"

  4. 看看thymeleaf documentation。他们有很好的文档。