三. 过滤器解决中文乱码问题
/** * 中文乱码处理 */ @WebFilter("/*") public class EncodingFiter implements Filter { private String encoding = "UTF-8";// 默认字符集 public EncodingFiter() { super(); } public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; // 中文处理必须放到 chain.doFilter(request, response)方法前面 res.setContentType("text/html;charset=" + this.encoding); if (req.getMethod().equalsIgnoreCase("post")) { req.setCharacterEncoding(this.encoding); } else { Map map = req.getParameterMap();// 保存所有参数名=参数值(数组)的Map集合 Set set = map.keySet();// 取出所有参数名 Iterator it = set.iterator(); while (it.hasNext()) { String name = (String) it.next(); String[] values = (String[]) map.get(name);// 取出参数值[注:参数值为一个数组] for (int i = 0; i < values.length; i++) { values[i] = new String(values[i].getBytes("ISO-8859-1"), this.encoding); } } } chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { String s = filterConfig.getInitParameter("encoding");// 读取web.xml文件中配置的字符集 if (null != s && !s.trim().equals("")) { this.encoding = s.trim(); } } }
四.加入分页功能
- 先不考虑功能性在页面上的table标签下,加入及分页工具条
<div style="text-align: right; width:98%;"> 第1页 共100条记录 <a>首页</a> <a>上页</a> <a>下页</a> <a>尾页</a> 第<input type="text" size="2" /> <a href="#">GO</a> </div>
- 不考虑通过的分页如下实现,先在结果页面中实现分页功能。
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="z" uri="/zking" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>学生信息</h1> <form action="<%=request.getContextPath()%>/students" method="post"> <input type="text" name="sname"/> <input type="submit" value="查询"> </form> <table border="1" style="width: 98%;"> <tr> <td>学号</td> <td>姓名</td> <td>年龄</td> <td>备注</td> </tr> <c:forEach items="${students}" var="student"> <tr> <td>${student.sid}</td> <td>${student.sname}</td> <td>${student.age}</td> <td>${student.remark}</td> </tr> </c:forEach> </table> <!-- 分页页面元素 --> <div style="text-align: right; width:98%;"> 第${pageBean.page}页 共${pageBean.total}条记录 <a href="javascript: goPage(1);">首页</a> <a href="javascript: goPage('${pageBean.previousPage}');">上页</a> <a href="javascript: goPage('${pageBean.nextPage}');">下页</a> <a href="javascript: goPage('${pageBean.totalPage}')">尾页</a> 第<input type="text" id="pagingPageNum" size="2" onkeypress="goSpecifiedPage(event,this.value);"/> <a href="javascript: goPage(document.getElementById('pagingPageNum').value)">GO</a> </div> <!-- 用于分页的隐藏表单 --> <form action="${pageBean.url}" id="pagingForm" method="post"> <input type="hidden" name="page" value="${pageBean.page}"/> <!-- 先只考虑本功能的查询参数,没有考虑公用性(不同功能的参数不同) --> <input type="hidden" name="sname" value="<%=request.getParameter("sname")%>"/> </form> <!-- 用于分页的js代码 --> <script> function goPage(pageNum) { var form = document.getElementById("pagingForm"); form.page.value = pageNum; form.submit(); } function goSpecifiedPage(event) { if(event.keyCode == 13) { var pageNum = document.getElementById("pagingPageNum").value; var form = document.getElementById("pagingForm"); form.page.value = pageNum; form.submit(); } } </script> </body> </html>
目前为止,分页功能已经实现了。
遗留下来的问题:
1 如果其他功能需要分页,则需要复制大量代码才能重用该功能
2 如果系统需要修改分页工具栏的显示风格呢?
四.封装分页标签
为了方便代码的复用,及可维护性,我们将分页功能封装了一个自定义标签(其实就是将原来写在页面中的代码,通过移入到自定义标签中去实现),开发自定义标签分成三步:
- 编写助手类
- 编写标签描述文件
- 在页面上引入标签库,并使用
编写助手类
public class PagingTag extends BodyTagSupport { private PageBean pageBean; public PageBean getPageBean() { return pageBean; } public void setPageBean(PageBean pageBean) { this.pageBean = pageBean; } @Override public int doStartTag() throws JspException { JspWriter out = this.pageContext.getOut(); try { out.println(buildHtml()); return SKIP_BODY; } catch (IOException e) { throw new JspException("分页标签异常", e); } } //生成Html内容 private String buildHtml() { //构建分页页面元素 String pagingElement = "<div style=\"text-align: right; width:98%;\">\r\n" + " 第" + pageBean.getPage() + "页 \r\n" + " 共" + pageBean.getTotal() + "条记录 \r\n" + " <a href=\"javascript: goPage(1);\">首页</a> \r\n" + " <a href=\"javascript: goPage('" + pageBean.getPreviousPage() + "');\">上页</a> \r\n" + " <a href=\"javascript: goPage('" + pageBean.getNextPage() + "');\">下页</a> \r\n" + " <a href=\"javascript: goPage('" + pageBean.getTotalPage() + "')\">尾页</a> \r\n" + " 第<input type=\"text\" id=\"pagingPageNum\" size=\"2\" value='"+pageBean.getPage()+"' onkeypress=\"goSpecifiedPage(event,this.value);\"/> \r\n" + " <a href=\"javascript: goPage(document.getElementById('pagingPageNum').value)\">GO</a>\r\n" + " </div>"; //构建隐藏表单,用于在分页时传递分页参数 String hiddenForm = "<form action='" + pageBean.getUrl() + "' id=\"pagingForm\" method=\"post\">" + "<input type=\"hidden\" name=\"page\" />"; Map<String, String[]> parameterMap = pageBean.getParameterMap(); for(Map.Entry<String, String[]> param: parameterMap.entrySet()) { String paramName = param.getKey(); if("page".equals(paramName)) continue; String[] values = param.getValue(); for(String val: values) { hiddenForm += "<input type='hidden' name='" + paramName + "' value='" + val + "'>"; } } hiddenForm += "</form>"; //构建分页功能需要的js代码块 String script = "<script>\r\n" + " function goPage(pageNum) {\r\n" + " var form = document.getElementById(\"pagingForm\");\r\n" + " form.page.value = pageNum;\r\n" + " form.submit();\r\n" + " }\r\n" + " \r\n" + " function goSpecifiedPage(event) {\r\n" + " if(event.keyCode == 13) {\r\n" + " var pageNum = document.getElementById(\"pagingPageNum\").value;\r\n" + " var form = document.getElementById(\"pagingForm\");\r\n" + " form.page.value = pageNum;\r\n" + " form.submit();\r\n" + " }\r\n" + " }\r\n" + " </script>"; return pagingElement + hiddenForm + script; } }
标签库描述文件中添加paging标签
<tag> <name>paging</name> <tag-class>com.zking.mvc.tag.PagingTag</tag-class> <body-content>empty</body-content> <attribute> <name>pageBean</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
使用分页标签
首先在页面中引入标签
<%@taglib prefix="z" uri="/zking" %>
将原来的分页功能,替换为标签即可
<z:paging pageBean="${pageBean}"/>
MySQL分页
- limit语法
select * from table_name limit [offset,] rows
- 参数说明
参数 | 说明 |
offset | 指定第一个返回记录行的偏移量(即从哪一行开始返回),注意:初始行的偏移量为0 |
offset | 返回具体行数 |
最后结果展示页面