struts2完成增删改查
利用struts完成增删改查:
思路:
1、导入相关的pom依赖(struts、自定义标签库的依赖)
2、分页的tag类导入、z.tld、完成web.xml的配置
3、dao层去访问数据
4、web层去调用dao层给前台返回数据
5、在struts_sy.xml进行配置
6、写jsp界面
1、导入相关的pom依赖(struts、自定义标签库的依赖)
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.psy</groupId> 5 <artifactId>caoluo</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>caoluo Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 12 <dependency> 13 <groupId>junit</groupId> 14 <artifactId>junit</artifactId> 15 <version>4.12</version> 16 <scope>test</scope> 17 </dependency> 18 19 20 <dependency> 21 <groupId>javax.servlet</groupId> 22 <artifactId>javax.servlet-api</artifactId> 23 <version>4.0.1</version> 24 <scope>provided</scope> 25 </dependency> 26 27 <dependency> 28 <groupId>org.apache.struts</groupId> 29 <artifactId>struts2-core</artifactId> 30 <version>2.5.13</version> 31 </dependency> 32 33 34 <!-- 5.3、jstl、standard --> 35 <dependency> 36 <groupId>jstl</groupId> 37 <artifactId>jstl</artifactId> 38 <version>1.2</version> 39 </dependency> 40 <dependency> 41 <groupId>taglibs</groupId> 42 <artifactId>standard</artifactId> 43 <version>1.1.2</version> 44 </dependency> 45 46 <dependency> 47 <groupId>mysql</groupId> 48 <artifactId>mysql-connector-java</artifactId> 49 <version>5.1.44</version> 50 </dependency> 51 52 <!-- 5.4、tomcat-jsp-api --> 53 <dependency> 54 <groupId>org.apache.tomcat</groupId> 55 <artifactId>tomcat-jsp-api</artifactId> 56 <version>8.0.47</version> 57 </dependency> 58 59 </dependencies> 60 <build> 61 <finalName>T224_caoluo</finalName> 62 <plugins> 63 <plugin> 64 <groupId>org.apache.maven.plugins</groupId> 65 <artifactId>maven-compiler-plugin</artifactId> 66 <version>3.7.0</version> 67 <configuration> 68 <source>1.8</source> 69 <target>1.8</target> 70 <encoding>UTF-8</encoding> 71 </configuration> 72 </plugin> 73 </plugins> 74 75 </build> 76 </project>
2、分页的tag类导入、z.tld、完成web.xml的配置
1 /** 2 * 分页工具类 3 * 4 */ 5 public class PageBean { 6 7 private int page = 1;// 页码 8 9 private int rows = 4;// 页大小 10 11 private int total = 0;// 总记录数 12 13 private boolean pagination = true;// 是否分页 14 // 获取前台向后台提交的所有参数 15 private Map<String, String[]> parameterMap; 16 // 获取上一次访问后台的url 17 private String url; 18 19 /** 20 * 初始化pagebean 21 * 22 * @param req 23 */ 24 public void setRequest(HttpServletRequest req) { 25 this.setPage(req.getParameter("page")); 26 this.setRows(req.getParameter("rows")); 27 // 只有jsp页面上填写pagination=false才是不分页 28 this.setPagination(!"fasle".equals(req.getParameter("pagination"))); 29 this.setParameterMap(req.getParameterMap()); 30 this.setUrl(req.getRequestURL().toString()); 31 } 32 33 public int getMaxPage() { 34 return this.total % this.rows == 0 ? this.total / this.rows : this.total / this.rows + 1; 35 } 36 37 public int nextPage() { 38 return this.page < this.getMaxPage() ? this.page + 1 : this.getMaxPage(); 39 } 40 41 public int previousPage() { 42 return this.page > 1 ? this.page - 1 : 1; 43 } 44 45 public PageBean() { 46 super(); 47 } 48 49 public int getPage() { 50 return page; 51 } 52 53 public void setPage(int page) { 54 this.page = page; 55 } 56 57 public void setPage(String page) { 58 this.page = StringUtils.isBlank(page) ? this.page : Integer.valueOf(page); 59 } 60 61 public int getRows() { 62 return rows; 63 } 64 65 public void setRows(int rows) { 66 this.rows = rows; 67 } 68 69 public void setRows(String rows) { 70 this.rows = StringUtils.isBlank(rows) ? this.rows : Integer.valueOf(rows); 71 } 72 73 public int getTotal() { 74 return total; 75 } 76 77 public void setTotal(int total) { 78 this.total = total; 79 } 80 81 public void setTotal(String total) { 82 this.total = Integer.parseInt(total); 83 } 84 85 public boolean isPagination() { 86 return pagination; 87 } 88 89 public void setPagination(boolean pagination) { 90 this.pagination = pagination; 91 } 92 93 public Map<String, String[]> getParameterMap() { 94 return parameterMap; 95 } 96 97 public void setParameterMap(Map<String, String[]> parameterMap) { 98 this.parameterMap = parameterMap; 99 } 100 101 public String getUrl() { 102 return url; 103 } 104 105 public void setUrl(String url) { 106 this.url = url; 107 } 108 109 /** 110 * 获得起始记录的下标 111 * 112 * @return 113 */ 114 public int getStartIndex() { 115 return (this.page - 1) * this.rows; 116 } 117 118 @Override 119 public String toString() { 120 return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination 121 + ", parameterMap=" + parameterMap + ", url=" + url + "]"; 122 } 123 124 }
z.tld
<description>1.1 core library</description> <display-name> core</display-name> <tlib-version>1.1</tlib-version> <short-name>z</short-name> <uri>/zking</uri> <tag> <name>page</name> <tag-class>com.tag.PageTag</tag-class> <body-content>JSP</body-content> <attribute> <name>pageBean</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
web.xml配置
1 <filter> 2 <filter-name>encodingFiter</filter-name> 3 <filter-class>com.util.EncodingFiter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>encodingFiter</filter-name> 7 <url-pattern>/*</url-pattern> 8 </filter-mapping> 9 10 <filter> 11 <filter-name>struts</filter-name> 12 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> 13 </filter> 14 <filter-mapping> 15 <filter-name>struts</filter-name> 16 <url-pattern>*.action</url-pattern> 17 </filter-mapping>
dao层去访问数据
1 public class ClazzDao extends BaseDao<Clazz> { 2 /** 3 * 查询分页 查询单个数据公用的方法 4 * @param clz 5 * @param pageBean 6 * @return 7 */ 8 public List<Clazz> list(Clazz clz,PageBean pageBean){ 9 String sql="select * from t_struts_class where true"; 10 String cname=clz.getCname(); 11 int cid=clz.getCid(); 12 if(cid!=0) { 13 sql +=" and cid = "+cid; 14 } 15 if(StringUtils.isNotBlank(cname)) { 16 sql +=" and cname like '%"+cname+"%'"; 17 } 18 return super.executeQuery(sql, Clazz.class, pageBean); 19 20 } 21 /** 22 * 新增方法 23 * @param clz 24 * @return 25 */ 26 public int add(Clazz clz) { 27 String sql="insert into t_struts_class values(?,?,?,?)"; 28 return super.executeUpdate(sql, new String[] {"cid","cname","cteacher","pic"}, clz); 29 } 30 /** 31 * 修改方法 32 * @param clz 33 * @return 34 */ 35 public int edit(Clazz clz) { 36 String sql="update t_struts_class set cname=?,cteacher=?,pic=? where cid=?"; 37 return super.executeUpdate(sql, new String[] {"cname","cteacher","pic","cid"}, clz); 38 } 39 /** 40 * 删除方法 41 * @param clz 42 * @return 43 */ 44 public int del(Clazz clz) { 45 String sql="delete from t_struts_class where cid=?"; 46 return super.executeUpdate(sql, new String[] {"cid"}, clz); 47 } 48 49 }
web层调用dao返回数据给jsp
1 public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{ 2 3 4 private ClazzDao clzDao=new ClazzDao(); 5 private Clazz clz=new Clazz(); 6 7 public String list() { 8 PageBean pageBean=new PageBean(); 9 pageBean.setRequest(request); 10 List<Clazz> list=this.clzDao.list(clz, pageBean); 11 request.setAttribute("clzList", list); 12 request.setAttribute("pageBean", pageBean); 13 return "list"; 14 } 15 /** 16 * 跳转新增修改页面的工用方法 17 * @return 18 */ 19 public String preSave() { 20 if(clz.getCid()!=0) { 21 Clazz c=this.clzDao.list(clz, null).get(0); 22 request.setAttribute("clz", c); 23 } 24 return "preSave"; 25 26 } 27 /** 28 * 新增 29 * @return 30 */ 31 public String add() { 32 result= this.clzDao.add(clz); 33 return "toList"; 34 35 } 36 /** 37 * 修改 38 * @return 39 */ 40 public String edit() { 41 this.clzDao.edit(clz); 42 return "toList"; 43 44 } 45 /** 46 * 删除 47 * @return 48 */ 49 public String del() { 50 this.clzDao.del(clz); 51 return "toList"; 52 53 } 54 @Override 55 public Clazz getModel() { 56 // TODO Auto-generated method stub 57 return clz; 58 } 59 60 61 }
在struts_sy.xml进行配置
1 <struts> 2 <package name="sy" extends="base" namespace="/sy"> 3 <action name="/clz_*" class="com.web.ClazzAction" method="{1}"> 4 <result name="list">/clzList.jsp</result> 5 <result name="preSave">/clzEdit.jsp</result> 6 <result name="toList" type="redirectAction">/clz_list</result> 7 </action> 8 </package> 9 </struts>
写jsp界面
首页:
1 <body> 2 3 <form action="${pageContext.request.contextPath}/sy/clz_list.action" 4 method="post"> 5 班级名:<input type="text" name="cname"> <input type="submit"> 6 7 </form> 8 <a href="${pageContext.request.contextPath }/sy/clz_preSave.action">新增</a> 9 <table border="1" width="100%"> 10 11 <tr> 12 <td>编号</td> 13 <td>班级名</td> 14 <td>教员</td> 15 <td>图片</td> 16 <td>操作</td> 17 </tr> 18 19 <c:forEach items="${clzList }" var="c"> 20 <tr> 21 <td>${c.cid }</td> 22 <td>${c.cname }</td> 23 <td>${c.cteacher }</td> 24 <td>${c.pic }</td> 25 <td> 26 <a href="${pageContext.request.contextPath }/sy/clz_preSave.action?cid=${c.cid }">修改</a> 27 <a href="${pageContext.request.contextPath }/sy/clz_del.action?cid=${c.cid }">删除</a> 28 <a href="${pageContext.request.contextPath }/sy/clz_preUpload.action?cid=${c.cid }">上传图片</a> 29 </td> 30 </tr> 31 </c:forEach> 32 </table> 33 34 <z:page pageBean="${pageBean }"></z:page> 35 36 </body>
新增页面与修改公用页面
1 <body> 2 <!-- 新增页面与修改页面公用 --> 3 <form action="${pageContext.request.contextPath }${clz.cname == null ? '/sy/clz_add.action' : '/sy/clz_edit.action'}" method="post"> 4 编号:<input type="text" name="cid" value="${clz.cid }"> <br> 5 班级名:<input type="text" name="cname" value="${clz.cname }"> <br> 6 教员:<input type="text" name="cteacher" value="${clz.cteacher }"> <br> 7 8 <input type="submit"> 9 </form> 10 </body>
运行效果图
首页:
新增:
修改:
完成;