一.新建项目
创建一个 Maven项目,具体步骤见:Maven_m0_52681702的博客-CSDN博客一.本期内容为什么使用Maven:一个Java项目要依赖很多jar包,每次下载别人项目后到处是红,得自己一个个从网上下载需要的jar包,麻烦的很。但是如果你用了Maven,我就会有一个配置文件,文件里写清楚我这个项目需要哪个jar包,别人下载你的项目后背后的机制直接按图索骥把你想要的jar包下载下来。背后的机制是什么?就涉及到了仓库和配置文件问题。1、Maven简单介绍Apache Maven是个项目管理和自动构建工具,基于项目对象模型(POM)的概念。作用:完成项目的相关操作,如:编译https://blog.csdn.net/m0_52681702/article/details/120713096?spm=1001.2014.3001.5501
二.导入pom.xml的相关依赖
1.web.xml改成3.1的版本
2.导入struts相关的配置文件
3.jdk1.5改为1.8? ?web2.3改为3.1
4.pom.xml需要添加plugins>plugin
<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.pjl</groupId>
<artifactId>T269_struts</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>T269_struts Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>
<!--添加jar包依赖-->
<!--mysql-->
<mysql.version>8.0.19</mysql.version>
<!--5.其他-->
<junit.version>4.12</junit.version>
<servlet.version>4.0.0</servlet.version>
<jackson.version>2.9.3</jackson.version>
<jstl.version>1.2</jstl.version>
<standard.version>1.1.2</standard.version>
<tomcat-jsp-api.version>8.0.47</tomcat-jsp-api.version>
<commons-beanutils.version>1.9.3</commons-beanutils.version>
<dom4j.version>1.6.1</dom4j.version>
<jaxen.version>1.1.6</jaxen.version>
<struts2-core.version>2.5.13</struts2-core.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2-core.version}</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>${jaxen.version}</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>${dom4j.version}</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>${commons-beanutils.version}</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${standard.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat-jsp-api.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.github.davidcarboni</groupId>
<artifactId>encrypted-file-upload</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>T269_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>
三.导入帮助类以及tld文件
如图:
四.Struts增删改查的编码
思路步骤:1.实体类 2.dao类 3.子控制器
本节课的内容区别在于:BaseAction(优化BaseAction)
? ? ? ? ? ?1.编码习惯问题,容易出现大小写配置错误
? ? ? ? ? ?2.每个子控制器都要实现对应接口,拿到request和respons对象
? ? ? ? ? ?3.向前端页面反馈的数据变量不统一
package com.pjl.crud.web;
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 HttpServletRequest req;
protected HttpServletResponse resp;
protected HttpSession session;
protected static final String LIST = "list";
protected static final String TOLIST = "toList";
protected static final String TOEdit = "toEdit";
@Override
public void setServletResponse(HttpServletResponse response) {
this.resp=response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.req=request;
this.session=request.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;
}
}
第一步:创建实体类Clz
package com.pjl.crud.entity;
public class Clz {
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 "Clz [cid=" + cid + ", cname=" + cname + ", cteacher=" + cteacher + ", pic=" + pic + "]";
}
}
第二步:Dao层ClzDao
package com.pjl.crud.dao;
import java.util.List;
import com.pjl.crud.entity.Clz;
import com.pjl.crud.util.BaseDao;
import com.pjl.crud.util.PageBean;
public class ClzDao extends BaseDao<Clz>{
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);
}
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"});
}
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"});
}
public void del(Clz t) throws Exception {
String sql = "delete t_truts_class where cid=?";
super.executeUpdate(sql,t,new String[] {"cid"});
}
}
第三步:子控制器ClzAction(web层)
package com.pjl.crud.web;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
import com.pjl.crud.dao.ClzDao;
import com.pjl.crud.entity.Clz;
import com.pjl.crud.util.PageBean;
public class ClzAction extends BaseAction<Clz>{
private Clz clz=new Clz();
private ClzDao clzDao =new ClzDao();
/**
* 查询班级列表
* @return
* @throws Exception
*/
public String list() throws Exception {
//首先对pagebean进行初始化
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();
//如果传了cid,则跳修改界面
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;
}
@Override
public Clz getModel() {
return clz;
}
}
第四步:所需界面
ClzEdit.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="http://jsp.veryedu.cn"%>
<!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">
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="submit">
</form>
</body>
</html>
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="http://jsp.veryedu.cn"%>
<!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='bname'
placeholder='请输入书籍名称'>
<!-- 不分页 -->
<!-- <input name="rows" 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>
展示效果:?
?
?
|