1、导入相关的pom依赖(struts、自定义标签库的依赖)
commons-fileupload commons-fileupload 1.3.1 jstl jstl 1.2 taglibs standard 1.1.2 org.apache.tomcat tomcat-jsp-api 8.0.47
2、分页的tag类导入、z.tld、完成web.xml的配置
PageTag
package com.tag;import java.util.Map;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.BodyTagSupport;import com.util.PageBean;public class PageTag extends BodyTagSupport { private static final long serialVersionUID = 8003507841172246203L; private PageBean pageBean; public PageTag() { super(); } public PageBean getPageBean() { return pageBean; } public void setPageBean(PageBean pageBean) { this.pageBean = pageBean; } @Override public int doStartTag() throws JspException { try { JspWriter out = pageContext.getOut(); out.println(this.toHtml()); return SKIP_BODY; } catch (Exception e) { throw new RuntimeException(e); } } private String toHtml() { StringBuffer bf = new StringBuffer(); if (null == pageBean || !pageBean.isPagination()) { return bf.toString(); } // 1. 生成表单 bf.append("
z.tld
1.0 1.2 Simple Tags /zking page com.tag.PageTag empty pageBean true true
web.xml
Archetype Created Web Application encodingFiter com.util.EncodingFiter encodingFiter /* struts2 org.apache.struts2.factory.StrutsActionProxy struts2 *.action
3、dao层去访问数据
创建与数据库相对应的实体类
Clazz
package com.entity;public class Clazz { private int cid; private String cname; private String cteacher; private String pic; public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public String getCteacher() { return cteacher; } public void setCteacher(String cteacher) { this.cteacher = cteacher; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } @Override public String toString() { return "Clazz [cid=" + cid + ", cname=" + cname + ", cteacher=" + cteacher + ", pic=" + pic + "]"; } }
ClazzDao
package com.dao;import java.sql.SQLException;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{ /** * 查詢 * @param clazz * @param pageBean * @return * @throws Exception */ public List list(Clazz clazz,PageBean pageBean) throws Exception{ String sql="select * from t_struts_class where true "; String cname=clazz.getCname(); int cid=clazz.getCid(); if(cid!=0) { sql+=" and cid="+cid; } if(StringUtils.isNotBlank(cname)) { sql+=" and cname like '%"+cname+"%'"; } return super.executeQuery(sql, Clazz.class, pageBean); } /** * 新增 * @param clazz * @return * @throws NoSuchFieldException * @throws Exception */ public int add(Clazz clazz) throws NoSuchFieldException, Exception { String sql="insert into t_struts_class values(?,?,?,?)"; return super.executeUpdate(sql, new String [] {"cid","cname","cteacher","pic"}, clazz); } /** * 删除 * @param clazz * @return * @throws NoSuchFieldException * @throws Exception */ public int del(Clazz clazz) throws NoSuchFieldException, Exception { String sql="delete from t_struts_class where cid=? "; return super.executeUpdate(sql, new String [] {"cid"}, clazz); } /** * 修改 * @param clazz * @return * @throws NoSuchFieldException * @throws Exception */ public int edit(Clazz clazz) throws NoSuchFieldException, Exception { String sql=" update t_struts_class set cname= ? , chteacher=? ,pic=? where cid=? "; return super.executeUpdate(sql, new String [] {"cname","cteacher","pic","cid"}, clazz); }}
4、web层去调用dao层给前台返回数据
ClazzAction
package com.web;import java.util.List;import com.dao.ClazzDao;import com.entity.Clazz;import com.opensymphony.xwork2.ModelDriven;import com.util.PageBean;public class ClazzAction extends BaseAction implements ModelDriven{ private Clazz clz=new Clazz(); private ClazzDao clazzDao=new ClazzDao(); public String list() { PageBean pageBean=new PageBean(); pageBean.setRequest(request); try { List list=this.clazzDao.list(clz, pageBean); request.setAttribute("clzList", list); request.setAttribute("pageBean", pageBean); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "list"; } /** * 跳转编辑页面(新增修改页面) */ public String preSave() { if(clz.getCid()!=0) { try { this.result=this.clazzDao.list(clz, null).get(0); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return "preSave"; } public String add() { try { this.code=this.clazzDao.add(clz); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } public String edie() { try { this.clazzDao.edit(clz); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } public String del() { try { this.clazzDao.del(clz); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "toList"; } @Override public Clazz getModel() { // TODO Auto-generated method stub return clz; } }
5、在struts_sy.xml进行配置
/clzList.jsp /clzEdit.jsp /clz_List
6、写jsp页面
clzList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@page isELIgnored="false"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@taglib prefix="z" uri="/zking"%>增加Insert title here 小说目录
编号 | 班级名称 | 教员 | 班级图片 | 操作 |
${c.cid } | ${c.cname } | ${c.cteacher } | ${c.pic } | 修改 删除 图片上传 |
ClzEdit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>班级编辑界面
页面效果