sanaka
sanaka

Reputation: 37

Unable to fetch values to View(JSP) from Controller using @ModelAttribute in Spring Boot

I'm unable to fetch the values from Controller(Home.java) to View (home_view.jsp) in Spring Boot.

home.jsp

  <body>
        <div class="container mt-10">
            <form action="processHome" method="post">
         
            <div class="form-group">
                <label for="emp_Id">Enter Employee Id</label>
                <input type="number" placeholder="Enter Id" name="id">
            </div>
          
            <div class="form-group">
                <label for="Name">Enter Employee Name</label>
                <input type="text" placeholder="Enter Name" name="name">
            </div>
          
            <div class="form-group">
                <label for="Password">Enter Employee Password</label>
                <input type="text" placeholder="Enter Password" name="password">
            </div>
          
            <button type="submit">Submit</button>
        </form> 

Home.java

 @Controller
    public class Home {
        
        @RequestMapping("home")
        public String index() {
            return "home";
        }
        
        @RequestMapping(path="processHome",method=RequestMethod.POST)
        public String homeForm(@ModelAttribute Employee emp,  Model model) {
            System.out.println(emp);  // Here in 'emp' I'm getting the values in my console
            return "home_view";
        }
    }

Employee.java

public class Employee {
    
    private int id;
    private String name;
    private String password;
   
   //getters & setters
   //toString()

}

home_view.jsp

<body>
     Model : ${emp.id} ${emp.name} ${emp.password}
</body>

OUTPUT

I am successfully redirected to the home_view.jsp but unable to fetch data. As in my console, I'm getting the values from the View and when I am using ModelAndView then also I am getting those values but when I'm using @ModelAttribute then only data isn't fetching into the view.

Upvotes: 0

Views: 370

Answers (1)

sanaka
sanaka

Reputation: 37

I've got the data on views but really not sure that Model object name is creating such differences

public String homeForm(@ModelAttribute Employee employee,  Model model) 

Upvotes: 0

Related Questions