生活随笔
收集整理的這篇文章主要介紹了
JSTL技术
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.JSTL概述
JSTL(JSP Standard Tag Library),JSP標準標簽庫,可以嵌入在jsp頁面中使用標簽的形式完成業(yè)務(wù)邏輯等功能。jstl出現(xiàn)的目的同el一樣也是要代替jsp頁面中的腳本代碼。JSTL標準標準標簽庫有5個子庫,但隨著發(fā)展,目前常使用的是他的核心庫
標簽庫標簽庫的URI前綴 Core http://java.sun.com/jsp/jstl/core c I18N http://java.sun.com/jsp/jstl/fmt fmt SQL http://java.sun.com/jsp/jstl/sql sql XML http://java.sun.com/jsp/jstl/xml x Functions http://java.sun.com/jsp/jstl/functions fn
2.JSTL導入
需要導兩個包:
jstl.jar
standar.jar
使用jsp的taglib指令導入核心標簽庫
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3.JSTL核心庫的常用標簽
1)<c:if test=””>標簽 其中test是返回boolean的條件 2)<c:forEach>標簽 使用方式有兩種組合形式: 示例:
1)遍歷List的值
2)遍歷List的值
3)遍歷Map<String,String>的值
4)遍歷Map<String,User>的值
5)遍歷Map<User,Map<String,User>>的值 entry.key-----User entry.value------List<String,User>
//forEach.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="beyond.domain.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html>
< head>
< meta http-equiv = " Content-Type" content = " text/html; charset=UTF-8" >
< title> Insert title here
</ title>
</ head>
< body> <%//模擬List
< String> strListList
< String> strList = new ArrayList
< String> ();strList.add("beyond0");strList.add("beyond1");strList.add("beyond2");strList.add("beyond3");request.setAttribute("wsq", strList);//遍歷List
< User> 的值List
< User> userList = new ArrayList
< User> ();User user1 = new User();//list集合中的第一個元素user1.setId(2);user1.setName("qibao");user1.setPassword("wsq");userList.add(user1);//list集合中的第二個元素User user2 = new User();user2.setId(3);user2.setName("yanyu");user2.setPassword("wsq");userList.add(user2);application.setAttribute("qb", userList);//遍歷Map
< String,String> 的值Map
< String,String> strMap = new HashMap
< String,String> ();strMap.put("name", "hjj");strMap.put("age", "32");strMap.put("addr", "香港");strMap.put("email", "hjj@qq.com");session.setAttribute("strMap", strMap);//遍歷Map
< String,User> 的值Map
< String,User> userMap = new HashMap
< String,User> ();userMap.put("user1", user1);userMap.put("user2", user2);session.setAttribute("userMap", userMap); /* //遍歷Map<User,Map
< String,User> >的值Map<User,Map
< String,User> > mapMap = new HashMap<User,Map
< String,User> >();mapMap.put(user1,userMap);mapMap.put(user2,userMap);session.setAttribute("mapMap", mapMap); */%>
< h1> 取出strList的數(shù)據(jù)
</ h1> < c: forEach items = " ${wsq}" var = " str" > ${str}
< br/> </ c: forEach> < h1> 取出userList的數(shù)據(jù)
</ h1> < c: forEach items = " ${qb}" var = " user" > user的name:${user.name}------------user的password:${user.password}
< br/> </ c: forEach> < h1> 取出strMap的數(shù)據(jù)
</ h1> < c: forEach items = " ${strMap}" var = " entry" > ${entry.key}---------------${entry.value}
< br/> </ c: forEach> < h1> 取出userMap的數(shù)據(jù)
</ h1> < c: forEach items = " ${userMap}" var = " entry" > ${entry.key}--------------${entry.value.id}--------------${entry.value.name}--------------${entry.value.password}
< br/> </ c: forEach> <%--
< h1> 取出mapMap的數(shù)據(jù)
</ h1> < c: forEach items = " ${mapMap}" var = " enter" > ${entry.key.key}--------------${entry.value.value.id}--------------${entry.value.name}--------------${entry.value.password}
< br/> </ c: forEach> --%>
</ body>
</ html>
//userLogin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import= "beyond.domain.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html>
< head>
< meta http-equiv = " Content-Type" content = " text/html; charset=UTF-8" >
< title> Insert title here
</ title>
</ head>
< body> <% User user = new User();user.setId(1014);user.setName("wsq");user.setPassword("wsq");session.setAttribute("user", user);/* 把user放到session域當中 */%>
</ body>
</ html>
//jstl.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html>
< head>
< meta http-equiv = " Content-Type" content = " text/html; charset=UTF-8" >
< title> Insert title here
</ title>
</ head>
< body> <%request.setAttribute("count", 1014);%>
< c: if test = " ${count==1014}" > nice you are right!!
</ c: if> < c: if test = " 1==1" > beyond
</ c: if> < c: if test = " 1!=1" > sq
</ c: if> < c: forEach begin = " 0" end = " 5" var = " i" > ${i}
< br/> </ c: forEach> < c: forEach items = " ${productList}" var = " pro" > ${pro.pname}
</ c: forEach> </ body>
</ html>
總結(jié)
以上是生活随笔 為你收集整理的JSTL技术 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。