当你点击搜索按钮时,我的html中出现了一个错误。
<form method="post">
<input type="number" name="numberProt">
<button type="submit">Найти</button>
</form>
在我的@Controller中
@Controller
public class ViolationController {
@Autowired
private ViolationServiceImpl violationService;
@GetMapping("/violations")
public String viewViolations(Model model) {
List<Violation> listViolations = violationService.findAll();
model.addAttribute("listViolations", listViolations);
return "violations";
}
@PostMapping("filterProt")
public String filterViolation(Integer numberProt, Model model) {
Iterable<Violation> protocols;
if (numberProt != null) {
protocols = violationService.findByNumProtocol(numberProt);
} else {
protocols = violationService.findAll();
}
model.addAttribute(protocols);
return "/violations";
}
}
请你帮忙。发生错误:已解决[org.springframework.web.HttpRequestMethodNotSupportedException: 不支持请求方法'POST']
你的表格不正确,它没有动作。只需将你的表单改正成这样即可。
<form action="/filterProt" method="post">
<input type="number" name="numberProt">
<button type="submit">Найти</button>
</form>
另外,你的控制器处理程序缺少@ModelAttribute注解。
@PostMapping("filterProt")
public String filterViolation(@ModelAttribute NumberModel numberModel, Model model) {
....
}
其中NumberModel是:
public class NumberModel{
private Integer numberProt;
public Integer getNumberProt() {
return numberProt;
}
public void setNumberProt(Integer numberProt) {
return this.numberProt = numberProt;
}
}