Springmvc JSR303 and interceptor

  • JSR303
  • Interceptor

1.JSR303

What is JSR303

JSR is the abbreviation of Java Specification Requests, which means Java specification proposal. It is a formal request to JCP (Java Community Process) to add a standardized technical specification. Anyone can submit a JSR to add new APIs and services to the Java platform. JSR has become an important standard in the Java world. JSR-303 is a sub-specification in JAVA EE 6 called Bean Validation. Hibernate Validator is the reference implementation of Bean Validation. Hibernate Validator provides implementation of all built-in constraints in the JSR 303 specification, in addition to Some additional constraints.

> Validating data is a common task that occurs in all application layers from the presentation layer to the persistence layer. Often the same validation logic is implemented at every layer, which is time-consuming and error-prone. To avoid duplicating these validations, developers often bundle validation logic directly into the domain model, mixing domain classes with validation code that is actually metadata about the classes themselves.

Why use JSR303

Hasn’t the front end already verified the data? Why do we still need to do verification? Why don’t we just use it? It's sloppy. If the front-end code verification is not written well, or for people who know a little programming, they can directly bypass the front-end and send requests (make extraordinary data requests through testing tools like Postman) and pass some wrong parameters. Come here, isn't your back-end code in danger?

Therefore, we generally perform one set of verifications on the front end and one set of verifications on the back end, so that security can be greatly improved.

Common annotations

Annotation name Annotation description
@NotNull is mainly used for object verification. The verification object cannot be null, and a string with a length of 0 cannot be checked.
@NotBlank is used for String type parameter verification, and checks that the string cannot be null and trim() The following size>0
@NotEmpty is mainly used for collection verification. The verification collection cannot be null and size>0. It can also be used for String verification.
@Size is used to check whether the object (Array, Collection, Map, String) has a length. Within the given range
@Range controls the range of a value
@Length is used for the size of the String object must be within the specified range
@Pattern is used for whether the String object conforms to the rules of regular expressions
@Email is used for whether the String object conforms to the email address Format
@Min is used for Number and String objects to see whether they are greater than or equal to the specified value.
@Max is used for Number and String objects to see whether it is less than or equal to the specified value.
@AssertTrue is used for Boolean objects to see whether it is true.
@AssertFalse is used for Boolean objects to see whether it is false.

package com.zlj.web;

import com.zlj.biz.StuBiz;
import com.zlj.model.Stu;
import com.zlj.util.PageBean;
import com.zlj.util.PropertiesUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zlj
 * @create 2023-09-08 16:55
 */
@Controller
@RequestMapping("stu")
public class StuController {
    @Autowired
    private StuBiz stuBiz;

    //    增
    @RequestMapping("/add")
    public String add(Stu stu,HttpServletRequest request) {
        int i = stuBiz.insert(stu);
        return "redirect:list";
    }
    //    给数据添加服务端校验
    @RequestMapping("/valiAdd")
    public String valiAdd(@Validated Stu stu, BindingResult result, HttpServletRequest req){
//        如果服务端验证不通过,有错误
        if(result.hasErrors()){
//            服务端验证了实体类的多个属性,多个属性都没有验证通过
            List<FieldError> fieldErrors = result.getFieldErrors();
            Map<String,Object> map = new HashMap<>();
            for (FieldError fieldError : fieldErrors) {
//                将多个属性的验证失败信息输送到控制台
                System.out.println(fieldError.getField() + ":" + fieldError.getDefaultMessage());
                 map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            req.setAttribute("errorMap",map);
        }else {
            this.stuBiz.insertSelective(stu);
            return "redirect:list";
        }
        return "stu/edit";
    }

    //            删
    @RequestMapping("/del/{sid}")
    public String del(@PathVariable("sid") Integer sid) {
        stuBiz.deleteByPrimaryKey(sid);
        return "redirect:/stu/list";
    }

//        改
@RequestMapping("/edit")
public String edit(Stu stu) {
    stuBiz.updateByPrimaryKeySelective(stu);
    return "redirect:list";
}
//文件上传
    @RequestMapping("/upload")
    public String upload(Stu stu,MultipartFile xxx){
        try {
//    3.后端可以直接利用mutipartFile类,接受前端传递到后台的文件
//    4.将文件转成流,然后写到服务器(某一个硬盘)
//    上传的图片真实的地址
        String dir= PropertiesUtil.getValue("dir");
//        网络访问的地址
        String server=PropertiesUtil.getValue("server");;
        //文件名
        String filename=xxx.getOriginalFilename();
        System.out.println("文件名"+filename);
        System.out.println("文件类别"+xxx.getContentType());
        FileUtils.copyInputStreamToFile(xxx.getInputStream(),new File(dir+filename));
//      /upload/0703.png
        stu.setSpic(server+filename);
        stuBiz.updateByPrimaryKeySelective(stu);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "redirect:list";
}


//                查
    @RequestMapping("/list")
    public String list(Stu stu, HttpServletRequest request) {
        //stu是用来接收前台传递后台的参数
        PageBean pageBean = new PageBean();
        pageBean.setRequest(request);
        List<Stu> stus = stuBiz.ListPager(stu, pageBean);
        request.setAttribute("lst", stus);
        request.setAttribute("pageBean", pageBean);
//      WEB-INF/jsp/stu/list.jsp
        return "stu/list";

    }
//下载文件
    @RequestMapping(value="/download")
    public ResponseEntity<byte[]> download(Stu stu,HttpServletRequest req){

        try {
            //先根据文件id查询对应图片信息
            Stu stus = this.stuBiz.selectByPrimaryKey(stu.getSid());
            String diskPath = PropertiesUtil.getValue("dir");
            String reqPath = PropertiesUtil.getValue("server");
//            /upload/0703.png  -->D:/temp/upload/0703.png
            String realPath = stus.getSpic().replace(reqPath,diskPath);
            String fileName = realPath.substring(realPath.lastIndexOf("/")+1);
            //下载关键代码
            File file=new File(realPath);
            HttpHeaders headers = new HttpHeaders();//http头信息
            String downloadFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");//设置编码
            headers.setContentDispositionFormData("attachment", downloadFileName);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            //MediaType:互联网媒介类型  contentType:具体请求中的媒体类型信息
            return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    //多文件上传
    @RequestMapping("/uploads")
    public String uploads(HttpServletRequest req, Stu stu, MultipartFile[] files){
        try {
            StringBuffer sb = new StringBuffer();
            for (MultipartFile file : files) {
                //思路:
                //1) 将上传图片保存到服务器中的指定位置
                String dir = PropertiesUtil.getValue("dir");
                String server = PropertiesUtil.getValue("server");
                String filename = file.getOriginalFilename();
                FileUtils.copyInputStreamToFile(file.getInputStream(),new File(dir+filename));
                sb.append(filename).append(",");
            }
            System.out.println(sb.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:list";
    }



    //数据回显
    @RequestMapping("/preSave")
    public String preSave(Stu stu, Model model) {
        if (stu != null && stu.getSid() != null && stu.getSid() != 0) {
            Stu s = stuBiz.selectByPrimaryKey(stu.getSid());
            model.addAttribute("s", s);
        }
        return "stu/edit";
    }
}
<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/common/header.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>博客的编辑界面</title>
</head>
<body>
<form action="${ctx }${empty s ? 'valiAdd' : 'edit'}" method="post">
    学生id:<input type="text" name="sid" value="${s.sid }"><span style="color: red;">${errorMap.sid}</span><br>
    学生姓名:<input type="text" name="same" value="${s.same }"><span style="color: red;">${errorMap.same}</span><br>
    学生年龄:<input type="text" name="sage" value="${s.sage }"><span style="color: red;">${errorMap.sage}</span><br>
    <input type="submit">
</form>
</body>
</html>

2.Interceptor

What is an interceptor

​SpringMVC's processor interceptor is similar to the filter in Servlet development and is used to pre-process and post-process the processor. It relies on the web framework and is based on Java's reflection mechanism in implementation, which is an application of aspect-oriented programming (AOP). Since the interceptor is based on the call of the web framework, Spring's dependency injection (DI) can be used to perform some business operations. At the same time, an interceptor instance can be called multiple times within a controller life cycle.

> What is a filter (Filter)
> Depends on the servlet container. Based on function callbacks in implementation, almost all requests can be filtered, but the disadvantage is that a filter instance can only be called once when the container is initialized. The purpose of using filters is to perform some filtering operations, such as: modifying the character encoding in the filter; modifying some parameters of HttpServletRequest in the filter, including: filtering vulgar text, dangerous characters, etc.

The difference between interceptors and filters

- filter

  1. Filter belongs to Servlet technology and can be used in any web project.

  2.filter is mainly due to filtering all requests

  3.The execution time of filter is earlier than Interceptor

- interceptor

  1.interceptor belongs to SpringMVC technology and must have a SpringMVC environment before it can be used.

  2.interceptor usually intercepts the processor Controller

  3.interceptor can only intercept requests processed by dispatcherServlet

Application scenarios

- Logging: Record the log of request information for information monitoring, information statistics, calculation of PV (Page View), etc.
- Permission check: such as login detection, enter the processor to detect whether you are logged in, and if not directly return to the login page;
- Performance monitoring: Sometimes the system is inexplicably slow for a certain period of time, you can use an interceptor to record the start time before entering the processor , record the end time after processing, so as to get the processing time of the request (if there is a reverse proxy, such as apache, it can be recorded automatically); -
General behavior: read the cookie to get the user information and put the user object into the request, so as to facilitate For subsequent process use, such as extracting Locale and Theme information, etc., as long as the processing methods in multiple Controllers are needed, we can use interceptors to implement them.

package com.zlj.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class  OneInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("【OneInterceptor】:preHandle...");

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("【OneInterceptor】:postHandle...");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("【OneInterceptor】:afterCompletion...");
    }
}
package com.zlj.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TwoInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("【TwoInterceptor】:preHandle...");

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("【TwoInterceptor】:postHandle...");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("【TwoInterceptor】:afterCompletion...");
    }
}

 The single interceptor and multiple interceptor configurations are as follows. The difference: a single interceptor will only intercept once and multiple interceptors will intercept multiple times.

//spring-mvc.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:aop="http://www.springframework.org/schema/aop"
       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 http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
   <!--1) 扫描com.zking.zf及子子孙孙包下的控制器(扫描范围过大,耗时)-->
    <context:component-scan base-package="com.zlj"/>

    <!--2) 此标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
    <mvc:annotation-driven />

    <!--3) 创建ViewResolver视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- viewClass需要在pom中引入两个包:standard.jar and jstl.jar -->
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--4) 单独处理图片、样式、js等资源 -->
<!--     <mvc:resources location="/css/" mapping="/css/**"/>-->
<!--     <mvc:resources location="/js/" mapping="/js/**"/>-->
<!--     <mvc:resources location="WEB-INF/images/" mapping="/images/**"/>  -->
    <mvc:resources location="/static/" mapping="/static/**"/>

    <!--    处理文件上传下载问题-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 必须和用户JSP 的pageEncoding属性一致,以便正确解析表单的内容 -->
        <property name="defaultEncoding" value="UTF-8"></property>
        <!-- 文件最大大小(字节) 1024*1024*50=50M-->
        <property name="maxUploadSize" value="52428800"></property>
        <!--resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常-->
        <property name="resolveLazily" value="true"/>
    </bean>

<!--  配置拦截器-->
    <mvc:interceptors>
        <bean class="com.zlj.interceptor.LoginInterceptor"></bean>
    </mvc:interceptors>

<!--单拦截器-->
<!--    <mvc:interceptors>-->
<!--        <bean class="com.zlj.interceptor.OneInterceptor"></bean>-->
<!--    </mvc:interceptors>-->

<!--    <mvc:interceptors>-->
<!--        &lt;!&ndash;2) 多拦截器(拦截器链)&ndash;&gt;-->
<!--        <mvc:interceptor>-->
<!--            <mvc:mapping path="/**"/>-->
<!--            <bean class="com.zlj.interceptor.OneInterceptor"/>-->
<!--        </mvc:interceptor>-->
<!--        <mvc:interceptor>-->
<!--            <mvc:mapping path="/stu/**"/>-->
<!--            <bean class="com.zlj.interceptor.TwoInterceptor"/>-->
<!--        </mvc:interceptor>-->
<!--    </mvc:interceptors>-->


    <!--    处理controller层发送请求到biz,会经过切面拦截处理-->
    <aop:aspectj-autoproxy/>
</beans>

Single interceptor console display

k

Multiple interceptor console display

package com.zlj.web;

import com.zlj.biz.StuBiz;
import com.zlj.model.Stu;
import com.zlj.util.PageBean;
import com.zlj.util.PropertiesUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zlj
 * @create 2023-09-08 16:55
 */
@Controller
@RequestMapping("stu")
public class StuController {
    @Autowired
    private StuBiz stuBiz;

    //    增
    @RequestMapping("/add")
    public String add(Stu stu,HttpServletRequest request) {
        int i = stuBiz.insert(stu);
        return "redirect:list";
    }
    //    给数据添加服务端校验
    @RequestMapping("/vliAdd")
    public String vliAdd(@Validated Stu stu, BindingResult result, HttpServletRequest req){
//        如果服务端验证不通过,有错误
        if(result.hasErrors()){
//            服务端验证了实体类的多个属性,多个属性都没有验证通过
            List<FieldError> fieldErrors = result.getFieldErrors();
            Map<String,Object> map = new HashMap<>();
            for (FieldError fieldError : fieldErrors) {
//                将多个属性的验证失败信息输送到控制台
                System.out.println(fieldError.getField() + ":" + fieldError.getDefaultMessage());
                 map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            req.setAttribute("errorMap",map);
        }else {
            this.stuBiz.insertSelective(stu);
            return "redirect:list";
        }
        return "stu/edit";
    }

    //            删
    @RequestMapping("/del/{sid}")
    public String del(@PathVariable("sid") Integer sid) {
        stuBiz.deleteByPrimaryKey(sid);
        return "redirect:/stu/list";
    }

//        改
@RequestMapping("/edit")
public String edit(Stu stu) {
    stuBiz.updateByPrimaryKeySelective(stu);
    return "redirect:list";
}
//文件上传
    @RequestMapping("/upload")
    public String upload(Stu stu,MultipartFile xxx){
        try {
//    3.后端可以直接利用mutipartFile类,接受前端传递到后台的文件
//    4.将文件转成流,然后写到服务器(某一个硬盘)
//    上传的图片真实的地址
        String dir= PropertiesUtil.getValue("dir");
//        网络访问的地址
        String server=PropertiesUtil.getValue("server");;
        //文件名
        String filename=xxx.getOriginalFilename();
        System.out.println("文件名"+filename);
        System.out.println("文件类别"+xxx.getContentType());
        FileUtils.copyInputStreamToFile(xxx.getInputStream(),new File(dir+filename));
//      /upload/0703.png
        stu.setSpic(server+filename);
        stuBiz.updateByPrimaryKeySelective(stu);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "redirect:list";
}


//                查
    @RequestMapping("/list")
    public String list(Stu stu, HttpServletRequest request) {
        //stu是用来接收前台传递后台的参数
        PageBean pageBean = new PageBean();
        pageBean.setRequest(request);
        List<Stu> stus = stuBiz.ListPager(stu, pageBean);
        request.setAttribute("lst", stus);
        request.setAttribute("pageBean", pageBean);
//      WEB-INF/jsp/stu/list.jsp
        return "stu/list";

    }
//下载文件
    @RequestMapping(value="/download")
    public ResponseEntity<byte[]> download(Stu stu,HttpServletRequest req){

        try {
            //先根据文件id查询对应图片信息
            Stu stus = this.stuBiz.selectByPrimaryKey(stu.getSid());
            String diskPath = PropertiesUtil.getValue("dir");
            String reqPath = PropertiesUtil.getValue("server");
//            /upload/0703.png  -->D:/temp/upload/0703.png
            String realPath = stus.getSpic().replace(reqPath,diskPath);
            String fileName = realPath.substring(realPath.lastIndexOf("/")+1);
            //下载关键代码
            File file=new File(realPath);
            HttpHeaders headers = new HttpHeaders();//http头信息
            String downloadFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");//设置编码
            headers.setContentDispositionFormData("attachment", downloadFileName);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            //MediaType:互联网媒介类型  contentType:具体请求中的媒体类型信息
            return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    //多文件上传
    @RequestMapping("/uploads")
    public String uploads(HttpServletRequest req, Stu stu, MultipartFile[] files){
        try {
            StringBuffer sb = new StringBuffer();
            for (MultipartFile file : files) {
                //思路:
                //1) 将上传图片保存到服务器中的指定位置
                String dir = PropertiesUtil.getValue("dir");
                String server = PropertiesUtil.getValue("server");
                String filename = file.getOriginalFilename();
                FileUtils.copyInputStreamToFile(file.getInputStream(),new File(dir+filename));
                sb.append(filename).append(",");
            }
            System.out.println(sb.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:list";
    }



    //数据回显
    @RequestMapping("/preSave")
    public String preSave(Stu stu, Model model) {
        if (stu != null && stu.getSid() != null && stu.getSid() != 0) {
            Stu s = stuBiz.selectByPrimaryKey(stu.getSid());
            model.addAttribute("s", s);
        }
        return "stu/edit";
    }
}
//edit.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/common/header.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>博客的编辑界面</title>
</head>
<body>
<form action="${ctx }${empty s ? 'vliAdd' : 'edit'}" method="post">
    学生id:<input type="text" name="sid" value="${s.sid }"><span style="color: red;">${errorMap.sid}</span><br>
    学生姓名:<input type="text" name="same" value="${s.same }"><span style="color: red;">${errorMap.same}</span><br>
    学生年龄:<input type="text" name="sage" value="${s.sage }"><span style="color: red;">${errorMap.sage}</span><br>
    <input type="submit">
</form>
</body>
</html>

 

package com.zlj.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("【implements】:preHandle...");
        StringBuffer url = request.getRequestURL();
        if (url.indexOf("/login") > 0 || url.indexOf("/logout") > 0){
            //        如果是 登录、退出 中的一种
            return true;
        }
//            代表不是登录,也不是退出
//            除了登录、退出,其他操作都需要判断是否 session 登录成功过
        String name = (String) request.getSession().getAttribute("name");
        if (name == null || "".equals(name)){
            response.sendRedirect("/page/login");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
package com.zlj.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author zlj
 * @create 2023-09-12 17:47
 */
@Controller
public class loginController {
    @RequestMapping("/login")
    public String login(HttpServletRequest req){
        String name = req.getParameter("name");
        HttpSession session = req.getSession();
        if ("zs".equals(name)){
            session.setAttribute("name",name);
        }
        return "redirect:/stu/list";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest req){
        req.getSession().invalidate();
        return "redirect:/stu/list";
    }
}
//login.jsp
<%--
  Created by IntelliJ IDEA.
  User: 朱
  Date: 2023/9/12
  Time: 17:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登录</h1>
<form action="/login" method="post">
    用户:<input name="name">
    <input type="submit">
</form>
</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_73471776/article/details/132831739