jsp 与jstl

区别:

jsp=html+java web混合
语法较为复杂难以编写

如if语句编写如下:

<body>
<%
if(flag){
%>
<h2>欢迎 <%=name %></h2>
<%} %>

</body>

需要将java语句套在<% %>中

jstl书写较为简便if语句如下:

<body>
jstltest <br><br>
<c:if test="${flag== true}">
    欢迎${name}
   <c:out value="${name}" /> 
</c:if>

</body>

jstl的使用:

导入jar包

 <dependency>
 <groupId>jstl</groupId>
 <artifactId>jstl</artifactId>
 <version>1.2</version>
 </dependency>

foreach

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>
	<c:forEach var="i" begin="1" end="10" step="1">   
第<c:out value="${i}" />行
<br>
	</c:forEach>

	<hr>


</body>
</html>

choose

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!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>
<c:choose>
       <c:when test="${age<12}">
              小学阶段
       </c:when>
       <c:when test="${age>=12&&age<16}">
              中学阶段
       </c:when>
	   <c:when test="${age>=16&&age<30}">
              大学阶段
       </c:when>
       <c:otherwise>
             你猜
       </c:otherwise>
</c:choose>


</body>
</html>

if

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!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>
jstltest <br><br>
<c:if test="${flag== true}">
    欢迎${name}
   <c:out value="${name}" /> 
</c:if>

</body>
</html>

jsp 与jstl

上一篇:C3P0数据库连接池


下一篇:MySQL数据库管理员密码忘记如何修改?