struts of crud

1, into the associated dependent pom (Struts, custom tag library dependencies)
2, tag classes into the tab, mytag.tld, complete configuration web.xml
. 3, to access data dao layer
4, web layer to call dao layer data is returned to the front
5, is configured in struts_sy.xml
6, write jsp

 

pom-dependent

	<!-- 5.3、jstl、standard -->
		<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>

		<!-- 5.4、tomcat-jsp-api -->
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jsp-api</artifactId>
			<version>8.0.47</version>
		</dependency>

 

pageTag (General tab)

package com.chenjiahao.crud.tag;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.chenjiahao.crud.util.PageBean;

public class PageTag extends BodyTagSupport {

	private static final long serialVersionUID = 1L;
	private PageBean pageBean;
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	@Override
	int the doStartTag public () {JspException throws 
		the JspWriter pageContext.getOut OUT = (); 
		the try { 
			Out.print (toHTML ()); 
		} the catch (IOException E) { 
			// the TODO Auto-Generated Block the catch 
			e.printStackTrace (); 
		} 
		super.doStartTag return (); 
	} 
	Private toHTML String () { 
		the StringBuffer the StringBuffer new new SB = (); 
		/ * 
		 * spliced form submitting data to form the background 
		 * Note: page parameters form spliced form is changed, it is not need to retain the value of the last request 
		 * / 
		sb.append ( "<form ID = 'pageBeanForm' Action = '" + pageBean.getUrl () + "' Method = 'POST'>"); 
		sb.append ( "<INPUT = type 'hidden' name = 'Page'> "); 
		the Map <String, String []>parameterMap = pageBean.getParameterMap();
		if(parameterMap != null && parameterMap.size() > 0) { 
			the Set <Entry<String, String[]>> entrySet = parameterMap.entrySet();
			for (Entry<String, String[]> entry : entrySet) {
				if(!"page".equals(entry.getKey())) {
					String[] values = entry.getValue();
					for (String val : values) {
						sb.append("<input type='hidden' name='"+entry.getKey()+"' value='"+val+"'>");
					}
				}
			}
		}
		sb.append("</form>");
		
		/*
		 * 展示的分页条
		 */
		sb.append("<div style='text-align: right; font-size: 12px;'>");
		sb.append("每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage () + "Page of" + pageBean.getMaxPage () + "Home page <a href='javascript:gotoPage(1)'> </a> <a");
		sb.append(" href='javascript:gotoPage("+pageBean.previousPage()+")'>上一页</a>  <a");
		sb.append(" href='javascript:gotoPage("+pageBean.nextPage()+")'>下一页</a>  <a");
		sb.append(" href='javascript:gotoPage("+pageBean.getMaxPage()+")'>尾页</a>  <input type='text'");
		sb.append(" id='skipPage'");
		sb.append(" style='text-align: center; font-size: 12px; width: 50px;'>  <a");
		sb.append(" href='javascript:skipPage()'>Go</a>");
		sb.append("</div>");
		
		/*
		 * 给分页条添加与后台交互的js代码
		 */
		sb.append("<script type='text/javascript'>");
		sb.append("		function gotoPage(page) {");
		sb.append("			document.getElementById('pageBeanForm').page.value = page;");
		sb.append("			document.getElementById('pageBeanForm').submit();");
		sb.append("		}");
		sb.append("		function skipPage() {");
		sb.append("			var page = document.getElementById('skipPage').value;");
		sb.append("			if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>"+pageBean.getMaxPage()+"){");
		sb.append("				alert('请输入1~N的数字');");
		sb.append("				return;");
		sb.append("			}");
		sb.append("			gotoPage(page);");
		sb.append("		}");
		sb.append("</script>");
		return sb.toString();
	}
}

  

 

 

 Custom tag library

mytag.tld

<taglib XMLs = "http://java.sun.com/JSP/TagLibraryDescriptor"> 
	<-! custom tag library version -> 
	<tlib-Version> 1.0 </ tlib-Version> 
	<-! Specification version -> 
	<JSP-version> 1.2 </ JSP-version> 
	<Short-name> the Simple tags </ Short-name> 
	<-! custom tag library name -> 
	<uri> / Zking </ uri> 




	<-! h custom tag -> 
	<! - tag -> 
	<tag> 
		<! - tag name -> 
		<name> Page </ name> 
		<! - helper class tag -> 
		<tag-class> com.chenjiahao.crud.tag.PageTag </ tag-class> 
		<- content type tag;! empty tag indicates an empty (empty tag being given), jsp represents the elements of a valid jsp -> 
		<Content-body> the JSP </ Content-body> 
		<-! tag properties ->
		<attribute> 
			<-! tag property name -> 
			<name> pageBean </ name> 
			<-! if necessary ->
			<required> to true </ required> 
			<-! supports dynamic data -> 
			<rtexprvalue> to true </ rtexprvalue> 
		</ attribute> 
	</ Tag> 




	


</ taglib>

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>

  
  <filter>
     <filter-name>encodingFiter</filter-name>
     <filter-class>com.chenjiahao.crud.util.EncodingFiter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>encodingFiter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  <filter>
    <filter-name>Struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Struts</filter-name>
   <url-pattern>*.action</url-pattern>
  </filter-mapping>
      
  
</web-app>

  dao layer

package com.chenjiahao.crud.dao;

import java.util.List;

import com.chenjiahao.crud.entity.Clazz;

import com.chenjiahao.crud.util.EntityBaseDao;
import com.chenjiahao.crud.util.PageBean;
import com.chenjiahao.crud.util.StringUtils;


public class ClazzDao extends EntityBaseDao<Clazz>{

	/**
	 * 查询分页方法,查询单条数据公共的方法
	 * @param clz
	 * @param pageBean
	 * @return
	 * @throws Exception
	 */
	  public List<Clazz> list(Clazz clz,PageBean pageBean)throws Exception{
		  String sql="select * from t_struts_class where true";
		  String cname=clz.getCname();
		  int cid=clz.getCid();
	
		  if(cid !=0) {
			  sql+=" and cid="+cid;
		  }
		  if(StringUtils.isNotBlank(cname)) {
			  sql+=" and cname like '%"+cname+"%'";
		  }
   
		return super.executeQuery(sql, pageBean, Clazz.class);
	  }
	  /**
	   * 新增方法
	   * @param clz
	   * @return
	   * @throws Exception
	   */
	  
	  public int add(Clazz clz) throws Exception{
		  String sql="insert into t_struts_class values(?,?,?,?)";
		  return super.executeUpdate(sql, new String[] {"cid","cname","cteacher","pic"}, clz);
	   * /
	   * @throws Exception
	   * @return
	   * @param CLZ
	   * modification method
	  / **
	  }
	  public int Edit (Clazz CLZ) throws Exception {
		  String sql="update t_struts_class set cname=?,cteacher=?,pic=? where cid=?";
		  return super.executeUpdate(sql, new String[] {"cname","cteacher","pic","cid"}, clz);
	  }
	  /**
	   * 删除方法
	   * @param clz
	   * @return
	   * @throws Exception
	   */
	  public int del(Clazz clz) throws Exception{
		  String sql="delete from t_struts_class where cid=?";
		  return super.executeUpdate(sql, new String[] {"cid"}, clz);
	  }
	  
	  
}

web layers to return data call dao jsp

package com.chenjiahao.crud.web;

import java.util.List;

import com.chenjiahao.crud.dao.ClazzDao;
import com.chenjiahao.crud.entity.Clazz;
import com.chenjiahao.crud.util.BaseAction;
import com.chenjiahao.crud.util.PageBean;
import com.opensymphony.xwork2.ModelDriven;

public class ClazzAction extends BaseAction implements ModelDriven<Clazz> {
	private ClazzDao clzDao = new ClazzDao();
	private Clazz clz = new Clazz();
    
	
	public String list() throws Exception {
		PageBean pageBean=new PageBean();
		pageBean.setRequest(request);
		List<Clazz> list=this.clzDao.list(clz, pageBean);
		request.setAttribute("clzList", list);
		request.setAttribute("pageBean", pageBean);
		return "list";
	}
	/**
	 * 跳转新增修改页面的公共方法
	 * @return
	 */
	public String preSave()throws Exception{
		if(clz.getCid()!=0) {
		 Clazz c=	this.clzDao.list(clz, null).get(0);
		 request.setAttribute("clz", c);
		}
		return "preSave";
	}
	/**
	 * 新增
	 * @return
	 * @throws Exception
	 */
	public String add()throws Exception {
		result=this.clzDao.add(clz);
		return "toList";
	}
	/**
	 * 修改
	 * @return
	 * @throws Exception
	 */
	public String edit()throws Exception {
		
		this.clzDao.edit(clz);
		return "toList";
	}
	
	/**
	 * 删除
	 * @return
	 * @throws Exception
	 */
	public String del() throws Exception{
		this.clzDao.del(clz);
		
		return "toList";
	}
	
	@Override
	public Clazz getModel() {
		// TODO Auto-generated method stub
		return clz;
	}

}

  struts_sy.xml Configuration

  <action name="/clz_*" class="com.chenjiahao.crud.web.ClazzAction" method="{1}">
	      <result name="list">/clzList.jsp</result>
	      <result name="preSave">/clzEdit.jsp</result>
          <result name="toList" type="redirectAction">/clz_list</result>
	  </action>
	  

  jspy page

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="/Zking"%>
 
<!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 }/sy/clz_list.action" method="post">

 班级名: <input type="text" name="cname" /><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>
       <td>操作</td>
   </tr>
   
   <c:forEach items="${clzList }" var="c">
     <tr>
       <td>${c.cid }</td>
       <td>${c.cname }</td>
       <td>${c.cteacher }</td>
       <td>${c.pic }</td>
       <td>
       <a href="${pageContext.request.contextPath }/sy/clz_preSave.action?cid=${c.cid}">修改</a>   
       <a href="${pageContext.request.contextPath }/sy/clz_del.action?cid=${c.cid}">删除</a>   
       <a href="${pageContext.request.contextPath} /sy/clz_preUpload.action?cid = $ {c.cid} "> Image Upload </a>   
</ Table>
   </ C: forEach>
   </ TR>
       </ TD>
<z:page pageBean="${pageBean }"></z:page>
</body>
</html>

  clzEdit.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 }${clz.cname == null ? '/sy/clz_add.action' : '/sy/clz_edit.action'}" method="post">
 cid:<input type="text" name="cid" value="${clz.cid }"/>
 cname:<input type="text" name="cname" value="${clz.cname }"/> 
 cteacher:<input type="text" name="cteacher" value="${clz.cteacher }"/>
<input type="submit"/>
</form>
</body>
</html>

  

Guess you like

Origin www.cnblogs.com/chenjiahao9527/p/11108602.html