如何在JSP中基于List实现JSTL?附后端Java代码
Hey there! I spot the issue in your current code right away—let's break this down and fix it properly.
The Problem with Your Backend Code
Right now, in your loop, you're overwriting the optionList attribute in the model every time you process a new question. By the end of the loop, the model only holds the options for the last question you processed. That's why your JSTL can't render all questions with their respective options.
Step 1: Adjust Your Backend to Link Questions & Options
Instead of storing options as a separate, overwritten list, you should associate each question with its own options directly. Here's how to do it:
- First, update your
EmployeeBeanclass to add a property for storing options (plus getter/setter methods):
public class EmployeeBean { // Your existing fields (like sk_question_id, question text, etc.) private List<EmployeeBean> options; // Add this property to hold question-specific options // Getter and Setter for the options list public List<EmployeeBean> getOptions() { return options; } public void setOptions(List<EmployeeBean> options) { this.options = options; } // Your other existing getters and setters }
- Modify your backend loop to attach each question's options to the question bean itself, instead of overwriting a single
optionListin the model:
List<EmployeeBean> questionList = adao.displayQuestions(eBean, functional_id); // Loop through each question to bind its options to the bean for (EmployeeBean question : questionList) { String q_id = question.getSk_question_id(); System.out.println("QUESTION ID :" + q_id); List<EmployeeBean> optionList = adao.displayOptions(eBean, q_id); question.setOptions(optionList); // Attach options directly to the question } model.addObject("questionList", questionList); // Only add the full question list once
Step 2: Render with JSTL in Your JSP
Now that each question has its own embedded options, you can nest two <c:forEach> tags to render all questions and their corresponding options correctly.
First, make sure you have the JSTL core taglib declared at the top of your JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Then use this code to render the questions and options (adjust field names to match your actual EmployeeBean properties):
<div class="question-section"> <c:forEach var="question" items="${questionList}"> <div class="question-card"> <!-- Display question content (replace with your actual field names) --> <h4>Q: ${question.questionText}</h4> <span class="question-id">ID: ${question.sk_question_id}</span> <!-- Render options for this specific question --> <div class="options-group"> <c:forEach var="option" items="${question.options}"> <div class="option-item"> <input type="radio" name="answer_${question.sk_question_id}" value="${option.optionId}"> <label>${option.optionText}</label> </div> </c:forEach> </div> </div> </c:forEach> </div>
Quick Notes
- Replace
questionText,optionId, andoptionTextwith the actual field names from yourEmployeeBeanthat hold the question content and option data. - Using
name="answer_${question.sk_question_id}"ensures radio buttons for each question are grouped correctly—so users can only select one option per question.
内容的提问来源于stack exchange,提问作者Manju

