Solution 1 :

First you correct the action spelling in 2nd form in your hello.html also use th:action for form action as you have used Thymeleaf. Also, you have not closed your name="text1" in your first form.
i.e

<input type="text" name="text1 th_value="${text1_value}"/>

change it to

<input type="text" name="text1" th_value="${text1_value}"/>
   <form th_action="@{/hello}" method="post">
            Enter a word:
            <input type="text" name="text1" th_value="${text1_value}">
            <input type="submit" value="Click">
        </form>
    
        <br/>
    
        <form  th_action="@{/hello/db}" method="post" >
            Enter Employee ID:
            <input type="number" name="text2" th_value="${text2_value}" >
            <input type="submit" value="Click" >
        </form>

Problem :

I am trying to get an Employee’s info from a database with a user-input ID number in the html page hello.html and display the info in another html file helloResponseDB.html, but it seems that when I submit the ID number, it leads me to different html page helloResponse.html, which displays a user-input word. Please could you help me?

Controller Code:

    import org.springframework.stereotype.Controller;
    
    import org.springframework.ui.Model;
    
    import org.springframework.web.bind.annotation.GetMapping;
    
    import org.springframework.web.bind.annotation.PostMapping;
    
    import org.springframework.web.bind.annotation.RequestParam;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    @Controller
    
    public class HelloController {
    
        @Autowired
        private HelloService helloService;
        
        @GetMapping("/hello")
        public String getHello() {
            return "hello";
        }
        
        @PostMapping("/hello") 
        public String 
        postRequest(@RequestParam(name="text1",required=false)String str, Model model) {
            model.addAttribute("sample",str);
            return "helloResponse";
        }
        
        @PostMapping("/hello/db")
        public String 
        postDbRequest(@RequestParam(name="text2")String str, Model model) {
            int id = Integer.parseInt(str);
            Employee employee = helloService.findOne(id);
            model.addAttribute("id",employee.getEmployeeId());
            model.addAttribute("name",employee.getEmployeeName());
            model.addAttribute("age",employee.getAge());
            return "helloResponseDB";
        }
    }

hello.html:

    <!DOCTYPE html>
    <html xmlns_th="http://www.thymeleaf.org">
    <head>
        <metacharset="UTF8"></meta>
        <title>Hello World</title>
    </head>
    <body>
        <h1>Hello World</h1>
    
        <form method="post" action="/hello">
            Enter a word:
            <input type="text" name="text1 th_value="${text1_value}"/>
            <input type="submit" value="Click"/>
        </form>
    
        <br/>
    
        <form method="post" actioin="/hello/db">
            Enter Employee's ID:
            <input type="number" name="text2" th_value="${text2_value}"/>
            <input type="submit" value="Click"/>
        </form>
    
    <body>
    </html>

Comments

Comment posted by tknkrtl

theres a typo in your form html “actioin” , also how do you post requestparam ?

Comment posted by BlueSafari

I really not sure about that question bc I just write following a book…

Comment posted by BlueSafari

Thank you for the correction. I just changed as you suggested.

Comment posted by BlueSafari

Yes! It worked! Was too excited to mention it.

By