struts2完成增删改查
思路:
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.psy</groupId>
<artifactId>caoluo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>caoluo 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>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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</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>T224_caoluo</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的配置
/**
* 分页工具类
*
*/
public class PageBean {
private int page = 1;// 页码
private int rows = 4;// 页大小
private int total = 0;// 总记录数
private boolean pagination = true;// 是否分页
// 获取前台向后台提交的所有参数
private Map<String, String[]> parameterMap;
// 获取上一次访问后台的url
private String url;
/**
* 初始化pagebean
*
* @param req
*/
public void setRequest(HttpServletRequest req) {
this.setPage(req.getParameter("page"));
this.setRows(req.getParameter("rows"));
// 只有jsp页面上填写pagination=false才是不分页
this.setPagination(!"fasle".equals(req.getParameter("pagination")));
this.setParameterMap(req.getParameterMap());
this.setUrl(req.getRequestURL().toString());
}
public int getMaxPage() {
return this.total % this.rows == 0 ? this.total / this.rows : this.total / this.rows + 1;
}
public int nextPage() {
return this.page < this.getMaxPage() ? this.page + 1 : this.getMaxPage();
}
public int previousPage() {
return this.page > 1 ? this.page - 1 : 1;
}
public PageBean() {
super();
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public void setPage(String page) {
this.page = StringUtils.isBlank(page) ? this.page : Integer.valueOf(page);
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public void setRows(String rows) {
this.rows = StringUtils.isBlank(rows) ? this.rows : Integer.valueOf(rows);
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return pagination;
}
public void setPagination(boolean pagination) {
this.pagination = pagination;
}
public Map<String, String[]> getParameterMap() {
return parameterMap;
}
public void setParameterMap(Map<String, String[]> parameterMap) {
this.parameterMap = parameterMap;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
/**
* 获得起始记录的下标
*
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination
+ ", parameterMap=" + parameterMap + ", url=" + url + "]";
}
}
z.tld
<description>1.1 core library</description>
<display-name> core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>z</short-name>
<uri>/zking</uri>
<tag>
<name>page</name>
<tag-class>com.tag.PageTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>pageBean</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
web.xml配置
<filter>
<filter-name>encodingFiter</filter-name>
<filter-class>com.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>
dao层去访问数据
public class ClazzDao extends BaseDao<Clazz> {
/**
* 查询分页 查询单个数据公用的方法
* @param clz
* @param pageBean
* @return
*/
public List<Clazz> list(Clazz clz,PageBean pageBean){
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, Clazz.class, pageBean);
}
/**
* 新增方法
* @param clz
* @return
*/
public int add(Clazz clz) {
String sql="insert into t_struts_class values(?,?,?,?)";
return super.executeUpdate(sql, new String[] {"cid","cname","cteacher","pic"}, clz);
}
/**
* 修改方法
* @param clz
* @return
*/
public int edit(Clazz clz) {
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
*/
public int del(Clazz clz) {
String sql="delete from t_struts_class where cid=?";
return super.executeUpdate(sql, new String[] {"cid"}, clz);
}
}
web层调用dao返回数据给jsp
public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
private ClazzDao clzDao=new ClazzDao();
private Clazz clz=new Clazz();
public String list() {
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() {
if(clz.getCid()!=0) {
Clazz c=this.clzDao.list(clz, null).get(0);
request.setAttribute("clz", c);
}
return "preSave";
}
/**
* 新增
* @return
*/
public String add() {
result= this.clzDao.add(clz);
return "toList";
}
/**
* 修改
* @return
*/
public String edit() {
this.clzDao.edit(clz);
return "toList";
}
/**
* 删除
* @return
*/
public String del() {
this.clzDao.del(clz);
return "toList";
}
@Override
public Clazz getModel() {
// TODO Auto-generated method stub
return clz;
}
}
在struts_sy.xml进行配置
<struts>
<package name="sy" extends="base" namespace="/sy">
<action name="/clz_*" class="com.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>
写jsp界面
首页:
<body>
<form action="${pageContext.request.contextPath}/sy/clz_list.action"
method="post">
班级名:<input type="text" name="cname"> <input type="submit">
</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 }">上传图片</a>
</td>
</tr>
</c:forEach>
</table>
<z:page pageBean="${pageBean }"></z:page>
</body>
新增页面与修改公用页面
<body>
<!-- 新增页面与修改页面公用 -->
<form action="${pageContext.request.contextPath }${clz.cname == null ? '/sy/clz_add.action' : '/sy/clz_edit.action'}" method="post">
编号:<input type="text" name="cid" value="${clz.cid }"> <br>
班级名:<input type="text" name="cname" value="${clz.cname }"> <br>
教员:<input type="text" name="cteacher" value="${clz.cteacher }"> <br>
<input type="submit">
</form>
</body>
运行效果图
首页:
新增:
修改:
完成;