jquery - 使用 Thymaleaf 和数据库抛出异常的动态下拉菜单
问题描述
我正在尝试做一个动态下拉菜单。首先,我必须从菜单中选择一个 IDE,然后根据我的选择,这个 IDE 的版本应该会出现。
1.第一个下拉菜单
<select class="field-title" id="ide1" name="ide1" th:field="*{ide}" >
<option th:each="ideSelected : ${ides}"
th:value="${ideSelected.getIDEName()}"
th:text="${ideSelected.getIDEName()}"></option>
</select>
- 第二个下拉菜单
<select class="field-title" id="versionContent" name="versionContent" th:field="*{versions}" > </select>
- 脚本
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#ide1").change(function() {
sendAjaxRequest();
});
});
</script>
<script>
function sendAjaxRequest() {
let ide = $("#ide1").val();
$.get("/ides/" + ide, function( data ) {
// $("#versionContent").empty();
data.forEach(function(item) {
let option = "<option value = " + item + ">" + item + "</option>";
$("#versionContent").append(option);
});
});
}
</script>
- 控制器
@GetMapping(value = "/{ideName}")
public Set<String> getAllVersions(@PathVariable("ideName") String ideName){
IDE ide = ideService.getIdeByName(ideName);
return ide.getVersions().stream().map(Version::getVersion).collect(Collectors.toSet());
}
- 抛出错误
[THYMELEAF][http-nio-8080-exec-2] Exception processing template "ides/IntelliJ": An error happened during template parsing (template: "class path resource [templates/ides/IntelliJ.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/ides/IntelliJ.html]")
有人可以帮我解决这个问题吗?谢谢!
解决方案
要让 Spring 将控制器方法的响应序列化为 JSON,您需要使用@RestController
,或者@ResponseBody
在方法本身上使用@Controller
.
推荐阅读
- stm32 - Yocto:建筑历史结果
- javascript - 按下某个按钮时如何有条件地渲染多个平面列表
- docker - 如果主机关闭,Docker swarm 中是否有任何重启策略不会重启容器?
- c# - 获取登录用户信息
- opengl - 片段着色器中未使用的变量会改变渲染结果
- java - 当需要找出二叉树的直径时,通用树的直径代码不起作用
- powerbi - 计算power bi中年份变化百分比的差异
- ios - Searchcontroller 在 tableview 解除拖动和 view.endEditing 后给出黑屏
- c++ - 如何为我的窗口设置标题栏的背景?
- database - 单击集合时,MongoDB 地图集显示“未找到 404 文档”