You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 /viewemp path 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 Employee list: 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 key list—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 named viewemp.html (Thymeleaf) or viewemp.jsp in 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 every Employee object in the list we passed from the Controller, assigning each one to the variable emp for the current iteration.
  • th:text="${emp.name}": This directive fetches the name property of the current emp object 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:17:36