我正在尝试动态填充模态。
首先,我通过 AJAX 发出 GET 请求,在该请求中发送所需对象的 id。然后我尝试更新我的模型属性,以便获得所需的对象。这就是所有失败的地方。我无法更新模型的属性。
否则一切正常。模式打开,但没有更新其 html。
理想情况下,我希望像这样解决这个问题:
1) 模态打开
2) Ajax 请求
3) Modal的html更新
我使用百里香作为我的模板引擎。
主控制器.java
@RequestMapping("/main")
public String main(Model model)
{
model.addAttribute("allVehicles", allVehicles);
model.addAttribute("selectedCar", new Car());
return "main";
}
@RequestMapping("/car/{id}")
@ResponseStatus(value = HttpStatus.OK)
public void car(@PathVariable("id") String id, Model model)
{
Car car = updateCarById(id);
model.asMap().replace("selectedCar", car);
}
main.html
...
<div th:each="vehicle : ${allVehicles}">
<div th:each="car : ${vehicles.allCars}">
<a th:onclick="'javascript:openModal(\'' + ${car.id} + '\');'">
</a>
</div>|
</div>
...
<script th:src="@{/js/car-viewer.js}"></script>
汽车查看器.js
function openModal(id)
{
$.ajax
({
url: "/car/" + id,
success: function (data) {
$("#carViewModal").modal("show");
}
});
}
carModalFragment.html
<div th:fragment="carModalFragment">
<div class="modal fade" id="carModal" tabindex="-1" role="dialog" aria-labelledby="carModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="carModalLabel" th:text="${selectedCar.name}"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" th:text="${selectedCar.cost}"></span>
</button>
</div>
...
</div>
</div>
</div>
</div>