JavaWeb基本知识点、JSTL标签

目的:尽量减小jsp页面java代码html

步骤:先导包(WebContent下的WEB—INF下的lib包下)java

第一步,导入核心jstl核心类库数组

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

c:out:把变量的值输出到浏览器,能够把特殊字符进行转化浏览器

对比jsp

<%
request.setAttribute("book", "<<java>>");
%>
book:${requestScope.book }
<br>
book:<c:out value="${requestScope.book }"></c:out>

能够使用默认值url

<c:out value="${aaaa }" default="获取为空"></c:out>

c:set 为域对象设置值code

<c:set var="hello" value="hpe" scope="request"></c:set>

c:remove:删除指定域对象里面的值htm

<c:set var="age" value="19" scope="request"></c:set>
<c:remove var="age" scope="request"/>

c:url:输出路径对象

<c:url value="/"></c:url>

c:if:不能实现elseblog

<c:set var="action" value="19" scope="request"></c:set>
<c:if test="${requestScope.action>18 }">成年了</c:if>

c:choose 判断至关于if elseif else

<c:set var="age1" value="70" scope="request"></c:set>
<c:choose>
<c:when test="${requestScope.age1>=18 }">你成年了</c:when>
<c:when test="${requestScope.age1>=40 }">你老了</c:when>
<c:otherwise>你仍是个小学生</c:otherwise>
</c:choose>

c:foreach :迭代语句(很是重要)

从10开始,50结束,间隔10

<c:forEach var="i" begin="10" end="50" step="10" varStatus="status">
${i },第${status.count }个
</c:forEach>

遍历一个数组

<%
String[] aa={"1A","2A","3A"};
pageContext.setAttribute("aa", aa);
%>
<c:forEach var="a" items="${aa }">
${ a}
</c:forEach>

list集合

<%
List<User> lsUser=new ArrayList<User>();
lsUser.add(new User("阿珂",18));
lsUser.add(new User("兰陵王",1800));
lsUser.add(new User("李白",180));
request.setAttribute("lsUser",lsUser);
%>
<c:forEach var="user" items="${requestScope.lsUser }">
${user.name }:${user.age }
</c:forEach>

<hr>

Map集合

<%
Map<String, User> map=new HashMap<String,User>();
map.put("aa", new User("宫本",345));
map.put("bb", new User("亚瑟",34));
request.setAttribute("map", map);
%>
<c:forEach var="m" items="${requestScope.map }">
key:${m.key }--${m.value.name }:${m.value.age }
</c:forEach>