11使用EL显示数据
1EL语法
①定义变量②变量存入作用域中③访问EL变量、对象属性、数组、运算
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
/* String username = "admin";
request.setAttribute("username", username); */
request.setAttribute("student.name", "张三");
ArrayList list = new ArrayList();
list.add("北京洪水");
list.add("热火夺冠");
request.setAttribute("list", list);
request.setAttribute("totalRecordCount", 8);
request.setAttribute("pageSize", 2);
%>
<!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>Insert title here</title>
</head>
<body>
${username } <br>
${requestScope["student.name"] }<br>
${list[1] }<br>
${totalRecordCount/pageSize }
</body>
</html>
2使用EL访问作用域
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
session.setAttribute("uid", "admin");
Object o = application.getAttribute("count");
if(o == null){
application.setAttribute("count", 1);
}
else{
application.setAttribute("count", (Integer)o+1);
}
%>
<!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>Insert title here</title>
</head>
<body>
用户名:${sessionScope.uid }<br>
网站访问次数:${count }
</body>
</html>
EL表达式获得参数
newsDetailView.jsp
<%@ page language="java" import="java.util.*,com.pb.news.entity.*" pageEncoding="UTF-8"%>
<jsp:useBean id="newsDao" beanName="com.pb.news.dao.impl.NewsDaoImpl" type="com.pb.news.dao.NewsDao" />
<jsp:useBean id="newsService" beanName="com.pb.news.service.impl.NewsServiceImpl" type="com.pb.news.service.NewsService" />
<jsp:setProperty property="newsDao" name="newsService" value="<%=newsDao%>"/>
<link href="<%=request.getContextPath() %>/css/common.css" rel="stylesheet" type="text/css" />
<%
String id = request.getParameter("id");
News news = newsService.getNewsById(Integer.parseInt(id));
request.setAttribute("news", news);
%>
传过来的ID:${param.id }
<div class="main-text-box-tbg">
<div class="main-text-box-bbg">
<div class="article-box">
<h1>${requestScope. news["title"] }</h1>
<div class="source-bar">发布者:<%=news.getAuthor() %> 分类:新闻信息 更新时间:<%=news.getCreateDate() %>" </div>
<div class="article-content">
<span class="article-summary"><b>摘要:<%=news.getSummary() %></b></span>
<img src="<%=request.getContextPath() %>/upload/<%=news.getPicPath() %>"/><br/>
<%=news.getContent() %>
</div>
</div>
</div>
</div>
12使用jstl实现新闻列表显示
1添加jstl.jar和standard.jar包 导入taglib指令
<span style="font-family: Arial, Helvetica, sans-serif;"> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %></span>
2使用cout显示变量值
<a href='newsDetailView.jsp?id=<%=news.getId() %>'><c:out value="${news.title }" escapeXml="true" /></a><!--true转义,false不转义-->
</td>
<td><c:out value="${news.author }" default="无" /></td><pre name="code" class="html"><!--<pre name="code" class="html">default设置默认值<span style="font-family: Arial, Helvetica, sans-serif;">--></span>
cset和cremove
<c:set var="uid" value="admin" scope="request" />
用户名:${uid } <br>
<c:remove var="uid" scope="request" />
用户名:${uid } <br>
3使用cforeach显示新闻列表
//每页显示的新闻列表
List<News> newsList=newsService.getPageNewsList(pageIndex, pageSize);
request.setAttribute("list", newsList);
%>
<tbody>
<c:forEach var="news" items="${list }" varStatus="status">
<tr class="admin-list-td-h2">
<td>
<%-- <a href='adminNewsView.jsp?id=2'><%=news.getTitle() %></a> --%>
<%-- <a href='newsDetailView.jsp?id=<%=news.getId() %>'>${news.title }</a> --%>
<a href='newsDetailView.jsp?id=${news.id }'><c:out value="${news.title }" escapeXml="true" /></a>
</td>
<td><c:out value="${news.author }" default="无" /></td>
<td>${news.createDate }</td>
<td>
<a href='adminNewsCreate.jsp?id=2'>修改</a>
<a href="javascript:if(confirm('确认是否删除此新闻?')) location='adminNewsDel.jsp?id=2'">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
4使用cif实现各行换色<c:if></c:if> status.count相当于for循环中的i
<tr <c:if test="${status.count%2==0 }">class="admin-list-td-h2"</c:if>>
5使用url链接新闻修改页面(优点:可以同时爸session的值一并传到服务器)
<a href='
<c:url value="newsDetailView.jsp">
<c:param name="id" value="${news.id }"></c:param>
</c:url>
'>修改</a>
6使用fmt formatdate标签格式化时间的显示
①导入taglib库指令
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
②替换el表达式
<td><fmt:formatDate value="${news.createDate }" pattern="yyyy-MM-dd"/></td>
7实现分页功能条的复用
1新建rollPage.jsp,剪切代码
2添加taglib指令,添加脚本
3使用<c:import>包含rollPage.jsp,并实现页面传参
4修改rollPage.jsp代码,接收参数,并替换原有代码
rollpage.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" %>
<script type="text/javascript">
function page_nav(frm,num){
frm.pageIndex.value=num;
frm.submit();
}
function jump_to(frm,pageno){
var regexp=/^\d+$/;
if(!regexp.test(pageno)){
alert("请输入 正确的数字!");
return false;
}else{
page_nav(frm,pageno);
}
}
</script>
<div class="page-bar">
<ul class="page-num-ul clearfix">
<li>共${param.totalCount }条记录 ${param.pageIndex }/${param.totalPage }页</li><pre name="code" class="html"><!--<pre name="code" class="html">${param.totalCount }
接收参数-->
<c:if test="${param.pageIndex>1 }" ><a href="javascript:page_nav(document.forms[0],1);">首页</a><a href="javascript:page_nav(document.forms[0],${param.pageIndex-1 });">上一页</a> </c:if><c:if test="${param.pageIndex<param.totalPage }" ><a href="javascript:page_nav(document.forms[0],${param.pageIndex+1 });">下一页</a><a href="javascript:page_nav(document.forms[0],${param.totalPage });">最后一页</a> </c:if></ul> <span class="page-go-form"><label>跳转至</label> <input type="text" name="inputPage" id="inputPage" class="page-key" />页 <button type="button" class="page-btn" onClick='jump_to(document.forms[0],document.getElementById("inputPage").value)'>GO</button></span></div>
newsdatillist.jsp导入rollpage.jsp
<c:import url="rollPage.jsp">
<c:param name="totalCount" value="<%=Integer.toString(totalCount) %>"></c:param><!--传参-->
<c:param name="pageIndex" value="<%=Integer.toString(pageIndex) %>"></c:param>
<c:param name="totalPage" value="<%=Integer.toString(totalPage) %>"></c:param>
</c:import>
13jsp与jdbc总结