<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--1.注册DispatcherServlet -springmvc的核心 前端控制器servlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--关联绑定一个springmvc的配置文件:【servlet-name】-servlet.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!--启动级别-1--> <load-on-startup>1</load-on-startup> </servlet> <!--/ 匹配所有的请求;(不包括.jsp)--> <!--/* 匹配所有的请求;(包括.jsp)--> <!-- 开启映射--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- springmvc主要配置--> <!-- 1.扫描包 让指定包下的注解生效,由IOC容器统一管理 通过这行代码找到控制器类--> <context:component-scan base-package="com.q.controller"/> <!-- 2.解决json的乱码问题--> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="failOnEmptyBeans" value="false"/> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 3.让Spring MVC不处理静态资源 例如:css js html mp3 mp4 --> <mvc:default-servlet-handler/> <!--4.关于拦截器的配置--> <mvc:interceptors> <mvc:interceptor> <!-- 配置需要拦截的路径--> <mvc:mapping path="/**"/> <!-- <mvc:exclude-mapping path="/Login"/>--> <bean id="loginInterceptor" class="com.q.config.LoginInterceptor"/> </mvc:interceptor> <!-- <mvc:interceptor>--> <!-- <mvc:mapping path="/**"/>--> <!-- 排除映射--> <!-- 配置不需要拦截的路径--> <!-- <mvc:exclude-mapping path="/Login"/>--> <!-- <bean class="com.q.config.TestInterceptor" id="testInterceptor"/>--> <!-- </mvc:interceptor>--> </mvc:interceptors> <!-- 注解 驱动 这个就是为了省去1.处理器映射器和2.处理器适配器--> <!-- <mvc:annotation-driven/>--> <!-- 5.视图解析器 internalResourceViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后缀--> <property name="suffix" value=".jsp"/> </bean> </beans>
到这里环境就差不多已经搭建好了,导包那些就通过maven导进来了
com/q/controller/UserController.java信息
package com.q.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; /** * @ClassName UserController * @Author 小林 * @Date 2021/3/31 0:23 * @describe: [控制层信息] **/ @Controller @RequestMapping("/user") public class UserController { //跳转到登陆页面 @RequestMapping("/jumpLogin") public String jumpLogin() throws Exception { return "Login"; } //跳转到成功页面 @RequestMapping("/jumpSuccess") public String jumpSuccess() throws Exception { return "success"; } //登陆提交 @RequestMapping("/Login") public String login(HttpSession session, String username, String pwd) throws Exception { // 向session记录用户身份信息 System.out.println("接收前端===" + username + pwd); session.setAttribute("user", username); session.setAttribute("pwd", pwd); return "success"; } //退出登陆 注销 @RequestMapping("logout") public String logout(HttpSession session) { // session 过期 使无效 session.invalidate(); System.out.println("用户注销:session里面的数据消清。"); return "Login"; } }
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <h1>首页</h1> <hr> <%--登录--%> <a href="${pageContext.request.contextPath}/user/jumpLogin">登录</a> <a href="${pageContext.request.contextPath}/user/jumpSuccess">成功页面</a> </body> </html>
Login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <%--在web-inf里面的所有页面和资源,只能通过controller访问,或者servlet访问,不能直接访问,只能通过服务器去请求跳转,或者进行重定向--%> <h1>登录页面</h1> <hr> <body> <form action="${pageContext.request.contextPath}/user/Login" method="post"> <label> 用户名: <input type="text" name="username"> </label> <br> <label> 密码: <input type="password" name="pwd"> </label> <br> <label> <input type="submit" value="提交"> </label> </form> </body> </html>
success.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h1>登录成功页面</h1> <hr ${user} <a href="${pageContext.request.contextPath}/user/logout">注销</a> </body> </html>
LoginInterceptor登录拦截器:
package com.q.config; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /* * @ClassName LoginInterceptor * @Author 小林 * @Date 2021/3/31 10:19 * @describe: [处理器拦截器] **/ /** * 先继承一个 【处理程序拦截器】 这个自定义的的拦截器就会有拦截的功能,因为要覆写这个拦截器的方法, * 完成后就完成了相关的拦截器功能了 */ public class LoginInterceptor implements HandlerInterceptor { @Override // 预先处理 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 如果是登陆页面则放行 System.out.println("uri: " + request.getRequestURI()); //获取请求URI 判断uri里面是否存在Login,有就放行 if (request.getRequestURI().contains("Login")) { return true; } HttpSession session = request.getSession(); // 如果用户已登陆也放行 if (session.getAttribute("user") != null && session.getAttribute("pwd") != null) { return true; } // 用户没有登陆就跳转到登陆页面 request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); return false; } //后期处理 public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } //完成后 public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } }
过滤器,如果用户没有登录的话就不用进入到成功登录页面,只能调回去登录页面