SpringMVC 向jsp页面传递数据库读取到的值方法
作者:jingxian 时间:2022-03-29 00:51:15
在开发过程中,我们经常需要将数据库查询到的值放入jsp页面进行显示,在springmvc的controller中,我们采用request将数据传递过去。
思路:
1、在comtroller中调用service层的方法获取数据库的数据,并且将其通过modelandview的addObject方法放置到域中
2、在jsp页面中通过jsp标签进行读取
开发controller.java文件:
//查询所有数据到页面显示
@RequestMapping("/dataAll")
public ModelAndView dataAll()throws Exception{
//调用Service层进行数据查找
List<DataList> dataLists = dataService.finDataAll();
ModelAndView modelAndView = new ModelAndView();
//将数据放到request中
modelAndView.addObject("datasList", dataLists);
//指定视图
modelAndView.setViewName("/data/dataList");
return modelAndView;
}
如上所示,程序通过如下这条代码:
//将数据放到request中
modelAndView.addObject("datasList", dataLists);
将查询到的数据放置到request中。
开发jsp页面接收显示数据:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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>数据查询列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/data/dataAll.action" method="post">
数据查询:
<table width="10%" border=1>
<tr>
<td><input type="text" name="num" placeholder="编号"/><br/></td>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
数据列表:
<table width="100%" border=1>
<tr>
<td>编号</td>
<td>湿度</td>
<td>温度</td>
<td>二氧化碳</td>
<td>粉尘</td>
<td>操作</td>
</tr>
<c:forEach items="${datasList }" var="data">
<tr>
<td>${data.num }</td>
<td>${data.hum }</td>
<td>${data.tem }</td>
<td>${data.co }</td>
<td>${data.fc }</td>
<td><a href="${pageContext.request.contextPath }/data/editDatas.action?num=${data.num}" rel="external nofollow" rel="external nofollow" >修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
页面通过如下代码获取信息,进而进行循环显示:
<c:forEach items="${datasList }" var="data">
<tr>
<td>${data.num }</td>
<td>${data.hum }</td>
<td>${data.tem }</td>
<td>${data.co }</td>
<td>${data.fc }</td>
<td><a href="${pageContext.request.contextPath }/data/editDatas.action?num=${data.num}" rel="external nofollow" rel="external nofollow" >修改</a></td>
</tr>
</c:forEach>
这里的dataList即为通过controller传递过来的Object的名字,包含dataList数据。
来源:https://blog.csdn.net/qq_28796345/article/details/72596854
标签:SpringMVC,jsp,传递,数据库,读取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解spring cloud eureka注册中心
2023-11-10 17:54:10
![](https://img.aspxhome.com/file/2023/3/59163_0s.png)
使用Maven Helper解决Maven插件冲突的方法
2023-11-08 06:07:05
![](https://img.aspxhome.com/file/2023/1/58751_0s.jpg)
Java继承Thread类创建线程类示例
2023-08-04 20:27:08
Java使用HttpUtils实现发送HTTP请求
2021-06-11 07:08:39
![](https://img.aspxhome.com/file/2023/4/61034_0s.jpg)
java日期格式化SimpleDateFormat的使用详解
2023-08-25 03:22:15
![](https://img.aspxhome.com/file/2023/0/58460_0s.png)
java在linux本地执行shell命令的实现方法
2023-11-14 02:31:33
![](https://img.aspxhome.com/file/2023/9/59569_0s.png)
简单了解4种分布式session解决方案
2023-08-09 11:45:49
![](https://img.aspxhome.com/file/2023/7/57717_0s.png)
SpringBoot使用Thymeleaf自定义标签的实例代码
2023-11-24 21:41:29
flutter日期选择器 flutter时间选择器
2023-09-22 04:50:15
![](https://img.aspxhome.com/file/2023/8/90158_0s.jpg)
详解Android Flutter中SliverAppBar的使用教程
2023-06-23 12:11:27
![](https://img.aspxhome.com/file/2023/7/97867_0s.jpg)
SpringBoot2之PUT请求接收不了参数的解决方案
2023-08-23 01:32:07
![](https://img.aspxhome.com/file/2023/2/58202_0s.png)
Android实现简易浏览器遇到问题的解决方法
2023-06-15 21:47:56
IntelliJ IDEA Project窗口的一些设置详解
2023-11-09 04:54:44
![](https://img.aspxhome.com/file/2023/6/58926_0s.png)
老生常谈Java String字符串(必看篇)
2023-06-20 19:56:20
如何将maven源改为国内阿里云镜像
2023-07-25 13:47:33
![](https://img.aspxhome.com/file/2023/9/57899_0s.png)
教你怎么用Java数组和链表实现栈
2023-10-29 08:13:57
![](https://img.aspxhome.com/file/2023/0/58720_0s.png)
java字符串抉择
2023-08-08 14:20:05
![](https://img.aspxhome.com/file/2023/7/58077_0s.png)
Java操作Mongodb数据库实现数据的增删查改功能示例
2023-11-25 00:02:05
Java利用移位运算将int型分解成四个byte型的方法
2023-11-09 08:25:00
![](https://img.aspxhome.com/file/2023/4/59334_0s.png)
SpringBoot预加载与懒加载实现方法超详细讲解
2021-08-31 11:39:13