<c:if test="${customercity.code==selectCity}">

解决方案 »

  1.   

    谢谢楼上的,
    我有一个option想当值等于selectCity时选中,下面这样为什么不行?
    <select name="city">
       <c:forEach var="customercity" items="${city}">
          <c:if test='${customercity.code==selectCity}'>
            <option value="<c:out value='${customercity.code}'/>" selected><c:out value="${customercity.name}"/></option>
          </c:if>
          <c:if test='${customercity.code!=selectCity}'>
            <option value="<c:out value='${customercity.code}'/>"><c:out value="${customercity.name}"/></option>
          </c:if>
        </c:forEach>
     </select>
      

  2.   

    你是对的,是这样写。
    这符合JSP1.2与JSTL1.0规范,可以在Tomcat4.1上运行。不过,也是有前提的:
    1. 已经使用过request.setAttribute("city", city)
    2. 已经使用过request.setAttribute("selectCity", selectCity)
    3. city是一个集合(比如List或Set)或对象数组,该集合内的那个Object(customercity)有code和name属性,并且有对应的public型的getCode()与getName()方法
    4. 我自己编程时,全是双引号,不必用单引号。此外,如果你的Web容器(比如Tomcat5.5)支持JSP2.0规范,还可以简化为:
    <select name="city">
       <c:forEach var="customercity" items="${city}">
          <c:if test="${customercity.code==selectCity}">
            <option value="${customercity.code}" selected>${customercity.name}</option>
          </c:if>
          <c:if test="${customercity.code!=selectCity}">
            <option value="${customercity.code}">${customercity.name}</option>
          </c:if>
        </c:forEach>
     </select>