Spring MVC的@RequestAttribute注解和@SessionAttribute注解的使用

一 视图

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;  charset=UTF-8">
<title>@RequestAttribute和@SessionAttribute测试</title>
</head>
<body>
<h2>@RequestAttribute和@SessionAttribute测试</h2>
<a href="attrbuteTest">测试@RequestAttribute和@SessionAttribute</a>

</body>
</html>

二 控制器

package org.fkit.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AttributeController{

    // 测试@CookieValue注解
    // 该方法映射的请求为 /cookieValueTest
    @GetMapping(value="/attrbuteTest")
     public ModelAndView attrbuteTest(ModelAndView mv) {
        System.out.println("attributeTest方法被调用...");
         // 客户端重定向到main请求,会被自定义过滤器拦截
         mv.setViewName("redirect:main");;
         return mv;
     }
    
    /*
     *  该方法映射的请求为/main
     *  @RequestAttribute("school")会获取request作用范围中名为"school"的属性的值赋给school变量
     *  @SessionAttribute("author")会获取session作用范围中名为"author"的属性的值赋给author变量
     */
     @RequestMapping("/main")
     public String main(
             @RequestAttribute("school") String school,
             @SessionAttribute("author") String author) {
         System.out.println("main方法被调用...");
         // 输出@RequestAttribute获得的school
         System.out.println("school: " + school);
        // 输出@SessionAttribute获得的author
         System.out.println("author: " + author);
         
         return "welcome";
     }

}

三 拦截器

package org.fkit.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

// 过滤器拦截/main请求
@WebFilter(value="/main")
public class TestAttributeFilter implements Filter{
            
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain)
            throws IOException, ServletException {
        
        System.out.println("进入AuthFilter过滤器的doFilter方法");
        // 将ServletRequest对象强转成HttpServletRequest对象
        HttpServletRequest request = (HttpServletRequest)arg0;
        // 在request作用范围域中设置一个school属性
        request.setAttribute("school", "疯狂软件");
        // 在session作用范围域中设置一个author属性
        request.getSession().setAttribute("author", "肖文吉");
        // 如果还有过滤器执行过滤器,否则进入请求处理方法
        chain.doFilter(request, arg1);
        
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        
    }

}

四 配置文件

<?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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd     
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
        
    <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
      如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean  -->
    <context:component-scan base-package="org.fkit.controller"/>
    <!-- 默认装配方案 -->
    <mvc:annotation-driven/>
     <!-- 静态资源处理 -->
    <mvc:default-servlet-handler/>
    
    <!-- 视图解析器  p:prefix属性表示前缀  p:suffix 表示后缀  -->
     <bean id="viewResolver"
           class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/content/" p:suffix=".jsp"/>
    
</beans>

五 测试

Spring MVC的@RequestAttribute注解和@SessionAttribute注解的使用

Spring MVC的@RequestAttribute注解和@SessionAttribute注解的使用

上一篇:IDEA类头注释


下一篇:Vue2.0过滤器