利用struts完成增删改查

本篇为下篇作为铺垫。

1.导入相关的Pom依赖(struts、自定义标签库的依赖)
<!-- 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>

Struts标签的使用

s:iterator
	S:action
	S:url
	S:form
	s:textfield
	S:select
	S:radio
	S:param
	s:textarea
  1. 不直接跳页面,跳子控制器,因为路径问题和*.action配置
  2. 修改页面弹栈的问题,load出的结果作为跟,属性可以直接取值
  3. 页面样式问题 theme
<table border="1" width="100%">
		<tr>
			<td>序号</td>
			<td>学号</td>
			<td>姓名</td>
			<td>拼音</td>
			<td>性别</td>
			<td>标记</td>
			<td>班级</td>
			<td>操作</td>
		</tr>
		<s:iterator var="s" value="result">
		<tr>
			<td>序号</td>
			<td><s:property value="#s.sid"/></td>
			<td><s:property value="#s.sname"/></td>
			<td><s:property value="#s.spin"/></td>
			<td><s:property value="#s.sex"/></td>
			<td><s:property value="#s.mark"/></td>
			<td><s:property value="#s.cname"/></td>
			<td>
				
			</td>
		</tr>
		</s:iterator>
	</table>
2.分页的tag类导入、z.tld、完成web.xml的配置

第一步导入以前的工具类和自定义标签库
mvc自定义框架(下)之增删改查学习过的。
strtus之crud(增删改查)_Java教程
web.xml配置乱码处理
EncodingFiter

  <filter>
   <filter-name>encodingFiter</filter-name>
   <filter-class>com.myy.three.util.EncodingFiter</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>encodingFiter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
3.dao层去访问数据

实体类:Clazz

package com.myy.three.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;
}
public Clazz(int cid, String cname, String cteacher, String pic) {
	super();
	this.cid = cid;
	this.cname = cname;
	this.cteacher = cteacher;
	this.pic = pic;
}
public Clazz() {
	super();
}
public String toString() {
	return "Clazz [cid=" + cid + ", cname=" + cname + ", cteacher=" + cteacher + ", pic=" + pic + "]";
}
   
	
}

ClzDao

package com.myy.three.dao;

import java.sql.SQLException;
import java.util.List;

import com.myy.three.entity.Clazz;
import com.myy.three.util.BaseDao;
import com.myy.three.util.PageBean;
import com.myy.three.util.StringUtils;

/**
 * 操作数据库
 * @author myy
 *
 */
public class ClzDao extends BaseDao<Clazz>{

	/**
	 * 班级列表查询(带分页)
	 * @param clz
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Clazz> list(Clazz clz,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
	    String sql = "select * from t_struts_class where true";
	    String cname = clz.getCname();
	    int cid = clz.getCid();
	    if(StringUtils.isNotBlank(cname)) {
	    	sql +=" and cname like '%"+cname+"%'";
	    }
	    if(cid != 0) {
	    	sql += " and cid = "+cid;
	    }
		return super.executeQuery(sql, Clazz.class, pageBean);
	}
	
	/**
	 * 班级信息的增加
	 * @param clz
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws SQLException
	 */
	public int add(Clazz clz) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
		String sql = "insert into t_struts_class values(?,?,?,?)";
		return super.executeUpdate(sql, new String[] {"cid","cname","cteacher","pic"}, clz);
	}
	
	/**
	 * 班级信息的删除
	 * @param clz
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws SQLException
	 */
	public int del(Clazz clz) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
		String sql = "delete from t_struts_class where cid = ? ";
		return super.executeUpdate(sql, new String[] {"cid"}, clz);
	}
	
	/**
	 * 班级信息的修改
	 * @param clz
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws SQLException
	 */
	public int edit(Clazz clz) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
		String sql = "update t_struts_class set cname=?,cteacher=?,pic=? where cid = ? ";
		return super.executeUpdate(sql, new String[] {"cname","cteacher","pic","cid"}, clz);
	}
	
	
}

工具类

package com.myy.three.util;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

/**
 * 每一个开发的子控制器要用的属性都定义在通用的action中。
 * @author Administrator
 *
 */
public class BaseAction implements ServletRequestAware, ServletResponseAware{
	/**
	 * 为了传值使用
	 */
	protected HttpServletResponse response;
	protected HttpServletRequest request;
	protected HttpSession session;
	protected ServletContext application;
	
	/**
	 * 为了配置跳转页面所用
	 */
	protected final static String SUCCESS = "success";
	protected final static String FAIL = "fail";
	protected final static String LIST = "list";
	protected final static String ADD = "add";
	protected final static String EDIT = "edit";
	protected final static String DETAIL = "detail";
	
	/**
	 * 具体传值字段	后端向jsp页面传值所用字段
	 */
	protected Object result;
	protected Object msg;
	protected int code;

	public Object getResult() {
		return result;
	}

	public Object getMsg() {
		return msg;
	}

	public int getCode() {
		return code;
	}

	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
		
	}

	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
		this.session = arg0.getSession();
		this.application = arg0.getServletContext();
	}
	

}

4.web层去调用dao层给前台返回数据

web层ClzAction

package com.myy.three.web;

import java.sql.SQLException;

import com.myy.three.dao.ClzDao;
import com.myy.three.entity.Clazz;
import com.myy.three.util.BaseAction;
import com.myy.three.util.PageBean;
import com.opensymphony.xwork2.ModelDriven;

public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
	private ClzDao clzDao = new ClzDao();
	private Clazz clz = new Clazz();

	/**
	 * 查询班级列表的方法
	 * @return
	 */
	public String list() {
		PageBean pageBean = new PageBean();
		pageBean.setRequest(request);
		try {
			this.result = clzDao.list(clz, pageBean);
			request.setAttribute("pageBean", pageBean);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			e.printStackTrace();
		}
		return "list";
	}
	
	/**
	 * 跳转编辑页面(新增编辑页面是同一张)
	 * @return
	 */
	public String preSave() {
//		什么时候跳转新增页面?什么时候跳转编辑页面?
//		区别在于跳编辑页面的时候需要传递cid到后台进行查询,并且将查询到的数据进行回显?
		int cid = clz.getCid();
		if(cid !=0 ) {
			try {
				this.result = clzDao.list(clz, null).get(0);
			} catch (InstantiationException | IllegalAccessException | SQLException e) {
				e.printStackTrace();
			}
		}
		return "preSave";
	}
	
	/**
	 * 增加班级信息
	 * @return
	 */
	public String add() {
		try {
			this.code = this.clzDao.add(clz);
		} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}
	
	/**
	 * 修改班级信息
	 * @return
	 */
	public String edit() {
		try {
			this.code = this.clzDao.edit(clz);
		} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}
	
	/**
	 * 删除班级信息
	 * @return
	 */
	public String del() {
		try {
			this.code = this.clzDao.del(clz);
		} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}

	@Override
	public Clazz getModel() {
		return clz;
	}
	
	
}

5.在struts_sy.xml进行配置

配置

	<action name="clz_*" class="com.myy.three.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>

默认是:forward
chain:Action链式处理的结果类型,也就是将结果转发到这个action中。
redirect:实际上dispatcher和redirect的区别就是在于转发和重定向的区别。
redirectAction:用于直接redirectaction。

6.写jsp

clzList.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/myy" prefix="m"%>
<!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>班级主页</title>
</head>
<body>
<h2>班级目录</h2>
	<br>

	<form action="${pageContext.request.contextPath}/sy/clz_list.action"
		method="post">
		<!-- <input type="hidden" name="rows" value="20"> --> 
		
		班级名称:<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="${result }" 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>&nbsp;
			<a href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${c.cid}">删除</a>
				</td>
			</tr>
		</c:forEach>
	</table>


 <m:page pageBean="${pageBean}"></m: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>书籍的编辑页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath}${result.cname == null ? '/sy/clz_add.action' : '/sy/clz_edit.action'}" method="post">
   编号:<input type="text" value="${result.cid}" name="cid"><br>
  班级名称:<input type="text" value="${result.cname}" name="cname"><br>
   教员:<input type="text" value="${result.cteacher}" name="cteacher"><br>
<%--    图片:<input type="text" value="${result.pic}" name="pic"><br> --%>
   <input type="submit" value="确认">
</form>

</body>
</html>

运行结果:
strtus之crud(增删改查)_Java开发_02
新增:
strtus之crud(增删改查)_Java教程_03
strtus之crud(增删改查)_Java教程_04
修改:
strtus之crud(增删改查)_Java开发_05
strtus之crud(增删改查)_Java教程_06