1

I want to take an id as input by the user and pass to the controller to get the data of particular id

it's working when I pass id manually in URL -- http://localhost:8080/student/1


<form th:action="@{{student}/{id}}" th:object="${Student}" method="post">
                    Roll Number:<br>
                    <input type="text" th:field="*{id}"><br>
                    <br><br>
                    <input class="button" type="submit" value="Submit">
 </form>

@GetMapping(value = "/student/{id}")
    public Optional<Student> getStudentDetail(@PathVariable int id){
        return studentRepository.findById(id) ;
    }

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Jun 19 11:04:22 IST 2019 There was an unexpected error (type=Internal Server Error, status=500). An error happened during template parsing (template: "class path resource [templates/student.html]")

3 Answers 3

1

change as AmirBll said, Also, you have to change your controller HTTP method from @GetMapping(value = "/student/{id}") to @PostMapping(value = "/student/{id}") as form data submission is POST method you declared in the form.

Sign up to request clarification or add additional context in comments.

Comments

1

In the form attribute you have used method="post" while in the controller class you are using @GetMapping. Use this

<input type="text" th:field="*{id}">

instead of

<input type="text" th:="*{id}"><br>

Also make getter / setter of id attributes in the Student class

5 Comments

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'Student' available as request attribute
try with th:object="${student}" instead of th:object="${Student}". Also the Student class must be under package which used in @ComponnetScan
can you please confirm that Student form bean is being loaded in the application context
No, it is not loaded in the application context
load this Form Bean class in the applicationContext, looks like the issue related to the non-availability of bean
0

you should use th:field for <input>:

<input type="text" th:field="*{id}">

2 Comments

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Wed Jun 19 11:27:55 IST 2019 There was an unexpected error (type=Internal Server Error, status=500). Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "student" - line 16, col 40)
by using <input type="text" th:field="*{id}">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.