如何将数据库中的值经过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前缀,一旦找到与 ...
随机推荐
- GC参考手册 —— GC 调优(基础篇)
GC调优(Tuning Garbage Collection)和其他性能调优是同样的原理.初学者可能会被 200 多个 GC参数弄得一头雾水, 然后随便调整几个来试试结果,又或者修改几行代码来测试.其 ...
- 个人简历模板web
根据自己以前使用的简单简历表格,对其进行了web前端还原,也算对自己初步学习知识的一个小小的记录. 下面是简历预览效果,很简洁的那种: 代码中没什么太困难的地方,主要记录下自己遇到的几个小问题吧: 1 ...
- 『取巧』VS2015试用期过后 继续试用
背景: 个人电脑 安装的 VS2015 Community 社区版. 一直用得挺好,都忘了要登录. 直到近来,30天试用期过 —— VS弹窗:要登录用户名.密码 才能继续使用. 但是,输入了无数次 邮 ...
- C# 语音合成
1. 引用System.Speech 2. 通过SpeechSynthesizer类朗读文本 new SpeechSynthesizer().SpeakAsync("我们都是好孩子We're ...
- spring boot 文件上传大小限制
错误信息 : Spring Boot:The field file exceeds its maximum permitted size of 1048576 bytes. 解决方法一:在启动类添加如 ...
- Web前端-Vue.js必备框架(五)
Web前端-Vue.js必备框架(五) 页面组件,商品列表组件,详情组件,购物车清单组件,结算页组件,订单详情组件,订单列表组件. vue-router 路由 vuex 组件集中管理 webpack ...
- Percona XtraBackup 关于 MySQL备份还原的详细测试
一. Percona XtraBackup 的优点. (1)无需停止数据库进行InnoDB热备: (2)增量备份MySQL: (3)流压缩传输到其它服务器: (4)在线移动表: (5)能够比较容易地创 ...
- CentOS_关机与重启命令详解
Linux centos关机与重启命令详解 Linux centos重启命令: 1.reboot 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过 ...
- Windows API编程(SDK编程)配置VS2017——出现LNK 2019错误的win32项目如何解决
最近刚入门SDK编程,在 我终于知道为什么windowsApi学的人这么少了 这篇文章中,确实发现了这样的问题,我的教程使用VS2013->Windows桌面->win32,就诞生了能使用 ...
- 20190415 - iOS11 无法连接到 App Store 的解决办法
问题:更新 iOS 11 后,打开 App Store 提示: 无法连接至 app store 解决: 进入 iOS 系统[设置][iTunes Store 与 App Store],退出当前登录用户 ...