一、EL表达式的使用
主要作用是将java代码和html代码分离
首先创建EL1/EL2/EL1-2三个jsp文件
EL1:form表单,输入用户名和密码,提交后,在另一页面显示用户名输入值和输入密码
<%@ 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=UTF-8"><title>Insert title here</title></head><body><form action="EL1-2.jsp" method="post">用户名:<input type="text" name="userName" ></br>密码:<input type="password" name="pwd" ></br><input type="submit" value="提交"></form></body></html>
EL1-2:取到值并设置到属性;多态方法取到服务对象,利用得到List方法,返回list对象,设置list属性,
<%@page import="cn.bdqn.pojo.NewsDetail"%><%@page import="java.util.List"%><%@page import="cn.bdqn.services.impl.NewsServicesImpl"%><%@page import="cn.bdqn.services.NewsServices"%><%@ 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=UTF-8"><title>Insert title here</title></head><body><!-- EL1转到EL2的java代码 --><%request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");String userName=request.getParameter("userName");String pwd=request.getParameter("pwd");request.setAttribute("uName", userName);request.setAttribute("pwd", pwd);NewsServices ns=new NewsServicesImpl();List<NewsDetail> list=ns.getNewsDetailList();request.setAttribute("list", list);//下步是为了查看作用域pageContext.setAttribute("scope", "pageContext");request.setAttribute("scope", "request");session.setAttribute("scope", "session");application.setAttribute("scope", "application");//通过请求和重定向跳转到哪个页面request.getRequestDispatcher("EL2.jsp").forward(request, response);//EL表达式,获取request对象中的参数采用的是request.getAttribute("param")=${param}%></body></html>
EL2:
<%@ 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=UTF-8"><title>Insert title here</title></head><body><!--<h1>用户名:<%=request.getParameter("userName") %></h1><h1>用户名:<%=request.getParameter("pwd") %></h1>--><h1>用户名:${uName} </h1><h1>用户名:${pwd} </h1><h2>新闻标题:${list[0].title}</h2><h2>算数运算:${3*(4+5) }</h2><h2>关系运算:${7>5 }</h2><h2>逻辑运算:${(7>5)&&(8>9) }</h2><h2>条件运算:${7>5?"ok":"no" }</h2><!-- 如果判断为真,就取?后面的 --><h2>empty(无值就是空返回true):${empty uName }</h2><h2>默认作用域:${scope }</h2><h2>作用域:${sessionScope.scope }</h2></body></html>
EL的作用:
1、用于获取javaBean的属性;${ news,title}
2、读取集合类型对象中的元素;${ list[0] , title}
3、可以使用运算符进行数据处理;${ 3<5 }
4、可以屏蔽一些常见异常;定义的是uName,但${ userName}是不对的,应该是${ uName}
5、可实现自动类型转换
二、JSTL
1、添加标签指令
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
2、常用标签
(1)、输出:
<%
request.setAttribute(“param1”,”
hello
“);
%>
(2)、< c : forEach >
例如下述代码,隔行变色
(3)、< c : if>
上 一页
具体用法看rollPage.jsp文件中的使用
(4)、< c : import>引入
三、优化新闻newsDetailList代码
思路:
1、将首页,下一页,上一页的跳转变成公共的,新建jsp页面,news—->rollPage.jsp;将newsDetailList中翻页的代码剪切到rollPage中。
2、在< c : import >下的< c : param >中写name时,要在上面写request.setAttribute(“ps”,ps);
其次在table结束时写入import,导入到rollPage.jsp,定义当前页和总条数;将rollPage中的java代码全部替换成${ c : if}和${ };
3、翻页调出公共部分,在script中加入1个方法,
//调出公共翻页
function toPage(pageIndex){
var url= path + “/news/newsDetailList.jsp?pageNo=”+pageIndex;
location.href=url;
}
再次回到rollPage中,将剩余的java代码替换成${ };
4、将ps.getContextPath替换成${ path };
在style下面引入
<%
request.setAttribute(“path”, request.getContextPath());
%>
还要在script中将Url中的/web42替换成path+”url”
var path=document.getElementById(“path”).value;
newsDetailList:
<%@page import="cn.bdqn.util.PageSupport"%><%@page import="cn.bdqn.pojo.NewsDetail"%><%@page import="java.util.List"%><%@page import="cn.bdqn.services.impl.NewsServicesImpl"%><%@page import="cn.bdqn.services.NewsServices"%><%@ 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>Insert title here</title></head><style>td{border:black solid 1px}#ip1{width:30px;}.trColor{background-color:gray;}</style><%request.setAttribute("path", request.getContextPath());%><input type="hidden" value="${path }" id="path"><script type="text/javascript">var path=document.getElementById("path").value;function deleteNews(id){var url= path + "/news/deleteNewsDetail.jsp?id="+id;var f=confirm("是否确认删除");if(f){location.href=url;}}function toJump(){var p =document.getElementById("pageJump").value;if(p==null||p==""){alert=("请输入正确页码");}else{if(isNaN(p)){alert("请输入数字");}else{var url= path + "/news/newsDetailList.jsp?pageNo="+p;location.href=url;}}}//调出公共翻页function toPage(pageIndex){var url= path + "/news/newsDetailList.jsp?pageNo="+pageIndex;location.href=url;}</script><jsp:useBean id="ns" class="cn.bdqn.services.impl.NewsServicesImpl"></jsp:useBean><body><a href="${path }/news/addNewsDetail.jsp">添加</a><table style="border:black solid 1px;border-collapse: collapse;"><tr><td>id</td><td>标题</td><td>创建时间</td><td colspan="2">操作</td></tr><%//NewsServices ns=new NewsServicesImpl();//List<NewsDetail> list=ns.getNewsDetailList();String pageNo=request.getParameter("pageNo");int pageNoIndex=0;if(pageNo==null){//当前页码数pageNoIndex=1;}else{pageNoIndex=Integer.parseInt(pageNo);}//拿出工具类int pageSize=5;PageSupport ps=new PageSupport();ps.setCurrentPageNo(pageNoIndex);ps.setPageSize(pageSize);ps.setTotalPageCount(ns.getNewsDetailCount());List<NewsDetail> list=ns.getNewsDetailListByPage(pageNoIndex, pageSize);request.setAttribute("list", list);request.setAttribute("ps",ps);//for(int i=0;i<list.size();i++){//NewsDetail nd=list.get(i);%><!-- JSTL标签 --><c:forEach var="nd" items="${list }" varStatus="varStatus" ><!-- 隔行变色 --><tr <c:if test="${varStatus.count%2==1 }" >class="trColor"</c:if>><td>${nd.id }</td><td><a href="${path }/news/newsDetailView.jsp?id=${nd.id}">${nd.title}</a></td><!-- 添加时间 --><td><fmt:formatDate value="${nd.createDate }" pattern="yyyy-MM-dd HH:mm:ss"/></td><td><a href="${path }/news/upDateNewsDetail.jsp?id=${nd.id}">修改</a></td><td><a href="javascript:deleteNews(${nd.id})">删除</a></td></tr></c:forEach><%//}%></table><c:import url="rollPage.jsp"><c:param name="currentPageNo" value="${ ps.currentPageNo}"></c:param><c:param name="totalPageNo" value="${ ps.totalPageNo}"></c:param></c:import><%request.setAttribute("param1","<h1>hello<h1>");%><!-- JSTL标签 default默认值(当取不到参数的时候,显示默认值),escapeXml="false"进行XTML格式化 --><!-- 主要学习out,if,foreach,import --><c:out value="${param1}" default="无" escapeXml="false"></c:out><c:set var="key1" value="value1" scope="request"></c:set><c:out value="${key1 }"></c:out><!-- 格式化时间,数字(逗号分隔) --><fmt:formatNumber value="123456789" pattern="#,###,###"></fmt:formatNumber></body></html>
rollPage:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><a href="javascript:toPage(1)">首页</a> <c:if test="${param.currentPageNo!=1 }"><a href="javascript:toPage(${param.currentPageNo-1})">上一页</a> </c:if><c:if test="${param.currentPageNo!=param.totalPageNo }"><a href="javascript:toPage(${param.currentPageNo+1})">下一页</a> </c:if><a href="javascript:toPage(${param.totalPageNo})">末页</a> 第${param.currentPageNo}页/共${param.totalPageNo}页<!--<form action="${path }/news/newsDetailList.jsp">跳转至:<input type="text" name="pageNo" id="ip1"><input type="submit" value="go"></form>--><input type="text" id="pageJump" size="1"><input type="button" value="GO" onclick="toJump()">
四、优化用户表
