无法使用 Thymeleaf 显示模型对象属性的值
下面是我的控制器和 html,使用 Spring boot 和 thymeleaf。我无法显示文本值“headers1”。如果我遗漏了什么,请告诉我。
@GetMapping(value = "/tabsDetails1")
@ResponseBody
public String getTabsDetails1(@RequestParam String Id, Model m) {
m.addAttribute("headers","headers1");
return "pages/enr :: tab-info";
}
--AJAx调用获取数据会调用tabsDetails1----- 单击按钮将调用 showTabsDetails 并应使用 thymeleaf 显示数据。
function showTabsDetails(id) {
var param = '?id=' + $("#id").val();
$.ajax({
url : "tabsDetails1"+param,
// method : 'GET',
// data : $form.serialize(),
success : function(response) {
$('#enrDiv').show();
}
});
}
----enr HTML------
<div th:fragment="tab-info" id="enrDiv" >
<div class="panel panel-default">
<div class="panel-heading"><b>TEST</b></div>
<div class="panel-body">
<h2 th:text="${headers}"></h2>
</div>
</div>
</div
另外,我通过片段将对象传递到新的 Html,但我在带有 th:field 属性的文本输入上看不到值。奇怪的是我能够看到 th:text 的值,但不能看到 th:field
<div class="tab-content">
<div class="tab-pane fade" th:classappend="${barStat.first} ? 'show active'" th:id="${h.key}" th:each="h, barStat: ${headers}">
<div th:if="${h.key} == 'MY_FORM'">
<div th:replace="~{fragments/myForm :: myHtmlForm(id,${h.value}) }"></div>
</div>
</div>
</div>
下面是 myHtmlForm 片段代码
<div th:fragment="myHtmlForm(id,headers)">
<div class="setup-content" id="step-5">
<form th:action="@{/saveForms}" th:id="${id}" method="post" th:object="${headers}" class="form-horizontal" >
<p th:text="${headers.name}"></p>
<div class="row form-row">
<div class="col-md-6 form-group">
<label class="control-label">* Name</label>
<input type="text" th:text="${headers.name}" class="form-control">
<!-- <span class="error style="display: none;"> Please enter a Name.</span> -->
</div>
</div>
</form>
</div>
</div>
Below is my controller and html using Spring boot with thymeleaf. I'm not able to display the text value "headers1". Please let me know if i'm missing something.
@GetMapping(value = "/tabsDetails1")
@ResponseBody
public String getTabsDetails1(@RequestParam String Id, Model m) {
m.addAttribute("headers","headers1");
return "pages/enr :: tab-info";
}
--AJAx Call to get data will call tabsDetails1-------
On click of the button will call showTabsDetails and should show the data using thymeleaf.
function showTabsDetails(id) {
var param = '?id=' + $("#id").val();
$.ajax({
url : "tabsDetails1"+param,
// method : 'GET',
// data : $form.serialize(),
success : function(response) {
$('#enrDiv').show();
}
});
}
----enr HTML------
<div th:fragment="tab-info" id="enrDiv" >
<div class="panel panel-default">
<div class="panel-heading"><b>TEST</b></div>
<div class="panel-body">
<h2 th:text="${headers}"></h2>
</div>
</div>
</div
Also I'm passing an object through fragment in to new Html and I dont see value on the text input with th:field attribute. Weird thing is i'm able to see the value with th:text but not with th:field
<div class="tab-content">
<div class="tab-pane fade" th:classappend="${barStat.first} ? 'show active'" th:id="${h.key}" th:each="h, barStat: ${headers}">
<div th:if="${h.key} == 'MY_FORM'">
<div th:replace="~{fragments/myForm :: myHtmlForm(id,${h.value}) }"></div>
</div>
</div>
</div>
Below is myHtmlForm fragment code
<div th:fragment="myHtmlForm(id,headers)">
<div class="setup-content" id="step-5">
<form th:action="@{/saveForms}" th:id="${id}" method="post" th:object="${headers}" class="form-horizontal" >
<p th:text="${headers.name}"></p>
<div class="row form-row">
<div class="col-md-6 form-group">
<label class="control-label">* Name</label>
<input type="text" th:text="${headers.name}" class="form-control">
<!-- <span class="error style="display: none;"> Please enter a Name.</span> -->
</div>
</div>
</form>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论