springmvc中的jsp頁面<c:if>的使用,springmvcjsp
首先看一個實實在在的項目文檔
<%@ 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這個變數名稱不為空白的話,就顯示出來,如果為空白的話,就不顯示。