目录
前言:
1、使用开发工具:eclipse
2、导包:使用手动导包,非maven
3、使用数据库:MySQL
4、jdk版本:1.8
5、tomcat版本:7.0
6、使用表格:dept表(deptno,dname,loc)
最终效果:实现在jsp展示查询结果
一、controller层
package com.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.entity.Dept;
import com.service.dept.DeptService;
@Controller
@RequestMapping(value = "dept")
public class DeptController {
@Resource
private DeptService deptservice;
@RequestMapping("getAll")
public String getAll(HttpSession session) {
System.out.println("=======");
List<Dept> list = deptservice.getAll();
System.out.println(list.size());
// 添加至session会话中
session.setAttribute("deptlist", list);
// 返回至相应的jsp页面
return "showdept";
}
}
二、jsp代码
<%@page import="com.entity.Dept"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr>
<td>部门编号</td>
<td>部门名称</td>
<td>部门地址</td>
</tr>
<%
//1.得到图书信息集合
List<Dept> list = (List)session.getAttribute("deptlist");
//2.迭代图书信息集合
for (int i = 0; i < list.size(); i++) {
%>
<tr>
<td><%=list.get(i).getDeptNo()%></td>
<td><%=list.get(i).getDeptName()%></td>
<td><%=list.get(i).getLOC()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>