0

如果用户在 JSP 中没有值,则在 JSP 中显示 Spring 下拉列表。我试图找出如何编写一些 Spring 代码,如果用户在从服务器返回的 borough 字段中有值,则不会显示以下下拉列表?

<form:select path="borough">
 <form:option value="Staten Island">Staten Island</form:option>
 <form:option value="Queens">Queens</form:option>
 <form:option value="Brooklyn">Brooklyn</form:option>
 <form:option value="Bronx">Bronx</form:option>
 <form:option value="Manhattan">Manhattan</form:option>
</form:select>  
4

1 回答 1

2

您可以使用 jstl 条件标记来完成此操作

<c:if test="${empty borough}">
    <form:select path="borough">
    <form:option value="Staten Island">Staten Island</form:option>
    <form:option value="Queens">Queens</form:option>
    <form:option value="Brooklyn">Brooklyn</form:option>
    <form:option value="Bronx">Bronx</form:option>
    <form:option value="Manhattan">Manhattan</form:option>
    </form:select>  
</c:if>

根据评论:

您可以使用 if else 来实现c:choose

<c:choose>
    <c:when test="${empty borough}">
        <form:select path="borough">
        <form:option value="Staten Island">Staten Island</form:option>
        <form:option value="Queens">Queens</form:option>
        <form:option value="Brooklyn">Brooklyn</form:option>
        <form:option value="Bronx">Bronx</form:option>
        <form:option value="Manhattan">Manhattan</form:option>
        </form:select>
    </c:when>
    <c:otherwise>
        This is the else block, you can have multiple when clause so it will become if else if else code
    </c:otherwise>
</c:choose>

一些文档

于 2012-05-22T17:31:31.817 回答