Witam jestem na etapie mojej małej aplikacji. Aplikacja ta polega na wyświetlaniu pytań z bazy danych. Chciałem zrobić metode do edytowania i usuwania pytań. Na stronie z wypisanymi wszystkimi pytaniami jest <input type="radio"> którego zaznaczenie miało by pobierać id pytania i zapomocą tego id wyświetlać pytanie do edycji/usunięcia. Problem polega na odebraniu tego id w @Controllerze. Cały kod jest na https://github.com/srslycpp/springboot/tree/master/Quiz
Z góry dziękuje
AllQuestions.html
<table>
<form action="#" th:action="@{/projects/quiz/editQuestion}" method="post">
<input type="radio" name="id" th:value="${question.id}" th:field="*{questions}"/>
<input value="Delete" name="delete" type="submit" />
</form>
<td colspan="2" th:utext="'Q: ' +${question.question}"></td>
</form>
</thead>
<tbody>
<tr>
<td th:utext=" 'odpA: ' +${question.odpA} "></td>
<td th:utext=" 'odpB: ' +${question.odpB} "></td>
</tr>
<tr>
<td th:utext=" 'odpC: ' +${question.odpC} "></td>
<td th:utext=" 'odpD: ' +${question.odpD} "></td>
</tr>
</tbody>
</tr>
</tfoot>
</table>
QuizController.java
@GetMapping("/projects/quiz/editQuestion")
public String alllQuestions(@ModelAttribute("question") Questions editQuestion,
Model model, HttpServletRequest request){
Long id = Long.parseLong(request.getParameter("id"));
//String string = request.getParameter("id");
System.out.println("<<<<<<<<<<<"+ id);
model.addAttribute("id", editQuestion.getId());
//System.out.println("???????????????????????????????"+id);
//questionService.editQuestion(id);
return "editQuestion"; }
@PostMapping(value = "/projects/quiz/editQuestion")
public String editQuestion (@ModelAttribute("questions") Questions editQuestion,
Model model,
HttpServletRequest request){
//Long id = Long.parseLong(request.getParameter("id"));
String string = request.getParameter("id");
System.out.println("<<<<<<<<<<<"+ string);
model.addAttribute("id", editQuestion.getId());
//model.addAttribute("editQuestion",questionService.editQuestion(id));
return "editQuestion";
}
Już na wiele sposobów próbowałem dlatego na razie wygląda to jak wygląda :P
Udało mi się rozwiązać swój problem dodajac @RequestParam("id") Long id do :
@PostMapping("/projects/quiz/editQuestion")
public String alllQuestions(@ModelAttribute("question") Questions editQuestion,@RequestParam("id") Long id