-
@Address 天津
-
@Description:
*/
public class AdminDaoImpl implements AdminDao {
@Override
public void insert(Admin admin) {
Connection conn = null;
PreparedStatement pstm = null;
try {
conn = JdbcUtils.getConnection();
String sql = “insert t_admin value(?,?,?)”;
pstm = conn.prepareStatement(sql);
pstm.setString(1, admin.getId());
pstm.setString(2, admin.getName());
pstm.setString(3, admin.getPassword());
pstm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
System.out.println(“注册异常”);
} finally {
JdbcUtils.close(pstm, null);
}
}
@Override
public Admin selectByNameAndPassword(String name, String password) {
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
Admin admin = null;
try {
conn = JdbcUtils.getConnection();
String sql = “select * from t_admin where name=? and password=?”;
pstm = conn.prepareStatement(sql);
pstm.setString(1, name);
pstm.setString(2, password);
rs = pstm.executeQuery();
if (rs.next()) {
admin = new Admin();
admin.setId(rs.getString(1));
admin.setName(rs.getString(2));
admin.setPassword(rs.getString(3));
}
return admin;
} catch (Exception e) {
e.printStackTrace();
System.out.println(“登录异常”);
throw new RuntimeException(e.getMessage());
} finally {
JdbcUtils.close(rs, pstm, conn);
}
}
}
3、service层[略,详情请参见文章末尾]
4、struts2实现Action层
(1)MyAction:管理员登录、注册 员工:增删改查
package com.tjcu.action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
/**
-
@author 王恒杰
-
@version 1.0
-
@date 2021/10/10 20:48
-
@email 1078993387@qq.com
-
@Address 天津
-
@Description:
*/
public class DownloadAction extends ActionSupport {
//1接收用户要下载的文件名称
private String fileName;
//服务器上下载文件的存储文件夹
private String filePath;
//服务方法用于文件下载
public InputStream getInputStream() throws Exception {
//根据相对路径获取下载文件的绝对路径
String realPath = ServletActionContext.getRequest().getSession().getServletContext().getRealPath(filePath);
FileInputStream is = new FileInputStream(realPath + “\” + fileName);
System.out.println(is);
//将读取的文件输入流直接返回到Client
return is;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
(2)UploadAction:上传功能
package com.tjcu.action;
import com.opensymphony.xwork2.ActionSupport;
import com.tjcu.entity.Admin;
import com.tjcu.entity.User;
import com.tjcu.service.AdminService;
import com.tjcu.service.impl.AdminServiceImpl;
import com.tjcu.service.impl.UserServieImpl;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;
/**
-
@author 王恒杰
-
@version 1.0
-
@date 2021/9/29 19:50
-
@email 1078993387@qq.com
-
@Address 天津
-
@Description:
*/
public class MyAction extends ActionSupport {
private String password1;
private String password2;
private String name;
private Admin admin;
private User user;
private String[] isCheck;
private String code;
public String register(){
// 管理员注册
if(password1.equals(password2)){
Admin admin1= new Admin(null, name, password1);
System.out.println(admin1);
AdminService adminService = new AdminServiceImpl();
adminService.register(admin1);
return “registerOk”;
}else {
return “registerError”;
}
}
public String login() throws UnsupportedEncodingException {
// 管理员登录
HttpServletRequest request = ServletActionContext.getRequest();
request.setCharacterEncoding(“utf-8”);
AdminService adminService = new AdminServiceImpl();
Admin admin1 = adminService.login(admin.getName(), admin.getPassword());
System.out.println(admin1);
HttpSession session = request.getSession();
String code1 = (String) session.getAttribute(“code”);
if(code.equals(code1)){
if(admin1!=null){
System.out.println(admin1.getName());
request.getSession().setAttribute(“admin”,admin.getName());
return “loginOk”;
}else{
return “loginError”;
}
}else{
return “loginError”;
}
}
public String userAdd(){
// 添加用户
UserServieImpl userServie = new UserServieImpl();
User user2 = new User(null,user.getUsername(),user.getSalary(),user.getAge());
userServie.add(user2);
return “useraddOk”;
}
public String userDelete(){
// 删除用户
UserServieImpl userServie = new UserServieImpl();
userServie.drop(user.getId());
System.out.println(user.getId());
return “userDeleteOk”;
}
public String deleteAll(){
// 批量删除用户
UserServieImpl userServie = new UserServieImpl();
for (String id : isCheck) {
userServie.drop(id);
}
System.out.println(isCheck);
return “dropAllOk”;
}
public String userSelectById(){
// 根据id查用户
UserServieImpl userServie = new UserServieImpl();
User user1 = userServie.selectById(user.getId());
System.out.println(user1);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute(“user”,user1);
return “userSelectOk”;
}
public String userShow(){
// 展示用户
HttpServletRequest request = ServletActionContext.getRequest();
if(request.getSession().getAttribute(“admin”)!=null) {
UserServieImpl userServie = new UserServieImpl();
List users = userServie.show();
request.setAttribute(“users”, users);
return “userShowOk”;
}else {
return “userShowError”;
}
}
public String userUpdate(){
// 更新用户
UserServieImpl userServie = new UserServieImpl();
User user1 = new User(user.getId(),user.getUsername(),user.getSalary(),user.getAge());
System.out.println(“update”+user1);
userServie.update(user1);
return “userUpdateOk”;
}
public String session(){
HttpServletRequest request = ServletActionContext.getRequest();
request.getSession().invalidate();
return “sessionOk”;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Admin getAdmin() {
return admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getIsCheck() {
return isCheck;
}
public void setIsCheck(String[] isCheck) {
this.isCheck = isCheck;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
(3)DownloadAction:下载功能
package com.tjcu.action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.IOException;
/**
-
@author 王恒杰
-
@version 1.0
-
@date 2021/10/10 20:27
-
@email 1078993387@qq.com
-
@Address 天津
-
@Description:
*/
public class UploadAction extends ActionSupport {
//接收数据
private File upload;
// Struts2框架提供数据获取上传文件原名方式 成员变量 名字:上传文件+“FileName”
private String uploadFileName;
// 获取当前上传文件类型 成员变量 名字:上传文件+ContentType MIMA类型 大类型/小类型
private String uploadContentType;
//Struts2的Action成员变量的作用
//1、接收Client请求参数 2.替换Request作用域 3.可以通过Struts2配置文件为Action的成员变量赋值
// 可以通过Struts2配置文件为Action的成员变量赋值 String path
private String filePath;
@Override
public String execute() throws Exception {
System.out.println(“文件名” + uploadFileName);
System.out.println(“文件类型” + uploadContentType);
System.out.println(filePath);
// 可以通过相对路径获取绝对路径 ServletContetx.getRealPath(“path”) 通过相对路径获取绝对路径
String realPath = ServletActionContext.getRequest().getSession().getServletContext().getRealPath(filePath);
System.out.println(realPath);
FileUtils.copyFile(upload, new File(realPath +“//”+ uploadFileName));
//跳转页面
return SUCCESS;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentTtpe) {
this.uploadContentType = uploadContentTtpe;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
(4)ValidationCodeAction:验证码功能
package com.tjcu.action;
/**
-
用于生成验证码图片 不提供返回页面
-
@author 86151
*/
public class ValidationCodeAction implements Action {
private static final long serialVersionUID = 5126616339795936447L;
private ConfigurableCaptchaService configurableCaptchaService = null;
private ColorFactory colorFactory = null;
private RandomFontFactory fontFactory = null;
private RandomWordFactory wordFactory = null;
private TextRenderer textRenderer = null;
public void init() throws ServletException {
configurableCaptchaService = new ConfigurableCaptchaService();
// 颜色创建工厂,使用一定范围内的随机色
colorFactory = new RandomColorFactory();
configurableCaptchaService.setColorFactory(colorFactory);
// 随机字体生成器
fontFactory = new RandomFontFactory();
fontFactory.setMaxSize(32);
fontFactory.setMinSize(28);
configurableCaptchaService.setFontFactory(fontFactory);
// 随机字符生成器,去除掉容易混淆的字母和数字,如o和0等
wordFactory = new RandomWordFactory();
wordFactory.setCharacters(“abcdefghkmnpqstwxyz23456789”);
wordFactory.setMaxLength(5);
wordFactory.setMinLength(4);
configurableCaptchaService.setWordFactory(wordFactory);
// 自定义验证码图片背景
MyCustomBackgroundFactory backgroundFactory = new MyCustomBackgroundFactory();
configurableCaptchaService.setBackgroundFactory(backgroundFactory);
// 图片滤镜设置
ConfigurableFilterFactory filterFactory = new ConfigurableFilterFactory();
List filters = new ArrayList();
WobbleImageOp wobbleImageOp = new WobbleImageOp();
wobbleImageOp.setEdgeMode(AbstractImageOp.EDGE_MIRROR);
wobbleImageOp.setxAmplitude(2.0);
wobbleImageOp.setyAmplitude(1.0);
filters.add(wobbleImageOp);
filterFactory.setFilters(filters);
configurableCaptchaService.setFilterFactory(filterFactory);
// 文字渲染器设置
textRenderer = new BestFitTextRenderer();
textRenderer.setBottomMargin(3);
textRenderer.setTopMargin(3);
configurableCaptchaService.setTextRenderer(textRenderer);
// 验证码图片的大小
configurableCaptchaService.setWidth(82);
configurableCaptchaService.setHeight(32);
}
@Override
public String execute() throws Exception {
init();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType(“image/png”);
response.setHeader(“cache”, “no-cache”);
HttpSession session = request.getSession(true);
OutputStream outputStream = response.getOutputStream();
// 得到验证码对象,有验证码图片和验证码字符串
Captcha captcha = configurableCaptchaService.getCaptcha();
// 取得验证码字符串放入Session
String validationCode = captcha.getChallenge();
session.setAttribute(“code”, validationCode);
// 取得验证码图片并输出
BufferedImage bufferedImage = captcha.getImage();
ImageIO.write(bufferedImage, “png”, outputStream);
outputStream.flush();
outputStream.close();
destroy();
return null;
}
public void destroy() {
wordFactory = null;
colorFactory = null;
fontFactory = null;
textRenderer = null;
configurableCaptchaService = null;
}
/**
- 自定义验证码图片背景,主要画一些噪点和干扰线
*/
private class MyCustomBackgroundFactory implements BackgroundFactory {
private Random random = new Random();
@Override
public void fillBackground(BufferedImage image) {
Graphics graphics = image.getGraphics();
// 验证码图片的宽高
int imgWidth = image.getWidth();
int imgHeight = image.getHeight();
// 填充为白色背景
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, imgWidth, imgHeight);
// 画100个噪点(颜色及位置随机)
for (int i = 0; i < 100; i++) {
// 随机颜色
int rInt = random.nextInt(255);
int gInt = random.nextInt(255);
int bInt = random.nextInt(255);
graphics.setColor(new Color(rInt, gInt, bInt));
// 随机位置
int xInt = random.nextInt(imgWidth - 3);
int yInt = random.nextInt(imgHeight - 2);
// 随机旋转角度
int sAngleInt = random.nextInt(360);
int eAngleInt = random.nextInt(360);
// 随机大小
int wInt = random.nextInt(6);
int hInt = random.nextInt(6);
graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);
// 画5条干扰线
if (i % 20 == 0) {
int xInt2 = random.nextInt(imgWidth);
int yInt2 = random.nextInt(imgHeight);
graphics.drawLine(xInt, yInt, xInt2, yInt2);
}
}
}
}
}
5、util层
- 手动封装JDBCUtils工具类
package com.tjcu.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
-
@author 王恒杰
-
@version 1.0
-
@date 2021/9/11 12:38
-
@email 1078993387@qq.com
-
@Address 天津
-
@Description:
*/
public class JdbcUtils {
// 静态的Properties集合,相当于属性
private static Properties p = new Properties();
// 静态的ThreadLocal输性 线程绑定对象
private static final ThreadLocal t = new ThreadLocal();
static {
InputStream is = JdbcUtils.class.getResourceAsStream(“/com/tjcu/jdbc.properties”);
try {
p.load(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
//从ThreadLocal中获取Connection
Connection conn = t.get();
try {
if (conn == null) {
Class.forName(p.getProperty(“driver”));
conn = DriverManager.getConnection(p.getProperty(“url”), p.getProperty(“username”), p.getProperty(“password”));
t.set(conn);
}
} catch (Exception e) {
e.printStackTrace();
}
return t.get();
}
public static void close(ResultSet rs, PreparedStatement pstm, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (pstm != null) {
try {
pstm.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
t.remove();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(ResultSet rs, PreparedStatement pstm) {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (pstm != null) {
try {
pstm.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(PreparedStatement pstm, Connection conn) {
if (pstm != null) {
try {
pstm.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
//关闭链接后切记使用remove方法,移除ThreadLocal中已关闭的链接对象
t.remove();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//关闭资源 用于关闭连接
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
//关闭链接后切记使用remove方法,移除ThreadLocal中已关闭的链接对象
t.remove();
} catch (Exception e) {
}
}
}
}
6、config 配置文件:jdbc.properties
- mysql-connector-java-8.0.16.jar之后com.mysql.cj.jdbc.Driver
driver=com.mysql.cj.jdbc.Driver
username=root
password=root
url=jdbc:mysql://localhost:3306/msc?useUnicode=true&characterEncoding=UTF-8 & useSSL=false & serverTimezone=Asia/Shanghai
源代码在githee仓库:
程序员小王Gitee: https://gitee.com/wanghengjie563135/EMS.git
文末
👉在此,鸣谢:刘浩老师讲解
📌 作者:王恒杰
📃 更新: 2021.10.18
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数同学面临毕业设计项目选题时,很多人都会感到无从下手,尤其是对于计算机专业的学生来说,选择一个合适的题目尤为重要。因为毕业设计不仅是我们在大学四年学习的一个总结,更是展示自己能力的重要机会。
因此收集整理了一份《2024年计算机毕业设计项目大全》,初衷也很简单,就是希望能够帮助提高效率,同时减轻大家的负担。
既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!
由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频
如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
//关闭链接后切记使用remove方法,移除ThreadLocal中已关闭的链接对象
t.remove();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//关闭资源 用于关闭连接
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
//关闭链接后切记使用remove方法,移除ThreadLocal中已关闭的链接对象
t.remove();
} catch (Exception e) {
}
}
}
}
6、config 配置文件:jdbc.properties
- mysql-connector-java-8.0.16.jar之后com.mysql.cj.jdbc.Driver
driver=com.mysql.cj.jdbc.Driver
username=root
password=root
url=jdbc:mysql://localhost:3306/msc?useUnicode=true&characterEncoding=UTF-8 & useSSL=false & serverTimezone=Asia/Shanghai
源代码在githee仓库:
程序员小王Gitee: https://gitee.com/wanghengjie563135/EMS.git
文末
👉在此,鸣谢:刘浩老师讲解
📌 作者:王恒杰
📃 更新: 2021.10.18
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数同学面临毕业设计项目选题时,很多人都会感到无从下手,尤其是对于计算机专业的学生来说,选择一个合适的题目尤为重要。因为毕业设计不仅是我们在大学四年学习的一个总结,更是展示自己能力的重要机会。
因此收集整理了一份《2024年计算机毕业设计项目大全》,初衷也很简单,就是希望能够帮助提高效率,同时减轻大家的负担。
[外链图片转存中…(img-qzGtYIzt-1712577655323)]
[外链图片转存中…(img-QN7tJpbo-1712577655323)]
[外链图片转存中…(img-DXkw6SoT-1712577655323)]
既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!
由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频
如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
[外链图片转存中…(img-RXJBHJT6-1712577655324)]