1、新建Maven项目(详细借鉴上一篇)
2、导入pom.xml依赖(jar依赖)
web.xml改成3.1
导入struts相关的配置文件
jdk1.5-->1.8-->同时pom.xml需要添加plugins>plugin
web.2.3-->3.1
3、导入pom.xml依赖
4、BaseAction
继承 ActionSupport
实现 ServletRequestAware,ServletResponseAware,ModelDriven<T>
①、对于常用的结果进行统一管理,编码错误问题
②、关于tomcat集成常用对象进行统一管理,如:request、response、session
③、封装了携带到页面的常量
package com.zking.util;
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;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public abstract class BaseAction<T> extends ActionSupport implements ServletRequestAware,ServletResponseAware,ModelDriven<T>{
protected HttpServletResponse resp;
protected HttpServletRequest req;
protected HttpSession session;
//1、编码习惯问题,容易出现大小写配置错误
protected static final String LIST="list";
protected static final String TOLIST="toList";
protected static final String TOEDIT="toEdit";
// 2、每个子控制器都要实现对应接口,拿到request、response对象
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.resp=arg0;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.req=arg0;
this.session=arg0.getSession();
}
// 3、向前端页面反馈的数据变量不统一
protected Object result;
protected String msg;
protected int code;
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
5、ClzAction
package com.dhm.web;
import com.dhm.dao.ClzDao;
import com.dhm.entity.Clz;
import com.zking.util.BaseAction;
import com.zking.util.PageBean;
public class ClzAction extends BaseAction<Clz>{
private Clz clz=new Clz();
private ClzDao clzDao=new ClzDao();
@Override
public Clz getModel() {
// TODO Auto-generated method stub
return clz;
}
/**
* 查询班级列表
* @return
* @throws Exception
*/
public String list() throws Exception{
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
this.result=this.clzDao.list(clz, pageBean);
this.req.setAttribute("result", result);
this.req.setAttribute("pageBean", pageBean);
return LIST;
}
/**
* 跳转新增、修改界面
* @return
* @throws Exception
*/
public String toEdit() throws Exception{
int cid = clz.getCid();
if(cid!=0) {
this.result=this.clzDao.list(clz, null).get(0);
this.req.setAttribute("result", result);
}
return TOEDIT;
}
/**
* 往数据库中新增数据
* @return
* @throws Exception
*/
public String add() throws Exception{
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;
}
}
5、ClzDao
package com.dhm.dao;
import java.util.List;
import com.dhm.entity.Clz;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
public class ClzDao extends BaseDao<Clz>{
/**
* 查询方法
* @param clz
* @param pageBean
* @return
* @throws Exception
*/
public List<Clz> list(Clz clz, PageBean pageBean) throws Exception {
String sql="select * from t_struts_class where 1=1";
int cid = clz.getCid();
if(cid!=0) {
sql+=" and cid = "+cid;
}
return super.executeQuery(sql, Clz.class, pageBean);
}
/**
* 增加方法
* @param t
* @throws Exception
*/
public void add( Clz t) throws Exception {
String sql="insert into t_struts_class values(?,?,?,?)";
super.executeUpdate(sql, t, new String[] {"cid","cname","cteacher","pic"});
}
/**
* 修改方法
* @param t
* @throws Exception
*/
public void edit( Clz t) throws Exception {
String sql="update t_struts_class set cname=?,cteacher=?,pic=? where cid=?";
super.executeUpdate(sql, t, new String[] {"cname","cteacher","pic","cid"});
}
/**
* 删除方法
* @param t
* @throws Exception
*/
public void del( Clz t) throws Exception {
String sql="delete from t_struts_class where cid=?";
super.executeUpdate(sql, t, new String[] {"cid"});
}
}
7、struts-sy.xml配置
type属性有四个选项:
1、默认forward:标签体对应的转发页面;
2、action:标签体对应的转发的Action后台方法;
3、redirect:标签体对应的重定向方法:
4、redirectAction:标签体对应的重定向;
<package name="sy" extends="base" namespace="/sy">
<action name="/clz_*" class="com.dhm.web.ClzAction" method="{1}">
<!-- 以前的写法:/book.action?methodName=list
type属性有四个选项:
1、默认forward:标签体对应的转发页面;
2、action:标签体对应的转发的Action后台方法;
3、redirect:标签体对应的重定向方法:
4、redirectAction:标签体对应的重定向;
-->
<result name="toList" type="redirectAction">/clz_list</result>
<result name="list">/clzList.jsp</result>
<result name="toEdit">/clzEdit.jsp</result>
</action>
</package>
8、界面
clzList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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">
<link
href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css"
rel="stylesheet">
<script
src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script>
<title>书籍列表</title>
<style type="text/css">
.page-item input {
padding: 0;
width: 40px;
height: 100%;
text-align: center;
margin: 0 6px;
}
.page-item input, .page-item b {
line-height: 38px;
float: left;
font-weight: 400;
}
.page-item.go-input {
margin: 0 10px;
}
</style>
</head>
<body>
<form class="form-inline"
action="${pageContext.request.contextPath }/sy/clz_list.action" method="post">
<div class="form-group mb-2">
<input type="text" class="form-control-plaintext" name="cname"
placeholder="请输入书籍名称">
<!-- <input name="rows" value="20" type="hidden"> -->
<!-- 不想分页 -->
<input name="pagination" value="false" type="hidden">
</div>
<button type="submit" class="btn btn-primary mb-2">查询</button>
<a class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/sy/clz_toEdit.action">新增</a>
</form>
<table class="table table-striped bg-success">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">班级名</th>
<th scope="col">教员</th>
<th scope="col">图片</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="b" items="${result }">
<tr>
<td>${b.cid }</td>
<td>${b.cname }</td>
<td>${b.cteacher }</td>
<td>${b.pic }</td>
<td>
<a href="${pageContext.request.contextPath }/sy/clz_toEdit.action?cid=${b.cid}">修改</a>
<a href="${pageContext.request.contextPath }/sy/clz_del.action?cid=${b.cid}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- 这一行代码就相当于前面分页需求前端的几十行了 -->
<z:page pageBean="${pageBean }"></z:page>
</body>
</html>
clzEdit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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">
<link
href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css"
rel="stylesheet">
<script
src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script>
<title>书籍新增/修改</title>
</head>
<body>
<form class="form-inline"
action="${pageContext.request.contextPath }/sy/clz_${empty result ? 'add' : 'edit'}.action" method="post">
ID:<input type="text" name="cid" value="${result.cid }"><br>
班级名称:<input type="text" name="cname" value="${result.cname }"><br>
教员:<input type="text" name="cteacher" value="${result.cteacher }"><br>
图片:<input type="text" name="pic" value="${result.pic }"><br>
<input type="submit">
</form>
</body>
</html>
9、界面效果
修改界面
---------------没有了-------------------------