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>