一、EL表达式的使用

主要作用是将java代码和html代码分离
首先创建EL1/EL2/EL1-2三个jsp文件
EL1:form表单,输入用户名和密码,提交后,在另一页面显示用户名输入值和输入密码

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="EL1-2.jsp" method="post">
  11. 用户名:<input type="text" name="userName" ></br>
  12. 密码:<input type="password" name="pwd" ></br>
  13. <input type="submit" value="提交">
  14. </form>
  15. </body>
  16. </html>

EL1-2:取到值并设置到属性;多态方法取到服务对象,利用得到List方法,返回list对象,设置list属性,

  1. <%@page import="cn.bdqn.pojo.NewsDetail"%>
  2. <%@page import="java.util.List"%>
  3. <%@page import="cn.bdqn.services.impl.NewsServicesImpl"%>
  4. <%@page import="cn.bdqn.services.NewsServices"%>
  5. <%@ page language="java" contentType="text/html; charset=UTF-8"
  6. pageEncoding="UTF-8"%>
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  11. <title>Insert title here</title>
  12. </head>
  13. <body>
  14. <!-- EL1转到EL2java代码 -->
  15. <%
  16. request.setCharacterEncoding("UTF-8");
  17. response.setCharacterEncoding("UTF-8");
  18. String userName=request.getParameter("userName");
  19. String pwd=request.getParameter("pwd");
  20. request.setAttribute("uName", userName);
  21. request.setAttribute("pwd", pwd);
  22. NewsServices ns=new NewsServicesImpl();
  23. List<NewsDetail> list=ns.getNewsDetailList();
  24. request.setAttribute("list", list);
  25. //下步是为了查看作用域
  26. pageContext.setAttribute("scope", "pageContext");
  27. request.setAttribute("scope", "request");
  28. session.setAttribute("scope", "session");
  29. application.setAttribute("scope", "application");
  30. //通过请求和重定向跳转到哪个页面
  31. request.getRequestDispatcher("EL2.jsp").forward(request, response);
  32. //EL表达式,获取request对象中的参数采用的是request.getAttribute("param")=${param}
  33. %>
  34. </body>
  35. </html>

EL2:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <!--
  11. <h1>用户名:<%=request.getParameter("userName") %></h1>
  12. <h1>用户名:<%=request.getParameter("pwd") %></h1>
  13. -->
  14. <h1>用户名:${uName} </h1>
  15. <h1>用户名:${pwd} </h1>
  16. <h2>新闻标题:${list[0].title}</h2>
  17. <h2>算数运算:${3*(4+5) }</h2>
  18. <h2>关系运算:${7>5 }</h2>
  19. <h2>逻辑运算:${(7>5)&&(8>9) }</h2>
  20. <h2>条件运算:${7>5?"ok":"no" }</h2><!-- 如果判断为真,就取?后面的 -->
  21. <h2>empty(无值就是空返回true):${empty uName }</h2>
  22. <h2>默认作用域:${scope }</h2>
  23. <h2>作用域:${sessionScope.scope }</h2>
  24. </body>
  25. </html>

EL的作用:
1、用于获取javaBean的属性;${ news,title}
2、读取集合类型对象中的元素;${ list[0] , title}
3、可以使用运算符进行数据处理;${ 3<5 }
4、可以屏蔽一些常见异常;定义的是uName,但${ userName}是不对的,应该是${ uName}
5、可实现自动类型转换

二、JSTL

1、添加标签指令

  1. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  2. <%@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:

  1. <%@page import="cn.bdqn.util.PageSupport"%>
  2. <%@page import="cn.bdqn.pojo.NewsDetail"%>
  3. <%@page import="java.util.List"%>
  4. <%@page import="cn.bdqn.services.impl.NewsServicesImpl"%>
  5. <%@page import="cn.bdqn.services.NewsServices"%>
  6. <%@ page language="java" contentType="text/html; charset=UTF-8"
  7. pageEncoding="UTF-8"%>
  8. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  9. <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  11. <html>
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  14. <title>Insert title here</title>
  15. </head>
  16. <style>
  17. td{
  18. border:black solid 1px
  19. }
  20. #ip1{
  21. width:30px;
  22. }
  23. .trColor{
  24. background-color:gray;
  25. }
  26. </style>
  27. <%
  28. request.setAttribute("path", request.getContextPath());
  29. %>
  30. <input type="hidden" value="${path }" id="path">
  31. <script type="text/javascript">
  32. var path=document.getElementById("path").value;
  33. function deleteNews(id){
  34. var url= path + "/news/deleteNewsDetail.jsp?id="+id;
  35. var f=confirm("是否确认删除");
  36. if(f){
  37. location.href=url;
  38. }
  39. }
  40. function toJump(){
  41. var p =document.getElementById("pageJump").value;
  42. if(p==null||p==""){
  43. alert=("请输入正确页码");
  44. }else{
  45. if(isNaN(p)){
  46. alert("请输入数字");
  47. }else{
  48. var url= path + "/news/newsDetailList.jsp?pageNo="+p;
  49. location.href=url;
  50. }
  51. }
  52. }
  53. //调出公共翻页
  54. function toPage(pageIndex){
  55. var url= path + "/news/newsDetailList.jsp?pageNo="+pageIndex;
  56. location.href=url;
  57. }
  58. </script>
  59. <jsp:useBean id="ns" class="cn.bdqn.services.impl.NewsServicesImpl"></jsp:useBean>
  60. <body>
  61. <a href="${path }/news/addNewsDetail.jsp">添加</a>
  62. <table style="border:black solid 1px;border-collapse: collapse;">
  63. <tr>
  64. <td>id</td>
  65. <td>标题</td>
  66. <td>创建时间</td>
  67. <td colspan="2">操作</td>
  68. </tr>
  69. <%
  70. //NewsServices ns=new NewsServicesImpl();
  71. //List<NewsDetail> list=ns.getNewsDetailList();
  72. String pageNo=request.getParameter("pageNo");
  73. int pageNoIndex=0;
  74. if(pageNo==null){
  75. //当前页码数
  76. pageNoIndex=1;
  77. }else{
  78. pageNoIndex=Integer.parseInt(pageNo);
  79. }
  80. //拿出工具类
  81. int pageSize=5;
  82. PageSupport ps=new PageSupport();
  83. ps.setCurrentPageNo(pageNoIndex);
  84. ps.setPageSize(pageSize);
  85. ps.setTotalPageCount(ns.getNewsDetailCount());
  86. List<NewsDetail> list=ns.getNewsDetailListByPage(pageNoIndex, pageSize);
  87. request.setAttribute("list", list);
  88. request.setAttribute("ps",ps);
  89. //for(int i=0;i<list.size();i++){
  90. //NewsDetail nd=list.get(i);
  91. %>
  92. <!-- JSTL标签 -->
  93. <c:forEach var="nd" items="${list }" varStatus="varStatus" >
  94. <!-- 隔行变色 -->
  95. <tr <c:if test="${varStatus.count%2==1 }" >class="trColor"</c:if>>
  96. <td>${nd.id }</td>
  97. <td><a href="${path }/news/newsDetailView.jsp?id=${nd.id}">${nd.title}</a></td>
  98. <!-- 添加时间 -->
  99. <td><fmt:formatDate value="${nd.createDate }" pattern="yyyy-MM-dd HH:mm:ss"/></td>
  100. <td><a href="${path }/news/upDateNewsDetail.jsp?id=${nd.id}">修改</a></td>
  101. <td><a href="javascript:deleteNews(${nd.id})">删除</a></td>
  102. </tr>
  103. </c:forEach>
  104. <%//}%>
  105. </table>
  106. <c:import url="rollPage.jsp">
  107. <c:param name="currentPageNo" value="${ ps.currentPageNo}"></c:param>
  108. <c:param name="totalPageNo" value="${ ps.totalPageNo}"></c:param>
  109. </c:import>
  110. <%
  111. request.setAttribute("param1","<h1>hello<h1>");
  112. %>
  113. <!-- JSTL标签 default默认值(当取不到参数的时候,显示默认值),escapeXml="false"进行XTML格式化 -->
  114. <!-- 主要学习outifforeach,import -->
  115. <c:out value="${param1}" default="无" escapeXml="false"></c:out>
  116. <c:set var="key1" value="value1" scope="request"></c:set>
  117. <c:out value="${key1 }"></c:out>
  118. <!-- 格式化时间,数字(逗号分隔) -->
  119. <fmt:formatNumber value="123456789" pattern="#,###,###"></fmt:formatNumber>
  120. </body>
  121. </html>

rollPage:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <a href="javascript:toPage(1)">首页</a>&nbsp;&nbsp;
  5. <c:if test="${param.currentPageNo!=1 }">
  6. <a href="javascript:toPage(${param.currentPageNo-1})">上一页</a>&nbsp;&nbsp;
  7. </c:if>
  8. <c:if test="${param.currentPageNo!=param.totalPageNo }">
  9. <a href="javascript:toPage(${param.currentPageNo+1})">下一页</a>&nbsp;&nbsp;
  10. </c:if>
  11. <a href="javascript:toPage(${param.totalPageNo})">末页</a>&nbsp;&nbsp;
  12. ${param.currentPageNo}页/共${param.totalPageNo}页
  13. <!--
  14. <form action="${path }/news/newsDetailList.jsp">
  15. 跳转至:<input type="text" name="pageNo" id="ip1">
  16. <input type="submit" value="go">
  17. </form>
  18. -->
  19. <input type="text" id="pageJump" size="1">
  20. <input type="button" value="GO" onclick="toJump()">

四、优化用户表