jstl语句变量如何写?

xxcw 2007-07-31 07:38:45
<c:if test="${customercity.code}==<%=selectCity%>">
selectCity是一个request的变量
...全文
243 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
RatKing 2007-08-10
  • 打赏
  • 举报
回复
你是对的,是这样写。
这符合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>
xxcw 2007-08-07
  • 打赏
  • 举报
回复
再顶一次,希望有人帮助我
xxcw 2007-08-01
  • 打赏
  • 举报
回复
谢谢楼上的,
我有一个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>
xxcw 2007-08-01
  • 打赏
  • 举报
回复
上面的写法可以吗?
penghuasheng 2007-07-31
  • 打赏
  • 举报
回复
<c:if test="${customercity.code==selectCity}">

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧