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

Spring MVC中如何从下拉列表获取选项名称而非选项值

Hey there! Let's sort out that problem where your JSP is showing the numeric value (like 1 or 2) instead of the friendly "Yes"/"No" label when echoing the user's saved selection. Here are a few straightforward, maintainable approaches to fix this:

1. Use an Enum to Centralize Options

This is my go-to because it keeps your option values and labels in one place, making updates easier later. First, create an enum:

public enum ConfirmationStatus {
    YES(1, "Yes"),
    NO(2, "No");

    private final int value;
    private final String label;

    ConfirmationStatus(int value, String label) {
        this.value = value;
        this.label = label;
    }

    public int getValue() { return value; }
    public String getLabel() { return label; }

    // Helper to fetch label from the saved numeric value
    public static String getLabelByValue(int value) {
        for (ConfirmationStatus status : values()) {
            if (status.value == value) {
                return status.label;
            }
        }
        return String.valueOf(value); // Fallback for invalid values
    }
}

Then in your controller, when preparing the model for the echo page:

// Get the saved value from your entity/database
int savedConfirmationValue = yourEntity.getIsConfirmed();
// Add the friendly label to the model
model.addAttribute("confirmationLabel", ConfirmationStatus.getLabelByValue(savedConfirmationValue));

Finally, in your JSP, just display the label directly:

<p>Confirmed: ${confirmationLabel}</p>
2. Bind a Map to the Form (For Dropdown Echo)

If you need to echo the selection in another dropdown (not just plain text), use Spring's form tag with an items map. First, add the options map to your model in the controller:

Map<Integer, String> confirmationOptions = new HashMap<>();
confirmationOptions.put(1, "Yes");
confirmationOptions.put(2, "No");
model.addAttribute("confirmationOptions", confirmationOptions);
// Pass your form object (which has the saved isConfirmed value) to the model too
model.addAttribute("yourForm", yourFormObject);

Then in the JSP, use the form select tag with the items attribute:

<form:select path="isConfirmed" items="${confirmationOptions}" />

Spring will automatically match the saved value (1/2) to the corresponding label ("Yes"/"No") and display the correct option as selected.

3. Plain JSTL Conditional (Quick Fix)

If you just need a simple, one-off solution without extra Java code, use JSTL's <c:choose> tag in your JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<p>Confirmed: 
    <c:choose>
        <c:when test="${yourForm.isConfirmed == 1}">Yes</c:when>
        <c:when test="${yourForm.isConfirmed == 2}">No</c:when>
        <c:otherwise>${yourForm.isConfirmed}</c:otherwise>
    </c:choose>
</p>

This works great for small, static option sets.

4. Add a Label Getter to Your Entity

Another clean option is to add a helper method directly to your entity class that returns the friendly label:

public class YourEntity {
    private int isConfirmed;

    // Standard getter and setter for isConfirmed

    public String getIsConfirmedLabel() {
        return switch(isConfirmed) {
            case 1 -> "Yes";
            case 2 -> "No";
            default -> String.valueOf(isConfirmed);
        };
    }
}

Then in your JSP, just call this getter:

<p>Confirmed: ${yourEntity.isConfirmedLabel}</p>

All these methods will solve your problem—pick the one that fits your project's structure best!

内容的提问来源于stack exchange,提问作者Beginner

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:28:25