JSP學習筆記(三)EL表達式 JSTL標籤

一、JSP語法:


1、JSP的註釋:             

作用:註釋Java腳本代碼             語法:<%--這是註釋--%>     

2、JSP的Java腳本表達式:             

作用:輸出數據到頁面上             語法:<%=表達式%>(實際上就是調用輸出流打印到頁面上) out.print(表達式);     

3、JSP中的Java腳本片段:(實際開發中,應做到JSP中不能出現一行Java腳本片段)             

作用:書寫Java代碼邏輯             語法:<%  語句1;   語句2;%>             

原理:其中的語句會原封不動的被服務器翻譯到對應的Servlet的_jspService方法中。     

4、JSP的聲明:             

作用:定義類的成員             語法:<%!                     Java代碼                         %>

 

二、EL表達式

對比:

採用表達式之前

     <%
		pageContext.setAttribute("msg", "pageContext");
		request.setAttribute("msg", "pageContext");
		session.setAttribute("msg", "pageContext");
		application.setAttribute("msg", "pageContext");
      %>
	pageContext<%=pageContext.getAttribute("msg") %><br/>
	request<%=request.getAttribute("msg") %><br/>
	session<%=session.getAttribute("msg") %><br/>
	application<%=application.getAttribute("msg") %>

採用表達式後:
 

	<%
		pageContext.setAttribute("msg", "pageContext");
		request.setAttribute("msg", "pageContext");
		session.setAttribute("msg", "pageContext");
		application.setAttribute("msg", "pageContext");
	%>
	pageContext: ${pageScope.msg}<br/>
	request: ${requestScope.msg}<br/>
	session: ${sessionScope.msg}<br/>
	application:${applicationScope.msg}
${msg} 

結合JSTL以及EL表達式練手項目:


拿之前shopping 作業來練手:

1.初始化項目。

2.實現思路:

效果:

部分源碼:

view:list

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
	<h2>訂單信息列表</h2>
	<a href="/orders/edit">新增</a>
	<table cellpadding="0" cellspacing="0" width="60%" border="1">
		<tr style="background-color: yellow">
			<th>序號</th>
			<th>用戶ID</th>
			<th>用戶真實姓名</th>
			<th>商品名稱</th>
			<th>商品價格</th>
			<th>添加的時間</th>
			<th>是否支付</th>
			<th>操作</th>
		</tr>
		<c:forEach items="${list}" var="ord" varStatus="vs">
			<tr style="background-color:${vs.count%2==0?'pink':''};">
				<td>${pageScope.ord.id}</td>
				<td>${pageScope.ord.users.id}</td>
				<td>${pageScope.ord.users.realName}</td>
	            <c:forEach items="${ord.details}" var="orderDetail" begin="0" end="0">
					<td>${pageScope.orderDetail.goods.goodsName}</td>
					<td>${pageScope.orderDetail.goods.price}</td>
				</c:forEach> 
				<td>${pageScope.ord.addDate}</td>
				<td>${pageScope.ord.isPay}</td>
				<td>修改</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

 

package com.shopping.web.servlet;

import java.io.IOException;
import java.util.List;

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 com.shopping.dao.OrderDao;
import com.shopping.dao.OrderDaoImpl;
import com.shopping.model.Orders;
@WebServlet("/orders/list")
public class OrdersListServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	private OrderDao dao;
	@Override
	public void init() throws ServletException {
		dao= new OrderDaoImpl();
	}
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//獲取所有的訂單信息
		List<Orders> list=dao.getAllByUser(2);
		req.setAttribute("list", list);
		req.getRequestDispatcher("/WEB-INF/views/orders/list.jsp").forward(req,resp);
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章