Spring Boot中值到视图的映射实现及代码示例咨询
Spring Boot中值到视图的映射实现方法
Hey there! Let me break down how value-to-view mapping works in Spring Boot using your provided code as a practical example.
1. Controller层:准备数据并关联视图
First, let's go through your Controller method line by line to understand its role:
@RequestMapping("/viewemp") public ModelAndView viewemp(ModelAndView model){ List<Employee> list=new ArrayList<Employee>(); list.add(new Employee("Rahul","abc@gmail.com","9985213655","Hyderabad",new java.sql.Date(System.currentTimeMillis()))); model.addObject("list",list); model.setViewName("viewemp"); return model; }
@RequestMapping("/viewemp"): This annotation binds HTTP requests to the/viewemppath directly to this method.ModelAndView model: Spring automatically injects this object—it’s a dual-purpose container that holds both your business data (the model) and the name of the view you want to render.- Creating the
Employeelist: This is just simulating data you’d normally fetch from a database or service layer. model.addObject("list", list): We store the employee list in the model using the keylist—this key is what the view will use to access the data later.model.setViewName("viewemp"): We tell Spring which view to render. Depending on your template engine (like Thymeleaf or JSP), Spring will look for a file namedviewemp.html(Thymeleaf) orviewemp.jspin the appropriate resources folder.- Returning
model: Spring processes this object, passes the data to the specified view, and renders the final HTML response for the client.
2. View层:渲染Model中的数据
You provided a partial HTML table—let’s complete it with Thymeleaf syntax (Spring Boot’s most common template engine) to show how to display the employee data dynamically:
<table border="2" width="70%" cellpadding="2"> <tr> <th>Name</th> <th>Email</th> <th>Phone</th> <th>City</th> <th>Join Date</th> </tr> <!-- Loop through each employee in the "list" from the model --> <tr th:each="emp : ${list}"> <td th:text="${emp.name}"></td> <td th:text="${emp.email}"></td> <td th:text="${emp.phone}"></td> <td th:text="${emp.city}"></td> <td th:text="${emp.joinDate}"></td> </tr> </table>
th:each="emp : ${list}": This is Thymeleaf’s loop directive—it iterates over everyEmployeeobject in thelistwe passed from the Controller, assigning each one to the variableempfor the current iteration.th:text="${emp.name}": This directive fetches thenameproperty of the currentempobject and inserts its value into the<td>element. The same logic applies to the other employee fields.
3. Core Principles to Remember
- ModelAndView is the bridge between your Controller and View in Spring MVC—it combines your data model and the view identifier into one object.
- The Controller’s responsibility is to prepare business data, attach it to the ModelAndView, and specify which view to use.
- The view engine (like Thymeleaf) uses the view name to locate the correct template, then pulls data from the Model to dynamically render the final HTML.
内容的提问来源于stack exchange,提问作者Sanjay Kumar
相关产品推荐
相关产品推荐

