【发布时间】:2019-07-19 18:17:11
【问题描述】:
基本上我正在创建一个赞成/反对票系统,现在我在 HTML 中创建了两个按钮:
<button type="button" name="upvote"></button>
<br>
<input type="number" th:value = "${commentValue}" disabled>
<br>
<button type="button" name="downvote"></button>
第一个带有名称属性的按钮 - "upvote" 和第二个带有 "downvote" 的按钮,现在我想点击按钮并相应地更改 commentValue,如果用户点击 upvote,请发表评论value 必须增加,并且在 downvote 时它必须减少
我发现一些主题说要实现 ActionListener、创建 JButton 对象等,但没有更简单的方法吗?
这是我现在的代码,按钮什么都不做。 :
控制器:
private int numberValue = 0;
@GetMapping("/check")
public String nuberCheck(Model model){
model.addAttribute("commentValue", numberValue);
return "numberCheck";
}
@PostMapping("/check")
public String upvote(Model model, @RequestParam String action){
if (action == "upvote"){
numberValue++;
model.addAttribute("commentValue", numberValue);
}else if (action == "downvote"){
numberValue --;
model.addAttribute("commentValue", numberValue);
}
return "numberCheck";
}
号码检查:
<form action="#" th:action="@{/check}" method="post">
<button type="button" name="action" th:value="upvote" th:text = "upvote"></button>
<br>
<input type="number" th:value = "${commentValue}" disabled>
<br>
<button type="button" name="action" th:value = "dowvnote" th:text = "dowvnote"></button>
</form>
已修复
将按钮类型更改为“提交”,然后在控制器中:
@PostMapping("/check")
public String check(Model model,@RequestParam String action) {
System.out.println(action);
if (action.equals("upvote")) {
numberValue++;
model.addAttribute("numberValue", numberValue);
}
if (action.equals("dowvnote")) {
numberValue--;
model.addAttribute("numberValue", numberValue);
}
return "numberCheck";
}
@RequestParam String action“action”是按钮的name属性,“save”和“cancel”是“value”属性:)希望对你有帮助
【问题讨论】:
-
我想这可能会有所帮助?至少对于最初的想法...stackoverflow.com/questions/31401669/…
标签: java html spring-boot thymeleaf