如何将数据库中的值经过servlet传入到jsp页面,并且用EL表达式显示出值
方法一;通过id查询某一数据库表中具体的行,将值封装在相应的对象中,如下面的对象Notice
servlet中
String noticeId=request.getParameter("noticeId");
Notice displayEditnotice=publicnoticeservice.displayEditnotice(Integer.valueOf(noticeId));
request.setAttribute("list_displayEditnotice", displayEditnotice);
System.out.println("displayEditnotice="+displayEditnotice);
request.getRequestDispatcher("Editnotice.jsp").forward(request, response);
Editnotice.jsp页面
<form....>
<table>
<tr>
<td>标题:</td>
<td><input type="text" id="title" name="title" value="${list_displayEditnotice.getTitle()}"></td>-
</tr>
<tr>
<td>内容:</td>
<td><textarea cols="50" rows="10" name="context" style="border:#FF0000 1px solid;overflow:visible;">${list_displayEditnotice.getContext()}</textarea></td>
</tr>
<tr>
<td><input type="submit" value="保存公告"></td>
</tr>
</table>
</form>
dao中接口的实现方法
public Notice displayEditnotice(int noticeId) {
Notice notice=null;
String sql="select noticeId,title,context,publicerId,publicer,writeDate from notice where noticeId=?";
conn=super.getConnection();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, noticeId);
rs=pstmt.executeQuery();
while(rs.next()){
notice=new Notice();
notice.setNoticeId(rs.getInt("noticeId"));
notice.setTitle(rs.getString("title"));
notice.setContext(rs.getString("context"));
notice.setPublicerId(rs.getInt("publicerId"));
notice.setPublicer(rs.getString("publicer"));
notice.setWriteDate(rs.getTimestamp("writeDate"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
super.closeAll(conn, pstmt, stmt, rs);
}
return notice;
}
方法二:将我数据库中表的所有数据显示出来,则将每一行的值封装在List集合中,在jsp页面用<c:forEach><forEach>迭代显示出来
注意要加标签库:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
servlet中
List<Notice> displaynotice=publicnoticeservice.displaypublicnotice();
request.setAttribute("list_displaynotice",displaynotice);
request.getRequestDispatcher("displaypublicnotice.jsp").forward(request, response);
displaypublicnotice.jsp
<table border="0"cellspacing="0" cellpadding="0">
<tr>
<td style="width:50px;text-align: center">序号</td>
<td style="width:170px;text-align: center">标题</td>
<td style="width:400px;text-align: center">内容</td>
<td style="width:70px;text-align: center">发布人</td>
<td style="width:200px;text-align: center">发布时间</td>
<td style="width:120px;text-align: center">操作</td>
</tr>
<c:forEach items="${list_displaynotice}" var="notice" varStatus="i">
<tr style="background:#7FFFD4">
<td style="width:50px;text-align: center">${i.count} </td>
<td style="width:100px;text-align: center">${notice.title}</td>
<td style="text-align: center"><font style="font-size:12px;">${notice.context}</font></td>
<td style="text-align: center">${notice.publicer}</td>
<td style="width:100px;text-align: center">${notice.writeDate}</td>
<td style="text-align: center"><a href="PublicNoticeServlet?method=deletenotice¬iceId=${notice.noticeId}" target="middle" style="TEXT-DECORATION:none">删除</a>
|<a href="PublicNoticeServlet?method=editnotice¬iceId=${notice.noticeId}"
target="middle" style="TEXT-DECORATION:none">编辑</a>
</td>
</tr>
</c:forEach>
</table>
dao中接口的实现方法
private Connection conn=null;
private PreparedStatement pstmt=null;
private Statement stmt=null;
private ResultSet rs=null;
public List<Notice> displaypublicnotice() {
List<Notice> list=new ArrayList<Notice>();
Notice notice=null;
String sql="select noticeId,title,context,publicerId,publicer,writeDate from notice";
conn=super.getConnection();
try {
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
notice=new Notice();
notice.setNoticeId(rs.getInt("noticeId"));
notice.setTitle(rs.getString("title"));
notice.setContext(rs.getString("context"));
notice.setPublicerId(rs.getInt("publicerId"));
notice.setPublicer(rs.getString("publicer"));
notice.setWriteDate(rs.getTimestamp("writeDate"));
list.add(notice);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{ // 7。关闭连接
super.closeAll(conn, pstmt, stmt, rs);
}
return list;
}
如何将数据库中的值经过servlet传入到jsp页面,并且用EL表达式显示出值的更多相关文章
- jsp页面 如何通过el表达式获取request属性值
1. 我在一个超连接后加个参数如: http://localhost:8080/test/testjstl.jsp?pid=001 此时在jsp页面中,获取jsp传过来的pid的参数值 ...
- 后端model传入前端JSP页面中的值判断后再取值
所遇到的问题后端model传入前端JSP页面中的值通过foreach循环内要满足条件才能取值给Div中,我们知道jsp页面中可以直接用EL表达式取值,格式就是${"model中传来的数据&q ...
- Servlet转发到JSP页面的路径问题
一.现象与概念 1. 问题 在Servlet转发到JSP页面时,此时浏览器地址栏上显示的是Servlet的路径,而若JSP页面的超链接还是相对于该JSP页面的地址且该Servlet和该JSP页面不在同 ...
- JSP中的内置标记(JSP动作)和 EL表达式
一.JSP的内置标记(JSP动作) (一)JSP的内置标记都是以<jsp: 开始标记的 一般会用到<jsp:useBean/>,<jsp:setProperty/>,&l ...
- uni-app中当uni.navigateTo传的参数为object时,通过传递的不同参数,在显示单页面内通过v-if判断显示出对应的内容(可实现多页面效果)
通过uni-app中当uni.navigateTo传的参数为object时,通过传递的不同参数,在显示单页面内通过v-if判断显示出对应的内容(可实现多页面效果) 起始页跳转到对应页面,并传递参数(o ...
- 数据库中的记录通过servlet回显到jsp页面中(连接数据库或者查询參照:对数据进行增删改查)
我们常常会用到通过图书的名称来查询图书那么这种话我们也就会使用到从数据库中搜索出数据而且载入到自己的Jsp页面中 这种话我们须要将从数据库中获取到的数据放进响应中然后通过%=request.getAt ...
- struts2 request内幕 为什么在struts2用EL表达式可以取值
不知道大家有没有想过这样一个问题:为什么在action中的实例变量,没有使用request.setAttribute()方法将值添加到request范围内,却能在jsp中用EL表达式取出? 众所周知, ...
- 小峰servlet/jsp(4)EL表达式
一.EL表达式内置对象: 二.EL表达式访问4种范围属性: 寻找值的顺序: page-->request-->session-->application; 三.EL表达式接收请求参数 ...
- EL表达式获取属性值的原理
EL表达式获取对象属性的原理是这样的:以表达式${user.name}为例EL表达式会根据name去User类里寻找这个name的get方法,此时会自动把name首字母大写并加上get前缀,一旦找到与 ...
随机推荐
- ecs云服务器 mysql经常自动停止挂掉重启问题分析
我的ecs服务器为1g内存的配置,在部署了nginx,mysql,redis,node服务后跑起项目来,(mysql使用默认配置),每过几天便发现了经常会出现数据库自动停止挂掉,然后几分钟后重启的现象 ...
- es6学习笔记-class之继承
继承 上一篇学习了class的概念,在es5时,对象的继承有好几种,原型链继承,借用构造函数继承,组合继承,原型式继承,寄生式继承以及寄生组合式继承,都是按照函数的形式去集成的,现在class也有它的 ...
- 你真的了解字典(Dictionary)吗?
从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点. 为了便于描述,我把上面的那条线路称为线路1,下面的称为线路2. 思路 ...
- C# 将object对象转换为实体对象
C# 将object对象转换为实体对象.一共两种方法. 第一种方法,代码如下: /// <summary> /// 将object对象转换为实体对象 /// </summary> ...
- 安卓开发笔记(二十):利用夜神模拟器调试运行Android Studio的apk
一.首先来到夜神模拟器的安装目录下 如下图所示: 再把这整个文件夹添加到我们的windows环境变量里.然后再把android studio 和夜神模拟器都打开,注意必须同时打开而且不能够把夜神模拟器 ...
- App瘦身、性能优化总结
App瘦身 资源瘦身 使用tinypng压缩PNG图片.视频可以通过 Final cut等软件进行分辨率压缩.音频则降低码率即可. 非必须资源文件可以放到自己服务器上 启动图使用 LaunchScre ...
- 如何用GoldWave批量删除mp3文件开头65秒?
具体操作如下: 一.打开goldwave--文件--批处理 二.批处理设置: 添加需处理的文件或文件夹(右侧) 1.转换:设置转换格式. 2.处理: ...
- 关于OSError: [WinError 10038] 在一个非套接字上尝试了一个操作。
在使用socket的时候,写了一个while循环,就报错了.结果如下: OSError: [WinError 10038] 在一个非套接字上尝试了一个操作. 代码 import socket impo ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 467. [leetcode] Unique Substrings in Wraparound String
467. Unique Substrings in Wraparound String Implement atoi to convert a string to an integer. Hint: ...