实验报告6-SSM框架整合

资料下载链接

实验报告6-SSM框架整合(1验证码)

实验报告6-SSM框架整合(2管理员登录)

实验报告6-SSM框架整合(3商品的分页查询)

实验报告6-SSM框架整合(4权限拦截器)

一、需求分析

使用普通整合方式实现SSM(SpringMVC、Spring和MyBatis)整合,实现管理员登录、后台首页和图书管理功能。

二、编码实现

1、初始代码

idea运行Exp6项目,启动Tomcat,显示HelloWorld页面

2、配置静态资源的访问映射

webapp目录下,新建/static

static目录下,导入layuimini-v2

applicationContext.xml

    <!-- 配置静态资源的访问映射 --><mvc:resources mapping="/static/**" location="/static/" />

测试。地址栏中输入“http://localhost:8080/static/layuimini-v2/images/bg.jpg”,能正常显示图片

3、验证码

java目录,com.sw.controller包

@Controller
@RequestMapping("/admin")
public class AdminController {@GetMapping("/login")public String login(){return "/admin/login";}
}

webapp目录,/pages/admin/login.jsp,复制layuimini-v2/page/login-2.html,并修改静态资源引用路径

com.sw.controller包,CommonController

@Controller
@RequestMapping("/common")
public class CommonController {@GetMapping("/createCaptcha")public void createCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {Captcha captcha = new ArithmeticCaptcha(115, 42);//算术类型captcha.setCharType(2);//本次产生的验证码String text = captcha.text();System.out.println(text);//将验证码进行缓存HttpSession session = request.getSession();session.setAttribute("captcha",text);//将生成的验证码图片通过输出流写回客户端浏览器页面captcha.out(response.getOutputStream());}
}

webapp目录,/pages/user/login.jsp

定义jquery

            $ = layui.jquery,

修改验证码图片鼠标悬停样式

.admin-captcha {cursor: pointer}

设置验证码图片的src属性,并设置单击事件

<img class="admin-captcha" src="/common/createCaptcha" onclick="changeCaptcha()">
        window.changeCaptcha=function () {var img = $("img.admin-captcha")img.attr("src","/common/createCaptcha?t=" + new Date().getTime())}

com.sw.util包,引入ApiResult类

com.sw.controller包,CommonController

    @GetMapping("/checkCaptcha")@ResponseBodypublic ApiResult checkCaptcha(String captcha,HttpServletRequest request){ApiResult result = new ApiResult();//数据校验if (captcha==null || captcha==""){result.setErrorCode();result.setMsg("验证码为空");return result;}//整数校验Integer captchaFront = 0;try {captchaFront = Integer.parseInt(captcha);}catch (Exception ex){System.out.println(ex.getMessage());result.setErrorCode();result.setMsg("验证码格式错误");return result;}String str = request.getSession().getAttribute("captcha").toString();Integer sessionCaptcha = Integer.parseInt(str);if (!sessionCaptcha.equals(captchaFront)){result.setErrorCode();result.setMsg("验证码错误");}return result;}

webapp目录,/pages/admin/login.jsp

            //验证校验码var captchaFlag = true$.ajax({//同步请求async: false,//请求地址url:"/common/checkCaptcha",//传递的数据data:{captcha:data.captcha},//返回数据类型dataType:"json",success:function(res){if (res.status != 200) {layer.alert(res.msg)captchaFlag = falsereturn false}},error: function () {layer.msg("系统异常");return false}})if (!captchaFlag){return false}
4、管理员登录

com.sw.pojo包,User

public class User {private int id;private String username;private String password;private String role;//get、set//tostring
}

com.sw.mapper包,UserMapper

public interface UserMapper {User getOne(User userFront);
}

com/sw/mapper目录,UserMapper.xml

    <select id="getOne" parameterType="User" resultType="User">select * from t_user where username=#{username} and password=#{password} and role=#{role}</select>

com.sw.service包,UserService

    User login(User userFront);

com.sw.service.impl包,UserServiceImpl

@Service("userService")
public class UserServiceImpl implements UserService {@Resourceprivate UserMapper userMapper;@Overridepublic User login(User userFront) {return userMapper.getOne(userFront);}
}

com.sw.util包,MyConst

    public static final String ADMIN_SESSION = "ADMIN_SESSION.17291#$%&*";

com.sw.util包,MD5Util

public class MD5Util {public static String encryptMD5(String input) {try {// 创建MD5加密对象MessageDigest md5 = MessageDigest.getInstance("MD5");// 执行加密操作byte[] messageDigest = md5.digest(input.getBytes());// 将字节数组转换为16进制字符串StringBuilder hexString = new StringBuilder();for (byte b : messageDigest) {String hex = Integer.toHexString(0xff & b);if (hex.length() == 1) {hexString.append('0');}hexString.append(hex);}// 返回加密后的字符串return hexString.toString();} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);}}
}

com.sw.controller包,AdminController

    @PostMapping("/login")@ResponseBodypublic ApiResult login(User user, HttpServletRequest request){ApiResult result = new ApiResult();//数据校验if (user==null||user.getUsername().equals("")||user.getPassword().equals("")){result.setErrorCode();result.setMsg("后台数据校验失败");}String md5 = MD5Util.encryptMD5(user.getPassword());user.setPassword(md5);user.setRole("admin");User userDb = userService.login(user);//登录失败if (userDb==null){result.setErrorCode();result.setMsg("用户名或者密码错误");return result;}userDb.setPassword("");request.getSession().setAttribute(MyConst.ADMIN_SESSION,userDb);return result;}

webapp目录,/pages/admin/login.jsp

            //异步登录$.ajax({//请求地址url:"/admin/login",type:"post",//传递的数据data:{username:data.username,password:data.password},//返回数据类型dataType:"json",success:function(res){if (res.status != 200) {layer.alert(res.msg)return false}else {window.location = '/admin/index';}},error: function () {layer.msg("系统异常");return false}})

com.sw.controller包,AdminController

    @GetMapping("/index")public String index(){return "/admin/index";}

webapp目录,新建/pages/admin/index.jsp

5、后台首页

webapp目录,/pages/admin/index.jsp,复制layuimini-v2/index.html,并修改静态资源引用路径

com.sw.controller包,ProductController

@Controller
@RequestMapping("/product")
public class ProductController {@GetMapping("/index")public String index(){return "/product/index";}
}

webapp目录,新建/pages/product/index.jsp,复制layuimini-v2/page/table.html,并修改静态资源引用路径

webapp目录,/static/layuimini-v2/api/init.json,删除“主页模板”目录,将“菜单管理”修改为“商品管理”,href指向“/product/index”

6、商品的分页查询

com.sw.pojo包,Product

public class Product {private int id;private String name;private double price;//get、set//tostring
}

com.sw.mapper包,ProductMapper

public interface ProductMapper {List<Product> getList(Product product);
}

com/sw/mapper目录,ProductMapper.xml

    <select id="getList" resultType="Product" parameterType="Product">select * from t_product<where><if test="name!=null and name !=''">and name like concat('%',#{name},'%')</if></where></select>

com.sw.service包,ProductService

    PageInfo<Product> page(int pageNum, int pageSize, Product product);

com.sw.service.impl包,ProductServiceImpl

@Service("productService")
public class ProductServiceImpl implements ProductService {@Resourceprivate ProductMapper productMapper;@Overridepublic PageInfo<Product> page(int pageNum, int pageSize, Product product) {PageHelper.startPage(pageNum,pageSize);PageInfo<Product> pageInfo = new PageInfo(productMapper.getList(product));return pageInfo;}
}

com.sw.util包,MyConst

    public static final Integer PAGE_NUM = 1;public static final Integer PAGE_SIZE = 10;

com.sw.controller包,ProductController

    @PostMapping("/page")@ResponseBodypublic ApiResult<PageInfo<Product>> page(Integer pageNum, Integer pageSize, Product product){ApiResult result = new ApiResult();pageNum = pageNum > 0 ? pageNum : MyConst.PAGE_NUM;pageSize = pageSize > 0 ? pageSize : MyConst.PAGE_SIZE;PageInfo<Product> page = productService.page(pageNum, pageSize, product);result.setData(page);return  result;}

webapp目录,/pages/product/index.jsp

        //初始化分页表格
​form.render()
​url: '/product/page',method:"post",cols: [[{ type:"numbers", width: 60, title: '序号'},{field: 'name', width: 280, title: '商品名'},{field: 'price', width: 80, title: '价格'},]],parseData: function(res){ //res 即为原始返回的数据console.log(res)return {"code": 0, //解析接口状态"msg": res.msg, //解析提示文本"count": res.data.total, //解析数据长度"data":  res.data.list //解析数据列表};},request: {pageName: 'pageNum' //页码的参数名称,默认:page,limitName: 'pageSize' //每页数据量的参数名,默认:limit}

webapp目录,/pages/product/index.jsp

修改第一个输入框的lable为“商品名”

            //执行搜索重载table.reload('currentTableId', {url: '/product/page',method:"post",where:{name:data.field.name,},parseData: function(res){ //res 即为原始返回的数据console.log(res)return {"code": 0, //解析接口状态"msg": res.msg, //解析提示文本"count": res.data.total, //解析数据长度"data":  res.data.list //解析数据列表};},request: {pageName: 'pageNum', //页码的参数名称limitName: 'pageSize' //每页数据量的参数名}}, 'data');return false;
7、权限拦截器

com.sw.interceptor包,MyAdminInterceptor

public class MyAdminInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//合法用户拥有访问权限User userSession = (User) request.getSession().getAttribute(MyConst.ADMIN_SESSION);if(userSession!=null){if(userSession.getRole().equals("admin")){return true;}}//非法用户跳转至登录页面response.sendRedirect("/admin/login");return false;}
}

applicationContext.xml

<!--配置拦截器-->
<mvc:interceptors><!--管理员权限拦截器--><mvc:interceptor><!--需要拦截的请求--><mvc:mapping path="/admin/*"/><mvc:mapping path="/product/*"/><!--放行的请求--><mvc:exclude-mapping path="/admin/login"/><mvc:exclude-mapping path="/common/*"/><!--拦截器全限定名--><bean class="com.sw.interceptor.MyAdminInterceptor"/></mvc:interceptor>
</mvc:interceptors>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/21278.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++20 范围(Range):简化集合操作

C20 范围&#xff1a;简化集合操作 一、范围&#xff08;Range&#xff09;的目的二、在模板函数中使用范围概念三、投影四、视图五、结论 一、范围&#xff08;Range&#xff09;的目的 在 C20 中&#xff0c;范围概念要求一个对象同时拥有迭代器和结束哨兵。这在标准集合的上…

YOLOv5改进(五)-- 轻量化模型MobileNetv3

文章目录 1、MobileNetV3论文2、代码实现2.1、MobileNetV3-small2.2、MobileNetV3-large 3、运行效果4、目标检测系列文章 1、MobileNetV3论文 Searching for MobileNetV3论文 MobileNetV3代码 MobileNetV3 是 Google 提出的一种轻量级神经网络结构&#xff0c;旨在在移动设备上…

官网上线,一款令人惊艳的文本转语音模型:ChatTTS

近日&#xff0c;一个名为 ChatTTS 文本转语音模型的项目在github上横空出世&#xff0c;一经推出便引发极大关注&#xff0c;短短四天时间&#xff0c;已经狂揽了14.2k的Start量。 ChatTTS是一款专为对话场景设计的支持中英文的文本转语音&#xff08;TTS&#xff09;模型&…

Codeforces Global Round 17 C. Keshi Is Throwing a Party 题解 二分答案

Keshi Is Throwing a Party 题目描述 Keshi is throwing a party and he wants everybody in the party to be happy. He has n n n friends. His i i i-th friend has i i i dollars. If you invite the i i i-th friend to the party, he will be happy only if at m…

未来已来:Spring Boot引领数据库智能化革命

深入探讨了Spring Boot如何与现代数据库技术相结合&#xff0c;预测并塑造未来的数据访问趋势。本书不仅涵盖了Spring Data JPA的使用技巧&#xff0c;还介绍了云原生数据库的概念&#xff0c;微服务架构下的数据访问策略&#xff0c;以及AI在数据访问层的创新应用。旨在帮助开…

微信小程序如何进行页面跳转

微信小程序中的页面跳转可以通过多种方式实现&#xff0c;以下是几种主要的跳转方式及其详细解释&#xff1a; wx.navigateTo 功能&#xff1a;保留当前页面&#xff0c;跳转到应用内的某个页面。特点&#xff1a; 可以在新页面使用wx.navigateBack返回原页面。每跳转一个新页…

XFeat:速度精度远超superpoint的轻量级图像匹配算法

代码地址&#xff1a;https://github.com/verlab/accelerated_features?tabreadme-ov-file 论文地址&#xff1a;2404.19174 (arxiv.org) XFeat (Accelerated Features)重新审视了卷积神经网络中用于检测、提取和匹配局部特征的基本设计选择。该模型满足了对适用于资源有限设备…

在table中获取每一行scope的值

目的 当前有一份如下数据需要展示在表格中&#xff0c;表格的页面元素套了一个折叠面板&#xff0c;需要循环page_elements中的数据展示出来 错误实践 将template放在了折叠面板中&#xff0c;获取到的scope是空数组 <el-table-column label"页面元素" show-o…

【并发程序设计】15.信号灯(信号量)

15.信号灯(信号量) Linux中的信号灯即信号量是一种用于进程间同步或互斥的机制&#xff0c;它主要用于控制对共享资源的访问。 在Linux系统中&#xff0c;信号灯作为一种进程间通信&#xff08;IPC&#xff09;的方式&#xff0c;与其他如管道、FIFO或共享内存等IPC方式不同&…

分析和设计算法

目录 前言 循环不变式 n位二进制整数相加问题 RAM模型 使用RAM模型分析 代码的最坏情况和平均情况分析 插入排序最坏情况分析 插入排序平均情况分析 设计算法 分治法 总结 前言 循环迭代&#xff0c;分析算法和设计算法作为算法中的三个重要的角色&#xff0c;下面…

C/C++|基于回调函数实现异步操作

首先&#xff0c;要搞懂一点&#xff0c;异步操作本质上也是并发&#xff0c;我们想要在线程级别实现异步并发基本就靠三种方式&#xff1a; 多线程并发回调函数协程 今天我们讨论的是回调函数&#xff0c;我们如何通过回调函数来实现异步操作呢&#xff1f; 非阻塞I/O操作回…

Java——二进制原码、反码和补码

一、简要介绍 原码、反码和补码只是三种二进制不同的表示形式&#xff0c;每个二进制数都有这三个形式。 1、原码 原码是将一个数的符号位和数值位分别表示的方法。 最高位为符号位&#xff0c;0表示正&#xff0c;1表示负&#xff0c;其余位表示数值的绝对值。 例如&…

力扣刷题--LCP 66. 最小展台数量【简单】

题目描述&#x1f357; 力扣嘉年华将举办一系列展览活动&#xff0c;后勤部将负责为每场展览提供所需要的展台。 已知后勤部得到了一份需求清单&#xff0c;记录了近期展览所需要的展台类型&#xff0c; demand[i][j] 表示第 i 天展览时第 j 个展台的类型。 在满足每一天展台需…

C# SolidWorks 二次开发-显示配置

在 SolidWorks 的二次开发中&#xff0c;显示配置&#xff08;Display States&#xff09;是一个非常重要的功能。显示配置允许用户在同一个配置&#xff08;Configuration&#xff09;下保存不同的显示状态&#xff0c;如隐藏或显示的零件、不同的颜色和材质等。本文将向新的开…

PostgreSQL LATERAL 的工作原理

LATERAL 的工作原理 外部查询生成一行结果&#xff1a;LATERAL 子查询会对每一行外部查询结果进行评估。LATERAL 子查询执行&#xff1a;对于每一行&#xff0c;LATERAL 子查询会使用该行的列值来执行自己的查询。结果合并&#xff1a;子查询的结果与外部查询的行合并&#xf…

URL 与域名的关系

URL、域名和DNS 是互联网上资源定位和访问的关键要素&#xff0c;它们之间有紧密的关系。 URL 与域名的关系 URL&#xff08;Uniform Resource Locator&#xff09; URL 是统一资源定位符&#xff0c;用于标识互联网上的资源。它不仅包含域名&#xff0c;还包括访问资源所需…

如何解决游戏行业DDOS攻击问题

随着网络游戏行业的迅速发展&#xff0c;网络游戏问题也不可忽视&#xff0c;特别是目前网络攻击频发&#xff0c;DDoS攻击的简单化以及普及化&#xff0c;对游戏来说存在非常大的安全威胁。 随着受攻击对象的范围在不断地拓展&#xff0c;网络游戏这种这种新型并且有着丰厚利…

Scala编程基础3 数组、映射、元组、集合

Scala编程基础3 数组、映射、元组、集合 小白的Scala学习笔记 2024/5/23 14:20 文章目录 Scala编程基础3 数组、映射、元组、集合apply方法数组yield 数组的一些方法映射元组数据类型转换求和示例拉链集合flatMap方法 SetHashMap apply方法 可以new&#xff0c;也可以不new&am…

flink Jobmanager metaspace oom 分析

文章目录 现象作业背景分析现象分析类卸载条件MAT 分析 解决办法flink 官方提示 现象 通过flink 页面提交程序&#xff0c;多次提交后&#xff0c;jobmanager 报metaspace oom 作业背景 用户代码是flink 代码Spring nacos 分析 现象分析 从现象来看肯定是因为有的类没有被…

Python教程-快速入门基础必看课程06-List索引

摘要 该视频主要讲述了Python中for循环的基本结构和用法&#xff0c;特别是针对处理复杂数据结构如list of list的情况。首先介绍了for循环的基本概念&#xff0c;然后通过实例详细解释了如何遍历list中的元素&#xff0c;并解决了单层for循环无法处理list of list的问题。视频…