springMVC跳转和传值

接上一篇:SpringMVC接收请求参数

1. 跳转

1.1 pom.xml中导入依赖:jstl,jsp-api,servlet-api


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>springMVC03artifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>
    <dependencies>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.3.16version>
        dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstlgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.0version>
        dependency>
    dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>*.xmlinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
        resources>
    build>

project>

1.2 视图解析器ViewResolver

springMVC跳转和传值_第1张图片


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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="com.lyx"/>

    <mvc:annotation-driven>mvc:annotation-driven>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    bean>
beans>

1.3 代码

后端控制器,跳转:forword,重定向:redirect

package com.lyx.web;

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

@Controller
@RequestMapping("/jump")
public class ForwardController {

    @RequestMapping("/test1")
    public String test1(){
        System.out.println("test1");
        return "forward:/hello.jsp";
    }
    @RequestMapping("/test2")
    public String test2(){
        System.out.println("test2");
        //return "forward:/hello/test1";//绝对路径。跨类跳转到hello的test1方法
        return "forward:test1";//相对路径,当前本类的test1
    }
    @RequestMapping("/test3")
    public String test3(){
        System.out.println("test3");
        return "redirect:/hello.jsp";//重定向到hello.jsp
    }
    @RequestMapping("/test4")
    public String test4(){
        System.out.println("test4");
        //return "redirect:/hello/test1";//绝对路径。跨类跳转到hello的test1方法
        return "redirect:test3";//相对路径,重定向到当前本类的test1
    }
}

1.4 细节

  1. 在增删改之后,为了防止请求重复提交,要用重定向redirect跳转,如果用forward,每次刷新页面都会重新提交一次数据
  2. 在查询之后,我们希望的就是每次跳转都重新刷新数据,所以用forward

2. 传值

2.1 request作用域与session作用域

2.1.1 request作用域

  • 后端控制器
@Controller
@RequestMapping("/data")
public class DataController {

    @RequestMapping("/test1")
    public String test1(HttpServletRequest request){
        System.out.println("test1");
        request.setAttribute("name","张三");
        request.setAttribute("age",22);
        return "data";
    }
}

  • data.jsp页面,JSP取值
<body>
name:${name}<br/>
age:${age}
body>
  • Tomcat运行,访问http://locahost:8080/data/test1
    springMVC跳转和传值_第2张图片

2.1.2 session作用域

@Controller
@RequestMapping("/data")
public class DataController {

    @RequestMapping("/test1")
    public String test1(HttpServletRequest request, HttpSession session){
        session.setAttribute("birth",new Date());
        System.out.println("test1");
        request.setAttribute("name","张三");
        request.setAttribute("age",22);
        return "index";
    }
}
  • JSP取值
<body>
    birth:<fmt:formatDate value="${sessionScope.birth}" pattern="yyyy-MM-dd"/><br/>
    name :  ${requestScope.name}<br/>
    age  :   ${requestScope.age}<br/>
body>
  • Tomcat运行,访问路径http://localhost:8080/data/test1
    springMVC跳转和传值_第3张图片

2.2 Model


2.2.1 model–>request作用域

当我们把数据存在model中的时候呢,model会把这个数据复制到request作用域

  • 后端控制器
@Controller
@RequestMapping("/data")
public class DataController {

    @RequestMapping("/test2")
    public String test2(Model model){
        model.addAttribute("gender",true);
        return "data";
    }
}
  • 当我们把数据存在model中的时候呢,model把这个数据复制到request作用域,所以所有model中的数据都可以通过以下方式调用
  • data.jsp页面
<body>
    gender:${requestScope.gender}
body>

springMVC跳转和传值_第4张图片

  • Tomcat运行,访问http://locahost:8080/data/test2
    springMVC跳转和传值_第5张图片

2.2.2 @SessionAttributes


通过注解设置key值,model向session作用域中存数据

  • 后端控制器
@Controller
@RequestMapping("/data")
@SessionAttributes(names = {"city","hobby"})
public class DataController {
    @RequestMapping("/sss")
    public String test(Model model){
        model.addAttribute("city","北京");
        model.addAttribute("hobby","football");
        return "index";
    }
}
  • JSP取值
<body>
   city:${sessionScope.city}<br/>
    hobby:${sessionScope.hobby}
body>
  • Tomcat运行,访问路径:http://localhost:8080/data/sss

springMVC跳转和传值_第6张图片

2.2.3 清除所有通过model存入session的数据

@Controller
@RequestMapping("/data")
public class DataController {
    
    @RequestMapping("ds")
    public String clean(SessionStatus sessionStatus){
        sessionStatus.setComplete();
        return "index";
    }
}

2.2.4 ModelAndView集中管理跳转和传值


ModelAndView是一个用来做数据整合的类
,Model就是数据,View就是视图(.jsp文件)

  • 后端控制器
@Controller
@RequestMapping("/data")
@SessionAttributes(names = {"city","hobby"})
public class DataController {

    @RequestMapping("/aaa")
    public ModelAndView test(){
    	//新建ModelAndView对象
        ModelAndView modelAndView = new ModelAndView();
        //设置视图名,即跳转的路径
        modelAndView.setViewName("forward:/index.jsp");
        //增加数据
        modelAndView.addObject("Singer","刘雨昕");
        return modelAndView;
    }

  • JSP取值,用EL表达式即可
<body>
   Singer:${requestScope.Singer}
body>
  • Tomcat运行,访问http:/localhost:8080/data/aaa

springMVC跳转和传值_第7张图片

你可能感兴趣的