JavaEE學習日誌持續更新----> 必看!JavaEE學習路線(文章總彙)
JSTL
JSTL引入
需求:有一個變量num,num值大於5,div標籤顯示紅色;否則,顯示藍色。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
--%>
<%
int num = 6;
if(num>5){
%>
<div style="color: red">文本是紅色</div>
<%
}else{
%>
<div style="color: blue">文本是藍色</div>
<%}%>
</body>
</html>
缺陷:java代碼和html混雜在一起,可維護性太低。
JSTL標籤庫
全名:JSP標準標籤庫
本質上是標籤,出現的目的,減少<%%>的出現
標籤庫:除了Core,其他都過時了。
引入jar包
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
if標籤
<c:if>
標籤:判斷,但沒有else
屬性:test,執行標籤體內容
條件爲真,則執行代碼;條件爲假,則什麼都不做。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
c:if標籤:判斷
屬性:test
執行標籤體內容
--%>
<%
pageContext.setAttribute("num",4);
%>
<c:if test="${num>=5}">
<div style="color: red">我是紅色</div>
</c:if>
<c:if test="${num<5}">
<div style="color: blue">我是藍色</div>
</c:if>
</body>
</html>
forEach標籤
<c:foreach>
標籤
屬性:
- begin=“開始值”
- end=“結束值”
- var=“定義變量” 保存的是循環中的值,值會自動存儲到pageContext域對象中
- step=“步長”
注意:循環包含開始值,也包含結束值
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach begin="1" end="5" var="i" step="2">
hello${pageScope.i}${i}<br>
</c:forEach>
</body>
</html>
增強foreach
作用:用於遍歷數組和集合
屬性:
- items=“遍歷的容器”
- var=“變量名” 變量表示數組中的元素,變量會自動保存在pageContext域對象中
- varStatus=“變量” 循環狀態對象
<%@ page import="com.itheima.domain.Address" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String[] str = {"a","b","c","d"};
pageContext.setAttribute("str",str);
%>
<%--
foreach標籤
屬性:
items="遍歷的容器"
var="變量名" 變量表示數組中的元素,變量會自動保存在pageContext域對象中
varStatus="變量" 循環狀態對象
--%>
<c:forEach items="${str}" var="s" varStatus="vs">
${s}${vs.count}<br>
</c:forEach>
<%
Address addr = new Address();
addr.setCity("北京");
addr.setArea("昌平");
Address addr2 = new Address();
addr2.setCity("天津");
addr2.setArea("武清");
List<Address> list = new ArrayList<Address>();
list.add(addr);
list.add(addr2);
pageContext.setAttribute("list",list);
%>
<c:forEach items="${list}" var="addr">
${addr.city}<br>
</c:forEach>
</body>
</html>
MVC設計模式
JavaEE經典三層架構
商品展示案例
需求:從數據庫中取出商品信息,顯示在頁面中。(未實現分頁)
dao層
package com.itheima.dao;
import com.itheima.domain.Product;
import com.itheima.utils.C3P0UtilsXML;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class ProductDao {
/*
方法查詢所有數據
數據表product,返回集合List<Product>
*/
public List<Product> findAll() throws SQLException {
QueryRunner qr = new QueryRunner(C3P0UtilsXML.getDataSource());
String sql = "select * from product";
List<Product> list = qr.query(sql, new BeanListHandler<Product>(Product.class));
return list;
}
}
service層
package com.itheima.service;
import com.itheima.dao.ProductDao;
import com.itheima.domain.Product;
import java.sql.SQLException;
import java.util.List;
public class ProductService {
/*
調用dao層方法,獲取結果集list,返回到Web層
*/
public List<Product> findAll(){
ProductDao dao = new ProductDao();
List<Product> list = null;
try {
list = dao.findAll();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
web層
package com.itheima.web;
import com.itheima.domain.Product;
import com.itheima.service.ProductService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet(urlPatterns = "/product")
public class ProductServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
調用業務層方法,獲取集合,集合存儲到request域對象,轉發回頁面
*/
ProductService service = new ProductService();
List<Product> list = service.findAll();
request.setAttribute("list",list);
request.getRequestDispatcher("/product_list.jsp").forward(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
前端
<%@ 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>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>會員登錄</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<!-- 引入自定義css文件 style.css -->
<link rel="stylesheet" href="css/style.css" type="text/css" />
<style>
body {
margin-top: 20px;
margin: 0 auto;
width: 100%;
}
.carousel-inner .item img {
width: 100%;
height: 300px;
}
</style>
</head>
<body>
<!-- 引入header.jsp -->
<jsp:include page="/header.jsp"></jsp:include>
<div class="row" style="width: 1210px; margin: 0 auto;">
<div class="col-md-12">
<ol class="breadcrumb">
<li><a href="#">首頁</a></li>
</ol>
</div>
<%--
從域對象中取出集合List
遍歷集合
遍歷的每個集合元素,存儲在了product對象中,product對象自動存儲在了
pageContext域
--%>
<c:forEach items="${list}" var="product">
<div class="col-md-2" style="height: 240px">
<a href="product_info.htm"> <img src="${product.pimage}"
width="170" height="170" style="display: inline-block;">
</a>
<p>
<a href="product_info.html" style='color: green'>${product.pname}</a>
</p>
<p>
<font color="#FF0000">商城價:${product.shop_price}</font>
</p>
</div>
</c:forEach>
</div>
<!--分頁 -->
<div style="width: 380px; margin: 0 auto; margin-top: 50px;">
<ul class="pagination" style="text-align: center; margin-top: 10px;">
<li class="disabled"><a href="#" aria-label="Previous"><span
aria-hidden="true">«</span></a></li>
<li class="active"><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">6</a></li>
<li><a href="#">7</a></li>
<li><a href="#">8</a></li>
<li><a href="#">9</a></li>
<li><a href="#" aria-label="Next"> <span aria-hidden="true">»</span>
</a></li>
</ul>
</div>
<!-- 分頁結束 -->
<!--商品瀏覽記錄-->
<div
style="width: 1210px; margin: 0 auto; padding: 0 9px; border: 1px solid #ddd; border-top: 2px solid #999; height: 246px;">
<h4 style="width: 50%; float: left; font: 14px/30px 微軟雅黑">瀏覽記錄</h4>
<div style="width: 50%; float: right; text-align: right;">
<a href="">more</a>
</div>
<div style="clear: both;"></div>
<div style="overflow: hidden;">
<ul style="list-style: none;">
<li
style="width: 150px; height: 216; float: left; margin: 0 8px 0 0; padding: 0 18px 15px; text-align: center;"><img
src="products/1/cs10001.jpg" width="130px" height="130px" /></li>
</ul>
</div>
</div>
<!-- 引入footer.jsp -->
<jsp:include page="/footer.jsp"></jsp:include>
</body>
</html>