strtus值crud

思路
1、导入相关的pom依赖(struts、自定义标签库的依赖)
2、分页的tag类导入、z.tld、完成web.xml的配置
3、dao层去访问数据
4、web层去调用dao层给前台返回数据
5、在struts_sy.xml进行配置
6、写jsp

1、导入相关的pom依赖(struts、自定义标签库的依赖)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lx</groupId>
  <artifactId>struts</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>struts Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
      <dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.44</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.5.13</version>
		</dependency>
		
		
		
		<!-- 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>
  </dependencies>
  <build>
    <finalName>struts</finalName>
    <plugins>
    <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
    </plugins>
  </build>
</project>

2、分页的tag类导入、z.tld、完成web.xml的配置:
在这里插入图片描述

3、dao层去访问数据:
Clazz实体类:

package com.lx.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 + "]";
}
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();
}
   
}

ClzDao:

package com.lx.dao;



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

import com.lx.entity.Clazz;
import com.lx.util.BaseDao;
import com.lx.util.PageBean;
import com.lx.util.StringUtils;

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 SQLException
     * @throws SecurityException 
     * @throws NoSuchFieldException 
     */
    public int add(Clazz clz) throws IllegalArgumentException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException {
    	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 SQLException
     * @throws SecurityException 
     * @throws NoSuchFieldException 
     */
    public int del(Clazz clz) throws IllegalArgumentException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException {
    	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 SQLException
     * @throws SecurityException 
     * @throws NoSuchFieldException 
     */
    public int edit(Clazz clz) throws IllegalArgumentException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException {
    	String sql="update t_struts_class set cname=?,cteacher=?,pic=? where cid = ?";
    	return super.executeUpdate(sql, new String [] {"cname","cteacher","pic","cid"}, clz);
    }
}

BaseAction类:

package com.lx.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;
	}

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

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

}
**4、web层去调用dao层给前台返回数据:**

ClazzAction:

package com.lx.web;

import java.sql.SQLException;

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

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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "list";
    }
    /**
     * 跳转编辑页面(新增编辑页面是同一张)
     * @return
     */
    public String preSave() {
    	int cid = clz.getCid();
    	if(cid!=0) {
    		try {
				this.result=clzDao.list(clz, null).get(0);
			} catch (InstantiationException | IllegalAccessException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
  		return "preSave";
      }
   /**
    * 增加班级信息方法 
    * @return
 * @throws SecurityException 
 * @throws NoSuchFieldException 
    */
    public String add(){
    	try {
			this.code=this.clzDao.add(clz);
		} catch (IllegalArgumentException | IllegalAccessException | SQLException | NoSuchFieldException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  		return "toList";
      }
    
    /**、
     * 修改班级信息
     * @return
     * @throws SecurityException 
     * @throws NoSuchFieldException 
     */
    public String edit()  {
    	try {
			this.code=this.clzDao.edit(clz);
		} catch (IllegalArgumentException | IllegalAccessException | SQLException | NoSuchFieldException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  		return "toList";
      }
    
    /**
     * 删除班级信息
     * @return
     * @throws SecurityException 
     * @throws NoSuchFieldException 
     */
    public String del(){
    	try {
			this.code=this.clzDao.del(clz);
		} catch (IllegalArgumentException | IllegalAccessException | SQLException | NoSuchFieldException | SecurityException 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进行配置:
配置struts-sy文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<!-- 相对mvc的差异性 package:用来将一类子控制器进行分类 http://localhost:8080/T237_struts/sy/user_add.action 
		中/sy对应的namespace="/sy" extends:包的继承 *的含义: *代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,*代表了add -->
	<package name="sy" extends="base" namespace="/sy">
		<action name="/user_*" class="com.lx.web.UserAction"
			method="{1}">
			<result name="success">/test.jsp</result>
		</action>

		<action name="demo_*" class="com.lx.web.DemoAction"
	method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="tomcat_*" class="com.lx.web.TomcatAction"
			method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="ognl_*" class="com.lx.ognl.Demo7" method="{1}">
		<result name="rs">/rs.jsp</result>
		</action>
		
		   <action name="clz_*" class="com.lx.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>
	</package>
</struts>

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="/lx" prefix="x" %>
<!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="bname">
		 <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>


 <x:page pageBean="${pageBean}"></x: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}${result.cname == null ? '/sy/clz_add.action' : '/sy/clz_edit.action'}" method="post">
    cid:<input type="text" name="cid" value="${result.cid }"><br>
    cname:<input type="text" name="cname" value="${result.cname }"><br>
    cteacher:<input type="text" name="cteacher" value="${result.cteacher }"><br>
    <input type="hidden" name="pic" value="${result.pic }"><br>
    <input type="submit" value="ok">
</form>
</body>
</html>

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值