泰州公司做网站,免费seo提交工具,网站的权限设置,网店erp1#xff0e;JSTL概述
JSTL#xff08;JSP Standard Tag Library)#xff0c;JSP标准标签库#xff0c;可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能。jstl出现的目的同el一样也是要代替jsp页面中的脚本代码。JSTL标准标准标签库有5个子库#xff0c;但随着发展…1JSTL概述
JSTLJSP Standard Tag Library)JSP标准标签库可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能。jstl出现的目的同el一样也是要代替jsp页面中的脚本代码。JSTL标准标准标签库有5个子库但随着发展目前常使用的是他的核心库
标签库标签库的URI前缀Corehttp://java.sun.com/jsp/jstl/corecI18Nhttp://java.sun.com/jsp/jstl/fmtfmtSQLhttp://java.sun.com/jsp/jstl/sqlsqlXMLhttp://java.sun.com/jsp/jstl/xmlxFunctionshttp://java.sun.com/jsp/jstl/functionsfn
2JSTL导入
需要导两个包
jstl.jar
standar.jar使用jsp的taglib指令导入核心标签库
% taglib urihttp://java.sun.com/jsp/jstl/core prefixc %3JSTL核心库的常用标签
1c:if test””标签 其中test是返回boolean的条件 2c:forEach标签 使用方式有两种组合形式 示例
1遍历List的值
2遍历List的值
3遍历MapString,String的值
4遍历MapString,User的值
5遍历MapUser,MapString,User的值 entry.key-----User entry.value------ListString,User
//forEach.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
% page importjava.util.* %
% taglib urihttp://java.sun.com/jsp/jstl/core prefixc %
% page importbeyond.domain.* %
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body%//模拟ListString strListListString strList new ArrayListString();strList.add(beyond0);strList.add(beyond1);strList.add(beyond2);strList.add(beyond3);request.setAttribute(wsq, strList);//遍历ListUser的值ListUser userList new ArrayListUser();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);//遍历MapString,String的值MapString,String strMap new HashMapString,String();strMap.put(name, hjj);strMap.put(age, 32);strMap.put(addr, 香港);strMap.put(email, hjjqq.com);session.setAttribute(strMap, strMap);//遍历MapString,User的值MapString,User userMap new HashMapString,User();userMap.put(user1, user1);userMap.put(user2, user2);session.setAttribute(userMap, userMap); /* //遍历MapUser,MapString,User的值MapUser,MapString,User mapMap new HashMapUser,MapString,User();mapMap.put(user1,userMap);mapMap.put(user2,userMap);session.setAttribute(mapMap, mapMap); */%h1取出strList的数据/h1c:forEach items${wsq} varstr${str}br//c:forEachh1取出userList的数据/h1c:forEach items${qb} varuser user的name:${user.name}------------user的password:${user.password} br//c:forEachh1取出strMap的数据/h1c:forEach items${strMap} varentry${entry.key}---------------${entry.value}br//c:forEachh1取出userMap的数据/h1c:forEach items${userMap} varentry ${entry.key}--------------${entry.value.id}--------------${entry.value.name}--------------${entry.value.password}br//c:forEach%-- h1取出mapMap的数据/h1c:forEach items${mapMap} varenter${entry.key.key}--------------${entry.value.value.id}--------------${entry.value.name}--------------${entry.value.password}br//c:forEach --%/body
/html//userLogin.jsp
% page languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-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-equivContent-Type contenttext/html; charsetUTF-8
titleInsert 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 languagejava contentTypetext/html; charsetUTF-8pageEncodingUTF-8%
% taglib urihttp://java.sun.com/jsp/jstl/core prefixc %!-- 通过taglib导入jstl的core库 --
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetUTF-8
titleInsert title here/title
/head
body!-- jstl标签经常会和el表达式(从域中取东西)配合使用 --%request.setAttribute(count, 1014);%c:if test${count1014}nice you are right/c:if!-- test代表的是一个返回boolean的表达式条件要么是true要么是false如果是true进入if标签内部但是注意这里没有else标签 --c:if test11beyond/c:ifc:if test1!1sq/c:if!-- 第一种组合方式 --!-- forEach模拟for(int i0;i5;i){System.out.println(i);} --c:forEach begin0 end5 vari${i}br//c:forEach!-- 也就是从0开始到5结束每次将值赋值给i然后通过el表达式输出i的值每输出一个就换行 --!-- 第二种组合方式 --!-- forEach模拟增强for循环 productList----ListProductfor(Product product : productList){}System.out.println(product.getPname());--!-- items:是一个集合或者数组 var:代表集合中的某一个元素 --c:forEach items${productList} varpro${pro.pname}/c:forEach/body
/html