springMVC跳转和传值

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

1. 跳转

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

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springMVC03</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.16</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>*.xml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

</project>

1.2 视图解析器ViewResolver

在这里插入图片描述

<?xml version="1.0" encoding="UTF8" ?>
<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>
<!--    视图解析器
        作用:1. 捕获后端控制器的返回值
        解析:2. 在返回值前后拼接/index.jsp-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

1.3 跳转:forword,重定向:redirect

后端控制器,
指定跳转方式 :return “forward:/view/success.jsp”
forward: 和 redirect: 需要注意,此种方式,不会被视图解析器加上前缀(/)、后缀(.jsp)。

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
    在这里插入图片描述

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
    在这里插入图片描述

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>

在这里插入图片描述

  • Tomcat运行,访问http://locahost:8080/data/test2
    在这里插入图片描述

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

在这里插入图片描述
补充:
在这里插入图片描述
在这里插入图片描述

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

在这里插入图片描述

2.2.5 @ModelAttribute

form表单发起请求

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/test/testModelAttribute" method="post">
    <input type="text" name="cardNo">
    <input type="submit" value="提交">
</form>
</body>
</html>

在任何一次请求前,都会先执行@ModelAttribute修饰的方法

如下代码
会先执行queryAccount()方法,
把account放入request作用域中
在这里插入图片描述

package com.lyx.controller;

import com.lyx.entity.Account;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("/test")
public class myController {
    @ModelAttribute
    public void queryAccount(Map<String,Object> map){
        Account account = new Account(100,"XiaoMing","123456",2220.5);
        map.put("acc",account);
    }

    @RequestMapping("/testModelAttribute")
    public String testModelAttribute(@ModelAttribute("acc") Account account) {
        account.setName("XiaXia");
        System.out.println(account);
        return "success";
    }

}

方法参数中可以不写注解,但必须保证request作用域中存在与参数类型同名但首字母小写的key值

在这里插入图片描述
去掉注解后如下图:

在这里插入图片描述

总结

就是,当form表单发起请求后,
在这里插入图片描述

现在from表单又提交过来了一个name=“cardNo”,由用户输入的值,

在这里插入图片描述

这个用户输入的值就会覆盖掉account中原本保存的值,原cardNO=100,输出改为666

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

素心如月桠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值