Spring MVC 拦截器
SpringMvc Interceptor拦截器的配置与使用
参考
loginForm.jsp

<%-- Created by IntelliJ IDEA. User: mythsky Date: 2017/12/5 Time: 21:57 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录页面</title> </head> <body> <h3>登录页面</h3> <form action="login" method="post"> <font color="red">${requestScope.message}</font> <table> <tr> <td><label>登录名:</label></td> <td><input type="text" id="loginname" name="loginname"></td> </tr> <tr> <td><label>密码:</label></td> <td><input type="text" id="password" name="password"></td> </tr> <tr><td><input type="submit" value="登录"></td></tr> </table> </form> </body> </html>
main.jsp

<%-- Created by IntelliJ IDEA. User: mythsky Date: 2017/12/5 Time: 22:04 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>书籍</title> </head> <body> <h3>欢迎[${sessionScope.user.username}]</h3> <br> <p>${requestScope.book.bookname}</p> </body> </html>
User

package org.mythsky.springmvcdemo.model; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.web.multipart.MultipartFile; import java.io.Serializable; import java.util.Date; public class User implements Serializable { private String loginname; private String username; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } private Date birthday; private MultipartFile image; public MultipartFile getImage() { return image; } public void setImage(MultipartFile image) { this.image = image; } public User() { super(); } public String getLoginname() { return loginname; } public void setLoginname(String loginname) { this.loginname = loginname; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
UserController

package org.mythsky.springmvcdemo.controller; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.mythsky.springmvcdemo.converter.DateEditor; import org.mythsky.springmvcdemo.model.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpSession; import java.util.Date; @Controller public class UserController { private static final Log logger= LogFactory.getLog(UserController.class); @InitBinder public void initBinder(WebDataBinder binder){ binder.registerCustomEditor(Date.class,new DateEditor()); } @RequestMapping(value = "/{formName}") public String loginForm(@PathVariable String formName){ return formName; } @RequestMapping(value = "/register",method = RequestMethod.POST) public String register(@ModelAttribute User user,Model model){ logger.info(user); model.addAttribute("user",user); return "success"; } @RequestMapping(value = "/login") public ModelAndView login(String loginname, String password, ModelAndView mv, HttpSession session){ if(loginname!=null&&loginname.equals("tom")&&password!=null&password.equals("123456")){ User user=new User(); user.setLoginname(loginname); user.setPassword(password); user.setUsername("管理员"); session.setAttribute("user",user); mv.setViewName("redirect:main"); }else{ mv.addObject("message","登录名或密码错误,请重新输入!"); mv.setViewName("loginForm"); } return mv; } }
BookController

package org.mythsky.springmvcdemo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class BookController { @RequestMapping(value = "/main") public String main(Model model){ model.addAttribute("bookname","hello world"); return "main"; } }
services.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="org.mythsky.springmvcdemo"></context:component-scan> <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>--> <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:annotation-driven></mvc:annotation-driven> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--上传文件大小限制,单位为字节(10MB)--> <property name="maxUploadSize"> <value>10485760</value> </property> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*"/> <bean class="org.mythsky.springmvcdemo.interceptor.AuthorizationInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> </beans>
登录页面
成功登陆页面
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!