0

In my application the user clicks a link to edit an exam of their choice.

When they click the link it should open the editExam.html and the URL contains the ID of the exam they selected.

When I click the link the URL is correct (it contains the exam ID) however it doesn't display the editExam.html page it just displays my main page (the default localhost page)

enter image description here

allSubjects.html (the page where the user selects which exam to edit)

    <h4> exams:</h4>
<div th:each="exam : ${subject.exam}"> 
<h4 th:text="${exam.examTitle}"/>
    <a th:href="@{/editExam.html(id=${exam.examId})}">Edit Exam</a> 

editExam.html (this is the page I can't display)

<form action="#"th:action="@{/editExam.html{examId}}"  th:object="${exam}" method="put">


    <table>
        <tr>
        <td> Exam Title:</td>
         <td><input type="text" th:field="*{examTitle}" th:text="${exam.examTitle}"/></td>
    <!--    <td th:if="${#fields.hasErrors('examTitlee')}" th:errors="*{examTitle}">error message</td>  --> 
        </tr>  
        <tr>
            <td> Exam grade worth </td>
            <td><input th:field="*{examGradeWorth}" /></td>
        <!--    <td th:if="${#fields.hasErrors('examGradeWorth')}" th:errors="*{examGradeWorth}">error message</td> -->
            </tr>  
            <tr>
                <td>examGradeAchieved</td>
                <td><input th:field="*{examGradeAchieved}"/></td>
                </tr>
            <tr>
                <td><button type="submit">Submit post</button></td>
                </tr>
    </table>
    </div>
</form>  

My controller:

//Update an exam
@RequestMapping(value = "/editExam.html{examId}", method = { RequestMethod.POST, RequestMethod.PUT})
public String editExam(@ModelAttribute("exam") @PathVariable(value = "examId")Long examId, @RequestBody Exam exam,Model model, BindingResult result) {

            examRepository.findOne(examId);
            model.addAttribute("examTitle", exam.getExamTitle());
            model.addAttribute("examGradeWorth", exam.getExamGradeWorth());
            model.addAttribute("examGradeAchieved", exam.getExamGradeAchieved());


        exam.setExamTitle(exam.getExamTitle());
         exam.setExamGradeWorth(exam.getExamGradeWorth());
         exam.setExamGradeAchieved(exam.getExamGradeAchieved());

          examRepository.save(exam);

    return "editExam";
}

This code was displaying the editExam page before I added "RequestMethod.POST" to the controller.

Jill
  • 431
  • 4
  • 13
  • 33

2 Answers2

1

Try to change

<a th:href="@{/editExam.html(id=${exam.examId})}">Edit Exam</a> 

to

<a th:href="@{/exam/{id}(id=${exam.examId})}">Edit Exam</a> 

and in your Controller

@RequestMapping(value = "/editExam.html{examId}", method = { RequestMethod.POST, RequestMethod.PUT})

to

@RequestMapping(value = "/exam/{examId}", method = { RequestMethod.POST, RequestMethod.PUT})
niclaszll
  • 380
  • 2
  • 3
  • 15
  • Thank you for your answer! I implemented that but unfortunately it still didn't work. I am going to try display the editExam.html with a separate method with a get request and see how I get on. – Jill Mar 22 '18 at 17:02
  • 1
    [This project](https://github.com/st-tu-dresden/videoshop) could help you if you are stuck. The catalog with discs is almost the same as your subjects and exams. – niclaszll Mar 22 '18 at 18:40
1

This url does not look correct. It should be /editExam.html?id=1, without a /.

I noticed that you are mixing request params and path variables. In your controller you expect @PathVariable(value = "examId") but in the view you are specifying request parameter instead of path variable.

Check this post: @RequestParam vs @PathVariable

Michal Lonski
  • 877
  • 1
  • 11
  • 20