重定向与转发

在转发和重定向中不需要视图解释器,视图解析的作用,其实就是把控制层返回回来的字符串,拼接到前缀和后缀之间,组成一个jsp页面的地址。

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
    <!--前缀-->
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <!--后缀-->
    <property name="suffix" value=".jsp"/>
</bean>

如果你要使用视图解析器的方式来进行转发,Controller层的代码如下

//使用视图解析器转发
@RequestMapping("/d1/t4")
public String DemonTest4 (HttpServletRequest request,Model model) {
    String text = request.getParameter("text");
    model.addAttribute("msg",text);
    return "hello";
}

配置文件(web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    <!--注册DispatcherServlet 本质就是一个servlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!--加载springmvc-servlet.xml文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>

        <!--配置web.xml文件的启动级别-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

配置文件(springmvc-servlet.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: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.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--扫描特定包下的注解-->
    <context:component-scan base-package="com.zkw.restful"/>
    <!--过滤静态资源-->
    <mvc:default-servlet-handler/>
    <!--mvc注解驱动-->
    <mvc:annotation-driven/>

</beans>

在来一个Controller层

注意:不能重定向到WEB-INF/下边。

原因:(1)重定向是客户端的,而转发是服务端内部的。(2)重定向是让客户端去访问重定向的地址,而WEB-INF下的文件是不能通过外部访问的!

package com.zkw.restful;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class TestDemon {

    //重定向
    @RequestMapping("/d1/t1")
    public String DemonTest (HttpServletRequest request,Model model) {
        String text = request.getParameter("text");
        model.addAttribute("msg",text);
        return "redirect:/test.jsp";
    }

    //转发
    @PostMapping("/d1/t2")
    public String DemonTest2 ( HttpServletRequest request, Model model) {
        String text = request.getParameter("text");
        model.addAttribute("msg",text);
        return "forward:/WEB-INF/jsp/hello.jsp";
    }

    //不使用视图解析器转发
    @PostMapping("/d1/t3")
    public String DemonTest3 (HttpServletRequest request,Model model) {
        String text = request.getParameter("text");
        model.addAttribute("msg",text);
        return "/WEB-INF/jsp/hello.jsp";
    }
    
}

最后来看一下,jsp页面(test.jsp、hello.jsp、index.jsp)因为hello.jsp和test.jsp页面一样,只写一个了。

index.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>

        <form action="/d1/t1" method="get">
          <input type="text" name="text">
          <input type="submit">
      </form>

      <form action="/d1/t2" method="post">
          <input type="text" name="text">
          <input type="submit">
      </form>

        <form action="/d1/t3" method="post">
            <input type="text" name="text">
            <input type="submit">
        </form>
  </body>
</html>

test.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

index.jsp页面

在这里插入图片描述

结果为

重定向:

在这里插入图片描述

转发:

在这里插入图片描述

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值