SSM-SpringMVC-22:SpringMVC中转发(forward)和重定向(redirect)

释放双眼,带上耳机,听听看~!

 

 

————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-

 

 

转发和重定向大家都熟悉,都学到框架了,怎么能不了解转发和重定向呢?

如果有不熟悉的,可以去百度搜几篇博客去看看,绝对比我在这儿再多扯点好,所以我这儿要讲的重点就是springmvc的转发和重定向的写法

 

首先了解一个概念:携带数据的要用转发而不是重定向,重定向是在客户端完成,转发是在服务器端完成,所以路径写法有所不同

 

我在这篇博客要写的是:转发到页面,转发到别的处理方法,重定向到页面,重定向到别的处理方法。

(我将处理方法扔一块了,不过我会解释清楚的)

 

案例开始:

一,处理方法:(直接return:"abc")的是转发到页面

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
1package cn.dawn.day14redirectandforward;
2
3import org.springframework.stereotype.Controller;
4import org.springframework.ui.Model;
5import org.springframework.web.bind.annotation.RequestMapping;
6
7import javax.servlet.http.HttpServletRequest;
8import javax.servlet.http.HttpServletResponse;
9
10/**
11 * Created by Dawn on 2018/3/28.
12 */
13@Controller
14public class RedirectAndForwardController {
15
16    /*转发到处理方法*/
17    @RequestMapping("addBookforward")
18    public String addBookforward(Model model){
19        model.addAttribute("msg","迟老大爱着原老大");
20        /*转发到处理方法*/
21        return "forward:/BookListredirect";
22    }
23    @RequestMapping("BookListforward")
24    public String BookListforward(Model model){
25        /*转发到页面页面*/
26        return "success";
27    }
28
29
30
31
32
33
34
35
36    /*重定向到处理方法*/
37    @RequestMapping("addBookredirect")
38    public String addBookredirect(Model model){
39        model.addAttribute("msg","迟老大爱着原老大");
40        /*重定向到处理方法*/
41        return "redirect:/BookListredirect";
42    }
43    @RequestMapping("BookListredirect")
44    public String BookListredirect(Model model){
45        /*转发到页面页面*/
46        return "success";
47    }
48
49
50
51
52
53
54    /*重定向页面*/
55    @RequestMapping("pageredirect")
56    public String pageredirect(Model model){
57        model.addAttribute("msg","迟老大爱着原老大");
58        /*重定向页面*/
59        return "redirect:/day14/success.jsp";
60    }
61
62
63
64
65
66    /*转发到页面*/
67    @RequestMapping("pageforward")
68    public String pageforward(Model model){
69        model.addAttribute("msg","迟老大爱着原老大");
70        /*转发到页面*/
71        return "success";
72    }
73}
74

 

二,自定义的xml配置文件:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4       xmlns:mvc="http://www.springframework.org/schema/mvc"
5       xmlns:context="http://www.springframework.org/schema/context"
6       xsi:schemaLocation="http://www.springframework.org/schema/beans
7        http://www.springframework.org/schema/beans/spring-beans.xsd
8        http://www.springframework.org/schema/mvc
9        http://www.springframework.org/schema/mvc/spring-mvc.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd">
12
13    <!--包扫描器-->
14    <context:component-scan base-package="cn.dawn.day14redirectandforward"></context:component-scan>
15    <!--视图解析器-->
16    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17        <property name="prefix" value="/day14/"></property>
18        <property name="suffix" value=".jsp"></property>
19    </bean>
20
21
22</beans>
23

 

三,修改web.xml的中央调度器的上下文配置位置

四,准备jsp页面:俩个,我的在day14的包下:

**1.login.jsp
**

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
2<html>
3<head>
4    <title>Title</title>
5</head>
6<body>
7<h2>登录</h2>
8<form action="${pageContext.request.contextPath}/pageforward" method="post">
9
10    <input type="submit" value="登录"/>
11</form>
12</body>
13</html>
14

 

2.success.jsp

 


1
2
3
4
5
6
7
8
9
1<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
2<html>
3<body>
4<%--<img src="image/1.jpg">--%>
5<h2>Success!</h2>
6<p>${msg}</p>
7</body>
8</html>
9

 

你这种方式需要改action中指向的处理方法名,才能调度到对应的处理方法

其实没必要这么麻烦,如果通过网页的url访问,只需要一个success.jsp页面即可,但我是给你提供思路的人,所以,我给你多一个写法

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++ lambda表达式

2022-1-11 12:36:11

病毒疫情

福建全省新冠肺炎住院患者清“0”!

2020-3-7 20:36:00

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索