Spring Controller传递到JSP的List无法显示,请求协助排查问题
Hey there! Let's dig into why your List isn't showing up in the JSP even though it has one item. I'll walk through common fixes based on typical Spring + JSP pitfalls:
1. Double-check how you're passing the List from the Controller
First, make sure you're using Model to add the List to the view context—this is the standard way Spring passes data to JSPs. The key name you use here must exactly match what you reference in the JSP (it's case-sensitive!).
Example correct Controller code:
@GetMapping("/your-endpoint") public String loadData(Model model) { List<YourEntity> yourList = yourService.fetchData(); // Confirmed size is 1 model.addAttribute("displayList", yourList); // "displayList" is the key to reference in JSP return "your-jsp-view-name"; }
Avoid storing the List in session unless you explicitly need it there—if you do, you'll need to access it via ${sessionScope.displayList} in the JSP.
2. Verify your JSTL setup and loop code
To iterate over the List, you need the JSTL core taglib. First, add this declaration at the top of your JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Then, use <c:forEach> to loop through the List. Add a check for empty data to debug easily:
<!-- First, confirm the List is actually reaching the JSP --> <p>List size in JSP: ${displayList.size()}</p> <c:if test="${not empty displayList}"> <div class="data-container"> <c:forEach items="${displayList}" var="item"> <!-- Replace "propertyName" with your entity's actual field (e.g., item.id, item.name) --> <p>Item value: ${item.propertyName}</p> </c:forEach> </div> </c:if> <c:if test="${empty displayList}"> <p>No data available</p> </c:if>
If the size shows 1 but the loop doesn't render content, the issue is likely with how you're accessing the entity's properties (see next point).
3. Ensure your entity has proper getter methods
Spring EL (the ${...} syntax) relies on public getter methods to access object properties. If your entity class doesn't have getters, the JSP can't read the values.
Example correct entity:
public class YourEntity { private String name; // Private field is fine // Required public getter for EL to access "name" public String getName() { return name; } // Setter is optional unless you're writing data back to the entity public void setName(String name) { this.name = name; } }
If you try to use ${item.name} without a getName() method, it will return nothing—making it look like the List isn't rendering.
4. Check your View Resolver configuration (Spring MVC)
Make sure your Spring configuration points to the correct JSP location. For example, if you're using XML config:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean>
If your Controller returns "your-view", Spring looks for /WEB-INF/views/your-view.jsp. A wrong path here could lead to rendering a blank or default page instead of your intended JSP.
Go through these steps one by one—start with verifying the List size in the JSP to confirm it's being passed correctly, then work down to the property access. That should help you pinpoint the issue!
内容的提问来源于stack exchange,提问作者Prashant Pandey

