1、导入相关的依赖:struts、自定义标签库
配置pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>strtus</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>strtus Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.16</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jsp-api</artifactId> <version>8.0.47</version> </dependency> </dependencies> <build> <finalName>struts</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
分页:
package com.util; import java.util.Map; import javax.servlet.http.HttpServletRequest; public class PageBean { private int page = 1;// 页码 private int rows = 10;// 行数/页大小 private int total = 0;// 总记录数 private boolean pagination = true;// 默认分页 private String url;// 上一次请求的地址 private Map<String, String[]> parameterMap;// 上一次请求的所有参数 public PageBean() { super(); } /** * 对分页bean进行初始化 * * @param request */ public void setRequest(HttpServletRequest request) { // 公共参数 this.setPage(request.getParameter("page")); this.setRows(request.getParameter("rows")); this.setPagination(request.getParameter("pagination")); // 请求地址和请求参数 this.setUrl(request.getContextPath() + request.getServletPath()); this.setParameterMap(request.getParameterMap()); } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Map<String, String[]> getParameterMap() { return parameterMap; } public void setParameterMap(Map<String, String[]> parameterMap) { this.parameterMap = parameterMap; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public void setPage(String page) { if (null != page && !"".equals(page.trim())) { this.page = Integer.parseInt(page); } } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public void setRows(String rows) { if (null != rows && !"".equals(rows.trim())) { this.rows = Integer.parseInt(rows); } } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public void setTotal(String total) { this.total = Integer.parseInt(total); } public boolean isPagination() { return pagination; } public void setPagination(boolean pagination) { this.pagination = pagination; } public void setPagination(String pagination) { if ("false".equals(pagination)) { this.pagination = false; } } /** * 下一页 * * @return */ public int getNextPage() { int nextPage = page + 1; if (nextPage > this.getMaxPage()) { nextPage = this.getMaxPage(); } return nextPage; } /** * 上一页 * * @return */ public int getPreviousPage() { int previousPage = page - 1; if (previousPage < 1) { previousPage = 1; } return previousPage; } /** * 最大页码 * * @return */ public int getMaxPage() { return total % rows == 0 ? total / rows : total / rows + 1; } /** * 起始记录的下标 * * @return */ public int getStartIndex() { return (page - 1) * rows; } @Override public String toString() { return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]"; } }
Dao层:
ClazzDao
package com.dao; import java.util.List; import com.entity.Clazz; import com.util.BaseDao; import com.util.PageBean; import com.util.StringUtils; public class ClazzDao extends BaseDao<Clazz>{ public List<Clazz> list(Clazz clz, PageBean pageBean) throws InstantiationException, IllegalAccessException{ String sql="select * from t_strtus_class"; String cname=clz.getCname(); if (StringUtils.isNotBlank(cname)) { sql+=" and cname like '%"+cname+"%'"; } return super.executeQuery(sql, pageBean,Clazz.class); } public int add(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { String sql="insert into t_struts_class values(?,?,?,?)"; return super.executeupdate(sql, new String[] {"cid","cname","cteacher","pic"}, clazz); } public int del(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { String sql="delete from t_struts_class where cid=?"; return super.executeupdate(sql, new String[] {"cid"}, clazz); } public int edit(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { String sql="update t_struts_class set cname=?,cteacher=?,pic=?"; return super.executeupdate(sql, new String[] {"cname","cteacher","pic","cid"}, clazz); } }
web层调用数据返回给前台
package com.web; import java.sql.SQLException; import java.util.List; import com.dao.ClazzDao; import com.entity.Clazz; import com.util.BaseAction; import com.util.PageBean; import com.opensymphony.xwork2.ModelDriven; public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{ private Clazz clz=new Clazz(); private ClazzDao clzDao=new ClazzDao(); public String list(){ PageBean pageBean =new PageBean(); pageBean.setRequest(request); try { List<Clazz> list = this.clzDao.list(clz, pageBean); request.setAttribute("clzList",list); request.setAttribute("pageBean",pageBean); } catch (InstantiationException | IllegalAccessException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "list"; } /** * 跳转编辑页面 * @return */ public String preSave() { if(clz.getCid()!=0) { try { this.result= this.clzDao.list(clz, null).get(0); } catch (InstantiationException | IllegalAccessException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return "preSave"; } public String add() { try { this.code= this.clzDao.add(clz); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } public String edit() { try { this.clzDao.edit(clz); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } public String del() { try { this.clzDao.del(clz); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } @Override public Clazz getModel() { // TODO Auto-generated method stub return clz; } }
配置strtus-sy.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="sy" extends="base" > <action name="/clz_*" class="com.web.ClazzAction" method="{1}"> <result name="list">/clzList.jsp</result> <result name="preSave">/Edit.jsp</result> <result name="toList" type="redirectAction">/clz_list</result> </action> </package> </struts>
JSP
clzList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@taglib prefix="z" uri="/lingerqi"%> <!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> <h2>小说目录</h2> <br> <form action="${pageContext.request.contextPath}/sy/clz_list.action" method="post"> 书名:<input type="text" name="bname"> <input type="submit" value="确定"> </form> <a href="${pageContext.request.contextPath}/sy/clz_preSave.action">增加</a> <table border="1" width="100%"> <tr> <td>编号</td> <td>班级名称</td> <td>老师</td> <td>班级图片</td> </tr> <c:forEach items="${clzList }" var="b"> <tr> <td>${b.cid }</td> <td>${b.cname }</td> <td>${b.cteacher }</td> <td>${b.pic }</td> <td><a href="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${b.cid}">修改</a> <a href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${b.cid}">删除</a> <a href="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${b.cid}">图片上传</a> </td> </tr> </c:forEach> </table> <z:page pageBean="${pageBean }"></z:page> </body> </html>
edit.jsp
<%@ 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="${pageContext.request.contextPath}${result.cname == null ? '/sy/clz_add.action' : '/sy/clz_edit.action'}" method="post"> cid:<input type="text" value="${result.cid }" name="cid"><br> cname:<input type="text" value="${result.cname }" name="cname"><br> cteacher:<input type="text" value="${result.cteacher }" name="cteacher"><br> <input type="submit" value="ok"> </form> </body> </html>