天天看点

Jstl与Select的默认值

jsp页面需要有语句<%@ taglib uri=" http://java.sun.com/jstl/core" prefix="c" %> 工程中需要导入jstl的jar包

1.单个变量型(有默认值)

             <select name="end_time_year">

                     <c:choose>

                           <c:when test="${a_year == '2007'}">

                               <option value="2007" selected>2007</option>

                           </c:when>

                           <c:otherwise>

                               <option value="2007">2007</option>

                           </c:otherwise>

                     </c:choose>

                     <c:choose>

                           <c:when test="${a_year == '2008'}">

                               <option value="2008" selected>2008</option>

                           </c:when>

                           <c:otherwise>

                               <option value="2008">2008</option>

                           </c:otherwise>

                     </c:choose>

             </select>

注: 其中a_year是从后台传过来的。如果传过来的值是2007,则选择下拉框中2007默认被选中。 2.遍历list(有默认值)              <select name="type">

                   <logic:iterate id="type2" name="type2List" scope="request">

                       <c:choose>

                           <c:when test="${type2 == a_fault_type}">

                               <option value="<c:out value="${a_fault_type}"/>" selected><c:out value="${a_fault_type}"/></option>

                           </c:when>

                           <c:otherwise>

                               <option value="<c:out value="${type2}"/>"><c:out value="${type2}"/></option>

                           </c:otherwise>

                      </c:choose>

                   </logic:iterate>

             </select> 注:name="type2List" scope="request"表示从后台action中通过request的方式传值。

    <c:choose>         <c:when>

        </c:when>

        <c:otherwise>

        </c:otherwise>

     </c:choose>

    可以理解为

       if(){

       }else{

       } 3.遍历list(无默认值)           <select name="type">

                   <logic:iterate id="type2" name="type2List" scope="request">

                            <option value="<c:out value="${type2}"/>"><c:out value="${type2}"/></option>

                   </logic:iterate>

           </select>

上一篇: jstl为空
下一篇: JSTL操作集合