JSTL標籤結合EL表示式——java web前端瘦身器
阿新 • • 發佈:2019-02-02
JSP Standard Tag Library 標準標籤庫:
Model 1 開發模式採用jsp 內嵌java 程式碼的方式實現業務邏輯與前臺頁面的互動,這樣雖然使得前臺呼叫業務簡單,卻也導致jsp多重職責,在系統龐大的專案中,難以繼續應用。Model 2則在業務邏輯manager與jsp間建立一層servlet,作為業務層與jsp顯示層的橋樑,將jsp中大部分獲值的java程式碼寫到servlet中,以減輕jsp負擔;但這樣做也不可避免的需要在jsp中寫入形似<%=user.getName() %>這樣的程式碼獲值。
JSTL標籤庫的使用便可徹底代替上述java程式碼,以類似於html標籤的形式,結合EL表示式,獲取資料。
EL表示式的使用:
使用EL表示式時無需引入任何jar包,只要jsp/servlet容器實現了J2EE1.4 、Servlet2.4、JSP2.0規範即可。
具體應用:
Servlet中doGet方法如下,分別用EL表示式獲取servlet傳遞的不同型別數值:
//結構 Group group =new Group(); group.setName("動力節點"); User user =new User(); user.setAge(23); user.setGroup(group); user.setUserName("zhang"); //map Map map=new HashMap(); map.put("k1","v1"); map.put("k2", "v2"); request.setAttribute("map", map); request.setAttribute("user",user); //陣列 String[] strArray=new String[]{"a","b","c"}; request.setAttribute("straary", strArray); //物件陣列 User[] users=new User[10]; for(int i=0;i<users.length;i++) { users[i]=new User(); users[i].setUserName("鍾艾伶_"+i); } //list 集合 List userList=new ArrayList(); for(int i=0;i<10;i++) { User userss=new User(); userss.setUserName("李四"+i); userList.add(userss); } request.setAttribute("userList",userList); request.setAttribute("users",users); request.setAttribute("v2",""); request.setAttribute("v3",new ArrayList()); request.setAttribute("v4","12"); request.setAttribute("v5",null); //分發器 將request、response傳到jsp中 request.getRequestDispatcher("/jstl_el.jsp").forward(request, resp);
EL獲取字串:
hello(普通jsp指令碼獲值):<%=request.getAttribute("hello")%><br>
hello(el表示式獲值): ${hello}<br>
EL獲取物件:【物件.屬性(get方法後的串,拼成get方法)】
EL獲取map:<li>結構 .進行導航 存取器</li> 姓名:${user.userName} <br> 年齡:${user.age}<br> 所屬組:${user.group.name}<br>
<li>map</li><br>
map.k1:${map.k1 }<br>
map.k2:${map.k2 }<br>
EL獲取字串陣列:
<li>字串陣列</li><br>
strArray[1]:${strarry[1] }
strArray[4]:${strarry[4] }
EL獲取物件陣列:
<li>物件陣列</li>
users[5].username:${users[5].username}<br>
EL獲取List集合:
<li>list.採用[].下標</li>
userList[6].username:${userList[6].username}<br>
EL表示式對運算子的支援:
1+1=${1+1 }<br>
1/1=${1/1 } <br>
10 % 2=${10 % 2 } <br>
常用的jstl標籤及其使用:
使用JSTL標籤不同於EL表示式可直接使用即可,JSTL使用前需將jstl.jar 和standard.jar拷貝到WEN-INF/lib包下,重啟tomcat後,在相應的jsp頁面採用taglib指令,引入標籤庫方可。
<%@taglib uri=" http://java.sun.com/jsp/jstl/core"prefix="c"%>
JSTL標籤結合EL表示式的使用:
對應servlet中doGet方法:
//普通字串
request.setAttribute("hello", "helloWorld");
//HTML字串
request.setAttribute("hello","<font color=red>北京歡迎您!</font>" );
//條件控制 c:if
request.setAttribute("v1","10");
request.setAttribute("v2","2");
//條件控制 c:choose c:when c:otherwise
request.setAttribute("userList", new ArrayList());
//迴圈標籤 c:foreach
Group group=new Group();
group.setName("動力節點");
List userrs=new ArrayList();
for(int i=0; i<10;i++)
{
User userr=new User();
userr.setUserName("zhang"+i);
userr.setAge(23+i);
userr.setGroup(group);
userrs.add(userr);
}
request.setAttribute("userrs",userrs);
request.getRequestDispatcher("/jsp_el.jsp").forward(request, resp);
簡單輸出:c:out
<li>採用c:out標籤</li>
hello使用標籤:<c:out value="123"/>
hello使用標籤:<c:out value="${hello} "/>
hello使用標籤:<c:out value="${hello123} ">沒有值</c:out>
hello使用標籤:<c:out value="${welcome} escapXml="true" />
c:set c:remove
<li>c:set c:remove</li>
<c:setvalue="root" var="userid"></c:set>
userid:${userid}
<c:removevar="userid"></c:remove>
userid:${userid}
邏輯判斷標籤: c:if \c:choose c:when c:otherwise
<li>條件控制標籤 c:if</li>
<c:iftest="${v1 lt v2 }">v1小於v2</c:if>
<li>c:choose c:when c:otherwise</li>
<c:choose>
<c:when test="${v1 gt v2}">v1 大於 v2</c:when>
<c:otherwise> v1 小於 v2</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${emptyuserList}">沒有滿足條件的使用者</c:when>
<c:otherwise>存在使用者資料</c:otherwise>
</c:choose>
迴圈標籤: c:foreach
<li>jsp指令碼顯示</li>
<tableborder="1">
<tr>
<td>使用者名稱</td>
<td>年齡</td>
<td>所屬組</td>
</tr>
<%
ListuserList=(List)request.getAttribute("userrs");
if(userList ==null || userList.size()==0){
%>
<tr>
<tdcolspan="3">沒有符合條件的資料</td>
</tr>
<%
}else{
for(Iteratoriter=userList.iterator();iter.hasNext())
{
Useruser=(User)iter.next();
}
}
%>
<tr>
<td><%=user.getUserName()%></td>
<td><%=user.getAge()%></td>
<td><%=user.getGrop()%></td>
</tr>
</table><span style="font-family: 'Microsoft YaHei'; font-size: 14px; background-color: rgb(255, 255, 255);"> </span>
<li>迴圈標籤 c:foreach</li>
<tableborder="1">
<tr>
<td>使用者名稱</td>
<td>年齡</td>
<td>所屬組</td>
</tr>
<c:choose>
<c:whentest="${empty userrs }">沒有符合條件的使用者</c:when>
<c:otherwise>
<c:forEachitems="${userrs}" var="users" varstatus="vs">
<c:choose>
<c:whentest="${vs.count mod 2 == 0 }"><trbgcolor="red"></c:when>
<c:otherwise><tr></c:otherwise>
</c:choose>
<tr>
<td>${users.userName}</td>
<td>${user.age}</td>
<td>${user.group}</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
糾錯標籤:c:catch
<li>普通java</li>
<%
<span style="white-space:pre"> </span>try{
Integer.parseInt("dfadf");
<span style="white-space:pre"> </span>}catch(NumberFormatException e){
e.printStackTrace();
}
%>
<li>c:catch</li>
<c:catchitems="msg">
<%Integer.parseInt("dfadf"); %>
</c:catch>
${msg }
介面引入傳參:
<li>c:import 引入某jsp頁面</li>
<c:importurl="http://localhost:8099/jstl_el"></c:import>
<li>c:url c:param</li>
<c:urlvalue="http://localhost:8099/jstl_el">
<c:param name="userId"value="${userrs.userName }"></c:param>
<c:param name="age"value="${userrs.age }"></c:param>
</c:url>