标签:img category tle ems jsp tst Servle 接收 相关
代码:
控制层代码
1 package com.day03.station.controller; 2 3 import com.day03.station.model.Ticket; 4 import com.day03.station.service.impl.TicketService; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import java.io.IOException; 12 import java.util.List; 13 14 /** 15 * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html 16 * 疑问咨询wx:851298348 17 */ 18 @WebServlet("/list") 19 public class TicketListServlet extends HttpServlet { 20 //业务对象 21 private TicketService ticketService=new TicketService(); 22 /** 23 * 查询车票列表 24 * @param req 25 * @param resp 26 * @throws ServletException 27 * @throws IOException 28 */ 29 @Override 30 public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 31 //1.接收参数 32 //2.调用业务方法 33 List<Ticket> list = ticketService.queryAll(); 34 req.setAttribute("list",list); 35 //3.控制跳转 36 req.getRequestDispatcher("/WEB-INF/views/ticketList.jsp").forward(req,resp); 37 38 } 39 }
jsp代码
1 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> 2 <html> 3 <head> 4 <title>车票列表</title> 5 </head> 6 <body> 7 我是车票列表页面 8 ${list} 9 <%-- 10 表格标签 11 行 <tr> 12 列 <td> 13 --%> 14 <table align="center" border="1"> 15 <tr> 16 <td>编号</td> 17 <td>start_station</td> 18 <td>stop_station</td> 19 <td>start_time</td> 20 <td>ticket_price</td> 21 </tr> 22 <tr> 23 <td>${list[0].id}</td> 24 <td>${list[0].startStation}</td> 25 <td>${list[0].stopStation}</td> 26 <td>${list[0].startTime}</td> 27 <td>${list[0].ticketPrice}</td> 28 </tr> 29 <tr> 30 <td>${list[1].id}</td> 31 <td>${list[1].startStation}</td> 32 <td>${list[1].stopStation}</td> 33 <td>${list[1].startTime}</td> 34 <td>${list[1].ticketPrice}</td> 35 </tr> 36 <tr> 37 <td>${list[2].id}</td> 38 <td>${list[2].startStation}</td> 39 <td>${list[2].stopStation}</td> 40 <td>${list[2].startTime}</td> 41 <td>${list[2].ticketPrice}</td> 42 </tr> 43 44 </table> 45 </body> 46 </html>
1.使用c标签前的准备
为了完全在JSP中消除Java代码,学习JAVA的标签库:
Java标签库:
标准标签库(JSTL).(SUN预先提供好的,我们使用的.)
以后学习MVC框架(Struts2/SpringMVC)的时候,都会学习一套标签.
=====================================================================
准备:
1:准备JSTL相关的jar(Tomcat根/webapps/examples/WEB-INF/lib).
jstl.jar standard.jar
el-api.jar jsp-api.jar
2:使用JSTL的JSP页面中,引人标签库:
比如引人核心JSTL:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
3:注意:有的idea没有添加c标签库,所以必须手动添加
2.ide配置
jsp代码:
1 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 3 <html> 4 <head> 5 <title>车票列表</title> 6 </head> 7 <body> 8 我是车票列表页面 9 10 <%-- 11 表格标签 12 行 <tr> 13 列 <td> 14 --%> 15 <table align="center" border="1"> 16 <tr> 17 <td>编号</td> 18 <td>start_station</td> 19 <td>stop_station</td> 20 <td>start_time</td> 21 <td>ticket_price</td> 22 </tr> 23 <c:forEach items="${list}" var="ticket"> 24 <tr> 25 <td>${ticket.id}</td> 26 <td>${ticket.startStation}</td> 27 <td>${ticket.stopStation}</td> 28 <td>${ticket.startTime}</td> 29 <td>${ticket.ticketPrice}</td> 30 </tr> 31 </c:forEach> 32 33 </table> 34 </body> 35 </html>
标签:img category tle ems jsp tst Servle 接收 相关
原文地址:https://www.cnblogs.com/newAndHui/p/9009693.html