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

如何在JSP中基于List实现JSTL?附后端Java代码

Fixing JSTL Rendering for Questions & Corresponding Options

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.

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:

  1. First, update your EmployeeBean class 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
}
  1. Modify your backend loop to attach each question's options to the question bean itself, instead of overwriting a single optionList in 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, and optionText with the actual field names from your EmployeeBean that 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:37:39