标签:bsp for 标签库 font info exce into alert j2ee
利用struts2完成增删改查
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.lww</groupId> 5 <artifactId>T224_struts</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>T224_struts Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 <dependency> 12 <groupId>junit</groupId> 13 <artifactId>junit</artifactId> 14 <version>4.12</version> 15 <scope>test</scope> 16 </dependency> 17 18 <dependency> 19 <groupId>javax.servlet</groupId> 20 <artifactId>javax.servlet-api</artifactId> 21 <version>4.0.1</version> 22 <scope>provided</scope> 23 </dependency> 24 25 <dependency> 26 <groupId>org.apache.struts</groupId> 27 <artifactId>struts2-core</artifactId> 28 <version>2.5.13</version> 29 </dependency> 30 31 <!-- 5.3、jstl、standard --> 32 <dependency> 33 <groupId>jstl</groupId> 34 <artifactId>jstl</artifactId> 35 <version>1.2</version> 36 </dependency> 37 <dependency> 38 <groupId>taglibs</groupId> 39 <artifactId>standard</artifactId> 40 <version>1.1.2</version> 41 </dependency> 42 43 <!-- 5.4、tomcat-jsp-api --> 44 <dependency> 45 <groupId>org.apache.tomcat</groupId> 46 <artifactId>tomcat-jsp-api</artifactId> 47 <version>8.0.47</version> 48 </dependency> 49 50 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 51 <dependency> 52 <groupId>mysql</groupId> 53 <artifactId>mysql-connector-java</artifactId> 54 <version>5.1.40</version> 55 </dependency> 56 57 </dependencies> 58 <build> 59 <finalName>T224_struts</finalName> 60 <plugins> 61 <plugin> 62 <groupId>org.apache.maven.plugins</groupId> 63 <artifactId>maven-compiler-plugin</artifactId> 64 <version>3.7.0</version> 65 <configuration> 66 <source>1.8</source> 67 <target>1.8</target> 68 <encoding>UTF-8</encoding> 69 </configuration> 70 </plugin> 71 </plugins> 72 </build> 73 </project>
2、分页的tag类导入、z.tld、完成web.xml的配置
PageTag 通用分页
1 package com.liuwenwu.crud.tag; 2 3 import java.io.IOException; 4 import java.util.Map; 5 import java.util.Map.Entry; 6 import java.util.Set; 7 8 import javax.servlet.jsp.JspException; 9 import javax.servlet.jsp.JspWriter; 10 import javax.servlet.jsp.tagext.BodyTagSupport; 11 12 import com.liuwenwu.crud.util.PageBean; 13 14 public class PageTag extends BodyTagSupport { 15 16 private static final long serialVersionUID = 1L; 17 private PageBean pageBean; 18 public PageBean getPageBean() { 19 return pageBean; 20 } 21 public void setPageBean(PageBean pageBean) { 22 this.pageBean = pageBean; 23 } 24 @Override 25 public int doStartTag() throws JspException { 26 JspWriter out = pageContext.getOut(); 27 try { 28 out.print(toHTML()); 29 } catch (IOException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 return super.doStartTag(); 34 } 35 private String toHTML() { 36 StringBuffer sb = new StringBuffer(); 37 /* 38 * 拼接向后台提交数据的form表单 39 * 注意:拼接的form表单中的page参数是变化的,所以不需要保留上一次请求的值 40 */ 41 sb.append("<form id=‘pageBeanForm‘ action=‘"+pageBean.getUrl()+"‘ method=‘post‘>"); 42 sb.append("<input type=‘hidden‘ name=‘page‘>"); 43 Map<String, String[]> parameterMap = pageBean.getParameterMap(); 44 if(parameterMap != null && parameterMap.size() > 0) { 45 Set<Entry<String, String[]>> entrySet = parameterMap.entrySet(); 46 for (Entry<String, String[]> entry : entrySet) { 47 if(!"page".equals(entry.getKey())) { 48 String[] values = entry.getValue(); 49 for (String val : values) { 50 sb.append("<input type=‘hidden‘ name=‘"+entry.getKey()+"‘ value=‘"+val+"‘>"); 51 } 52 } 53 } 54 } 55 sb.append("</form>"); 56 57 /* 58 * 展示的分页条 59 */ 60 sb.append("<div style=‘text-align: right; font-size: 12px;‘>"); 61 sb.append("每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage()+"页,共"+pageBean.getMaxPage()+"页 <a href=‘javascript:gotoPage(1)‘>首页</a> <a"); 62 sb.append(" href=‘javascript:gotoPage("+pageBean.previousPage()+")‘>上一页</a> <a"); 63 sb.append(" href=‘javascript:gotoPage("+pageBean.nextPage()+")‘>下一页</a> <a"); 64 sb.append(" href=‘javascript:gotoPage("+pageBean.getMaxPage()+")‘>尾页</a> <input type=‘text‘"); 65 sb.append(" id=‘skipPage‘"); 66 sb.append(" style=‘text-align: center; font-size: 12px; width: 50px;‘> <a"); 67 sb.append(" href=‘javascript:skipPage()‘>Go</a>"); 68 sb.append("</div>"); 69 70 /* 71 * 给分页条添加与后台交互的js代码 72 */ 73 sb.append("<script type=‘text/javascript‘>"); 74 sb.append(" function gotoPage(page) {"); 75 sb.append(" document.getElementById(‘pageBeanForm‘).page.value = page;"); 76 sb.append(" document.getElementById(‘pageBeanForm‘).submit();"); 77 sb.append(" }"); 78 sb.append(" function skipPage() {"); 79 sb.append(" var page = document.getElementById(‘skipPage‘).value;"); 80 sb.append(" if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>"+pageBean.getMaxPage()+"){"); 81 sb.append(" alert(‘请输入1~N的数字‘);"); 82 sb.append(" return;"); 83 sb.append(" }"); 84 sb.append(" gotoPage(page);"); 85 sb.append(" }"); 86 sb.append("</script>"); 87 return sb.toString(); 88 } 89 }
z.tld自定义标签配置文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 6 version="2.0"> 7 8 <description>zking 1.1 core library</description> 9 <display-name>zking core</display-name> 10 <tlib-version>1.1</tlib-version> 11 <short-name>z</short-name> 12 <uri>/zking</uri> 13 14 <tag> 15 <!-- 标签库中的标签名 --> 16 <name>page</name> 17 <!-- 标签对应的助手类的全路径名 --> 18 <tag-class>com.liuwenwu.crud.tag.PageTag</tag-class> 19 <body-content>JSP</body-content> 20 <attribute> 21 <!-- 属性名 --> 22 <name>pageBean</name> 23 <!-- 属性值是否是必填 --> 24 <required>true</required> 25 <!-- 是否支持表达式 --> 26 <rtexprvalue>true</rtexprvalue> 27 </attribute> 28 </tag> 29 </taglib>
wed.xml配置
1 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 4 version="3.1"> 5 <display-name>Archetype Created Web Application</display-name> 6 <filter> 7 <filter-name>encodingFiter</filter-name> 8 <filter-class>com.liuwenwu.crud.util.EncodingFiter</filter-class> 9 </filter> 10 <filter-mapping> 11 <filter-name>encodingFiter</filter-name> 12 <url-pattern>/*</url-pattern> 13 </filter-mapping> 14 15 <filter> 16 <filter-name>struts</filter-name> 17 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> 18 </filter> 19 <filter-mapping> 20 <filter-name>struts</filter-name> 21 <url-pattern>*.action</url-pattern> 22 </filter-mapping> 23 </web-app>
3、编写dao层去访问数据
1 package com.liuwenwu.crud.dao; 2 import java.sql.SQLException; 3 import java.util.List; 4 5 import org.apache.commons.lang3.StringUtils; 6 7 import com.liuwenwu.crud.entity.Clazz; 8 import com.liuwenwu.crud.util.EntityBaseDao; 9 import com.liuwenwu.crud.util.PageBean; 10 11 12 public class ClazzDao extends EntityBaseDao<Clazz>{ 13 14 /** 15 * 查询分页,查询单条数据公用方法 16 * @param clz 17 * @param pageBean 18 * @return 19 * @throws InstantiationException 20 * @throws IllegalAccessException 21 * @throws SQLException 22 */ 23 public List<Clazz> list(Clazz clz,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{ 24 String sql="select * from t_struts_class where true "; 25 String cname=clz.getCname(); 26 int cid=clz.getCid(); 27 if(cid!=0) { 28 sql+=" and cid="+cid; 29 } 30 if(StringUtils.isNoneBlank(cname)) { 31 sql+=" and cname like ‘%"+cname+"%‘"; 32 } 33 return super.executeQuery(sql, pageBean, Clazz.class); 34 35 } 36 37 /** 38 * 新增方法 39 * @param clz 40 * @return 41 * @throws NoSuchFieldException 42 * @throws SecurityException 43 * @throws IllegalArgumentException 44 * @throws IllegalAccessException 45 * @throws SQLException 46 */ 47 public int add(Clazz clz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException { 48 String sql="insert into t_struts_class values(?,?,?,?)"; 49 return super.executeUpdate(sql, new String[] {"cid","cname","cteacher","pic"}, clz); 50 51 } 52 53 /** 54 * 修改方法 55 * @param clz 56 * @return 57 * @throws NoSuchFieldException 58 * @throws SecurityException 59 * @throws IllegalArgumentException 60 * @throws IllegalAccessException 61 * @throws SQLException 62 */ 63 public int edit(Clazz clz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException { 64 String sql="update t_struts_class set cname=?,cteacher=?,pic=? where cid=?"; 65 return super.executeUpdate(sql, new String[] {"cname","cteacher","pic","cid"}, clz); 66 67 } 68 /** 69 * 删除方法 70 * @param clz 71 * @return 72 * @throws NoSuchFieldException 73 * @throws SecurityException 74 * @throws IllegalArgumentException 75 * @throws IllegalAccessException 76 * @throws SQLException 77 */ 78 public int del(Clazz clz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException { 79 String sql="delete from t_struts_class where cid=?"; 80 return super.executeUpdate(sql, new String[] {"cid"}, clz); 81 82 } 83 }
4、web层去调用dao层给前台返回数据
1 package com.liuwenwu.crud.web; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 6 import com.liuwenwu.crud.dao.ClazzDao; 7 import com.liuwenwu.crud.entity.Clazz; 8 import com.liuwenwu.crud.util.PageBean; 9 import com.opensymphony.xwork2.ModelDriven; 10 11 public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{ 12 13 private ClazzDao clzDao=new ClazzDao(); 14 private Clazz clz=new Clazz(); 15 16 public String list() { 17 PageBean pageBean=new PageBean(); 18 pageBean.setRequest(request); 19 try { 20 List<Clazz> list= this.clzDao.list(clz, pageBean); 21 // this.result=this.clzDao.list(clz, pageBean); 22 request.setAttribute("clzList", list); 23 request.setAttribute("pageBean", pageBean); 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 return "list"; 28 } 29 30 /** 31 * 跳转新增修改页面的公用方法 32 * @return 33 */ 34 public String preSave() { 35 if(clz.getCid()!=0) { 36 try { 37 Clazz c = this.clzDao.list(clz, null).get(0); 38 request.setAttribute("clz", c); 39 } catch (Exception e) { 40 e.printStackTrace(); 41 } 42 } 43 return "preSave"; 44 } 45 /** 46 * 新增 47 * @return 48 */ 49 public String add() { 50 try { 51 result=this.clzDao.add(clz); 52 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException 53 | SQLException e) { 54 // TODO Auto-generated catch block 55 e.printStackTrace(); 56 } 57 return "toList"; 58 } 59 /** 60 * 修改 61 * @return 62 */ 63 public String edit() { 64 try { 65 this.clzDao.edit(clz); 66 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException 67 | SQLException e) { 68 // TODO Auto-generated catch block 69 e.printStackTrace(); 70 } 71 return "toList"; 72 } 73 /** 74 * 删除 75 * @return 76 */ 77 public String del() { 78 try { 79 this.clzDao.del(clz); 80 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException 81 | SQLException e) { 82 // TODO Auto-generated catch block 83 e.printStackTrace(); 84 } 85 return "toList"; 86 } 87 88 89 @Override 90 public Clazz getModel() { 91 // TODO Auto-generated method stub 92 return clz; 93 } 94 95 }
5、在struts_sy.xml进行配置
1 <action name="/clz_*" class="com.liuwenwu.crud.web.ClazzAction" method="{1}"> 2 <result name="list">/clzList.jsp</result> 3 <result name="preSave">/clzEdit.jsp</result> 4 <result name="toList" type="redirectAction">/clz_list</result> 5 </action>
6、前端jsp展示页面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 <%@taglib prefix="z" uri="/zking" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <h2>班级</h2><br> 13 <form action="${pageContext.request.contextPath }/sy/clz_list.action" method="post"> 14 班级名:<input type="text" name="cname"> 15 <input type="submit" value="确定"> 16 </form> 17 <a href="${pageContext.request.contextPath }/sy/clz_preSave.action">新增</a> 18 <table border="1" width="100%"> 19 <tr> 20 <td>编号</td> 21 <td>班级名</td> 22 <td>教员</td> 23 <td>图片</td> 24 <td>操作</td> 25 </tr> 26 <c:forEach items="${clzList }" var="c"> 27 <tr> 28 <td>${c.cid }</td> 29 <td>${c.cname }</td> 30 <td>${c.cteacher}</td> 31 <td>${c.pic }</td> 32 <td> 33 <a href="${pageContext.request.contextPath }/sy/clz_preSave.action?cid=${c.cid}">修改</a> 34 <a href="${pageContext.request.contextPath }/sy/clz_del.action?cid=${c.cid}">删除</a> 35 <a href="${pageContext.request.contextPath }/sy/clz_preUpload.action?cid=${c.cid}">图片上传</a> 36 </td> 37 </tr> 38 </c:forEach> 39 </table> 40 <z:page pageBean="${pageBean }"></z:page> 41 42 </body> 43 </html>
运行结果:
标签:bsp for 标签库 font info exce into alert j2ee
原文地址:https://www.cnblogs.com/hyfl/p/11217250.html