首先看一个实实在在的项目文档
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!doctype html> <html lang="zh-cn"> <head> <base href="<%=basePath%>"> <meta charset="UTF-8"> <meta name="Keywords" content="种筹天下" /> <meta name="Description" content="种筹天下" /> <title>种筹天下</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,user-scalable=ture"> <link rel="stylesheet" href="m/css/other.css"> </head> <body> <!-- <div id="header"> <div class="header_left_btn"> <a href="javascript:window.history.go(-1);"><img src="m/images/back.png" height="28"></a> </div> <p class="header_tt">搜索结果</p> </div> --> <div id="main"> <div class="search_box"> <c:if test="${! empty testUnit.crop_name}"><p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【作物名称】</font></span><span class="search_right" style="width:65%">${testUnit.crop_name }</span></p></c:if> <c:if test="${! empty testUnit.service_item}"><p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【服务项目】</font></span><span class="search_right" style="width:65%">${testUnit.service_item } ${testUnit.service_item_son }</span></p></c:if> <c:if test="${! empty testUnit.method}"><p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【测试方法】</font></span><span class="search_right" style="width:65%">${testUnit.method }</span></p></c:if> <c:if test="${! empty testUnit.wait_data}"><p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【等待时间】</font></span><span class="search_right" style="width:65%">${testUnit.wait_data }</span></p></c:if> <c:if test="${! empty testUnit.cost}"><p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【费用】</font></span><span class="search_right" style="width:65%">${testUnit.cost }</span></p></c:if> <c:if test="${! empty testUnit.name}"><p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【单位名称】</font></span><span class="search_right" style="width:65%">${testUnit.name }</span></p></c:if> <c:if test="${! empty testUnit.address}"><p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【地址】</font></span><span class="search_right" style="width:65%">${testUnit.address }</span></p></c:if> <c:if test="${! empty testUnit.contactor}"><p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【联系人】</font></span><span class="search_right" style="width:65%">${testUnit.contactor }</span></p></c:if> <c:if test="${! empty testUnit.phone_number}"><p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【电话】</font></span><span class="search_right" style="width:65%">${testUnit.phone_number }</span></p></c:if> <c:if test="${! empty testUnit.introduction}"><p class="search_p clearfix"><span class="search_tt"><font color=green>【测试单位简介】</font></span></p> <p class="search_p clearfix"><span class="search_tt">${testUnit.introduction}</span></p></c:if> <p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【国家】</font></span><span class="search_right" style="width:65%">${testUnit.country }</span></p> <p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【地区】</font></span><span class="search_right" style="width:65%">${testUnit.area_province } ${testUnit.area_city } ${testUnit.area_county }</span></p> <c:if test="${! empty testUnit.picture}"><p class="search_p clearfix"><span class="search_tt"><font color=green>【相关图片】</font></span></p> <table width="90%" border="0" cellspacing="0" cellpadding="0" style="margin:12px auto 50px;"> <c:set value="${fn:split(testUnit.picture,';')}" var="pictures" /> <c:forEach items="${pictures }" var="pict" varStatus="s"> <tr> <td ><img width="100%" height="65%" src="${pict }"></td> </tr> <tr> <td> </td> </tr> </c:forEach> </table> </c:if> </div> </div> </body> </html>
总结一下其中的<c:if>的使用:
<c:if test="${! empty testUnit.crop_name}"><p class="search_p clearfix"><span class="search_left" style="width:30%"><font color=green>【作物名称】</font></span><span class="search_right" style="width:65%">${testUnit.crop_name }</span></p></c:if>
意思是,如果testUnit.crop_name这个变量名称不为空的话,就显示出来,如果为空的话,就不显示。
原文:http://blog.csdn.net/itbuluoge/article/details/44588813