SpringMvc的简单入门(四)之拦截器
程序员文章站
2022-07-09 18:03:42
...
一.过滤器与拦截器的区别
1.当浏览器访问Tomcat的时候过滤器会拦截资源,过滤器什么资源都拦截,过滤器在拦截器之前,是用来拦截servlet的
2.DispatcherServlet*处理器 在所有的action实现之前的拦截,拦截器拦截的是action,拦截器是类似过滤器的一种资源,是SpringMvc内置实现的,必须实现一个接口HandlerInterceptor
二.自定义拦截器
拦截器原理
在mvc-servlet.xml中配置拦截器
<!-- 该拦截器用于拦截URL上参数 -->
<mvc:interceptors>
<!-- 自定义拦截器 -->
<mvc:interceptor>
<!-- 要拦截的资源 path=/*拦截所有-->
<mvc:mapping path="/tm"/>
<!-- 实现拦截器类的路径 -->
<bean class="springmvc.less05.controller.MyInterController"></bean>
</mvc:interceptor>
</mvc:interceptors>
实现接口HandlerInterceptor
package springmvc.less05.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class MyInterController implements HandlerInterceptor {
//在action执行完拦截
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
//在action执行之后拦截
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
//在action执行之前就拦截
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
//获取设置的UUID值
String myToken= request.getParameter("token");
//获取session
Object myToken1= request.getSession().getAttribute("token");
//true 让拦截器过 false则是拦下
//判断是否是重复提交,如何参数有值就是重复提交,需要验证重复提交
if(myToken!=null){
//如果session==null就是重复提交返回false拦截
if(myToken1==null){
return false;
}else{
//如果设置的UUID和session相等就不拦截action,清掉session的值
if(myToken.equals(myToken1)){
request.getSession().removeAttribute("token");
return true;
}else{
return false;
}
}
}else{
return true;
}
}
}
action
package springmvc.less05.controller;
import java.io.OutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import springmvc.less05.dao.MoneyDaoImpl;
@Controller
public class InterController {
@RequestMapping(value="/inter",method=RequestMethod.GET)
public String quert(OutputStream os)throws Exception{
os.write("hello".getBytes());
return null;
}
@Autowired
MoneyDaoImpl mdi;
@RequestMapping(value="/tm",method=RequestMethod.GET)
public String quer(Integer money,OutputStream os)throws Exception{
//根据前台传过来的金额进行扣钱
mdi.updateMoney(money);
//显示剩余的金钱
os.write(("lostedmoney is:"+mdi.selectMoney()).getBytes());
return null;
}
}
业务逻辑层
package springmvc.less05.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class MoneyDaoImpl {
@Autowired
JdbcTemplate jdbc;
//修改金额
public void updateMoney(int money){
String sql="update mymoney set lostedmoney=lostedmoney-"+money+" where usid=1";
jdbc.execute(sql);
}
//查询剩余金额
public int selectMoney(){
String sql="select lostedmoney from mymoney where usid=1";
Integer lostedMoney=jdbc.queryForObject(sql,Integer.class);
return lostedMoney;
}
}
前台页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib tagdir="/WEB-INF/tags" prefix="my"%><!-- 引入防重复提交标签 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'money.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/tm">
扣钱:<input name ='money'>
<!-- 引入防重复提交标签 -->
<my:token></my:token>
<input type="submit" name="扣钱">
</form>
</body>
</html>
三.防重复提交
防重复提交原理
在WEB-INF设置标签 设置隐藏表单域和给session设值
<%@ tag language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@attribute name="tokename" required="false" %>
<%
String ranStr=UUID.randomUUID().toString();
String key=(tokename==null?"token":tokename);
session.setAttribute(key,ranStr);
%>
<input type='hidden' name='<%=key %>' value='<%=ranStr %>'/>
上一篇: Python基础一
下一篇: 深入了解JVM垃圾收集机制
推荐阅读
-
C#之BackgroundWorker从简单入门到深入精通的用法总结
-
JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
-
JAVA WEB快速入门之从编写一个基于SpringMVC框架的网站了解Maven、SpringMVC、SpringJDBC
-
.NET Core实战项目之CMS 第四章 入门篇-Git的快速入门及实战演练
-
10-SpringMVC核心之拦截器与过滤器的不同
-
tensorflow入门之训练简单的神经网络方法
-
SpringMVC入门(二)—— 参数的传递、Controller方法返回值、json数据交互、异常处理、图片上传、拦截器
-
MyBatis从入门到精通(四):MyBatis XML方式的基本用法之增删改
-
软件测试的那些事之接口自动化测试(四):Helloworld入门
-
Python自学之旅 #新手 #MacBook #《“笨办法”学Python》#第四章:言归正传讲Python:Mac系统的终端Terminal命令行快速入门之较复杂的命令