当前位置: 移动技术网 > IT编程>开发语言>Java > 浅谈Spring 重定向指南

浅谈Spring 重定向指南

2019年07月19日  | 移动技术网IT编程  | 我要评论

1. 概述

本文将重点介绍在 spring 中实现重定向(redirect),并将讨论每个策略背后的原因。

2. 为什么要重定向?

让我们先来考虑在 spring 应用程序中为什么您可能需要做一个重定向的原因。

当然有很多可能的例子和原因。 一个简单的可能是 post 表单数据,围绕双重提交问题,或者只是将执行流委托给另一个控制器方法。

附注一点,典型的 post / redirect / get 模式并不能充分解决双重提交问题 - 在初始提交完成之前刷新页面的问题可能仍然会导致双重提交。

3、使用  redirectview 重定向

我们从这个简单的方法开始 - 直接来一个例子:

@controller
@requestmapping("/")
public class redirectcontroller {
   
  @getmapping("/redirectwithredirectview")
  public redirectview redirectwithusingredirectview(redirectattributes attributes) {
    attributes.addflashattribute("flashattribute", "redirectwithredirectview");
    attributes.addattribute("attribute", "redirectwithredirectview");
    return new redirectview("redirectedurl");
  }
}

在背后,redirectview 会触发 httpservletresponse.sendredirect() - 这将执行实际的重定向。

注意这里我们是如何注入重定向属性到方法里面的 - 由框架完成这部分繁重的工作,让我们能够与这些属性交互。

我们添加 attribute 到模型redirectattributes中 - 将其作为 http 查询参数(query parameter)暴露。 该模型包含的对象 - 通常是字符串或可以被转换成字符串的对象。

现在让我们来测试我们的重定向功能 - 用一个简单的 curl 命令来帮助实现:

curl -i http://localhost:8080/spring-rest/redirectwithredirectview

结果将是:

http/1.1 302 found
server: apache-coyote/1.1
location: 
 http://localhost:8080/spring-rest/redirectedurl?attribute=redirectwithredirectview

4. 使用redirect:前缀进行重定向

前面一个方法使用redirectview,因为一些原因它并不是最优的。

首先,我们现在是耦合于spring api的,因为我们在我们的代码里直接地使用redirectview。

其次,我们需要从一开始就知道,当实现控制器操作的时候,它的结果将总是重定向的,但情况并非总是如此。

更好的选择是使用redirect:前缀——重定向视图名称像其它逻辑视图名称一样被注入到控制器中。控制器甚至不知道重定向正在发生。

它看起来像是这样的:

@controller
@requestmapping("/")
public class redirectcontroller {
   
  @getmapping("/redirectwithredirectprefix")
  public modelandview redirectwithusingredirectprefix(modelmap model) {
    model.addattribute("attribute", "redirectwithredirectprefix");
    return new modelandview("redirect:/redirectedurl", model);
  }
}

当视图名称跟redirect:一起返回的时候,urlbasedviewresolver类(以及它的所有子类)会将其识别为一个需要进行重定向的特殊指示。视图名称剩下的部分会被当作重定向url。

这里有一个地方需要注意——当我们在这里使用redirect:/redirectedurl逻辑视图的时候,我们正在做一个跟当前servlet上下文相关的重定向。

如果需要重定向到一个绝对url,我们可以使用像这样的名称:redirect: http://localhost:8080/spring-redirect/redirectedurl。

所以现在,当我们执行curl命令:

curl -i http://localhost:8080/spring-rest/redirectwithredirectprefix

我们会立刻得到一个重定向:

http/1.1 302 found
server: apache-coyote/1.1
location: 
 http://localhost:8080/spring-rest/redirectedurl?attribute=redirectwithredirectprefix

5. 使用forward前缀转发:

我们现在看看如何做一些略有不同的事——一个转发。

在看代码之前,我们先来看一下对转发与重定向的语义的快速、高层概括:

  • 重定向将以包含302响应码和location头的新url进行响应;然后浏览器/客户端将再次向新的url发出请求
  • 转发完全在服务器端发生; servlet容器将相同的请求转发到目标url;浏览器中的url无须改变

现在我们来看看代码:

@controller
@requestmapping("/")
public class redirectcontroller {
   
  @getmapping("/forwardwithforwardprefix")
  public modelandview redirectwithusingforwardprefix(modelmap model) {
    model.addattribute("attribute", "forwardwithforwardprefix");
    return new modelandview("forward:/redirectedurl", model);
  }
}

与redirect:一样,forward:前缀将由urlbasedviewresolver及其子类解析。在内部,这将创建一个internalresourceview,它为新视图执行一个requestdispatcher.forward()操作。

当我们用curl执行该命令时:

curl -i http://localhost:8080/spring-rest/forwardwithforwardprefix

我们会得到http 405 (不允许的方法):

http/1.1 405 method not allowed
server: apache-coyote/1.1
allow: get
content-type: text/html;charset=utf-8

与我们在重定向解决方案中的两个请求相比,在这种情况下,我们只有一个请求从浏览器/客户端发送到服务器端。当然,以前由重定向添加的属性也不需要了。

6. 包含redirectattributes的属性

接下来 - 让我们看看在一个重定向中传递属性 - 充分利用框架中的redirectattribures:

@getmapping("/redirectwithredirectattributes")
public redirectview redirectwithredirectattributes(redirectattributes attributes) {
 
  attributes.addflashattribute("flashattribute", "redirectwithredirectattributes");
  attributes.addattribute("attribute", "redirectwithredirectattributes");
  return new redirectview("redirectedurl");
}

如前所述,我们可以直接在方法中插入属性对象 - 这使得该机制非常容易使用。

还要注意,我们也添加一个flash属性 - 这是一个不会被添加到url中的属性。我们可以通过这种属性来实现——我们稍后可以在重定向的最终目标的方法中使用@modelattribute(“flashattribute”)来访问flash属性:

@getmapping("/redirectedurl")
public modelandview redirection(
 modelmap model, 
 @modelattribute("flashattribute") object flashattribute) {
   
   model.addattribute("redirectionattribute", flashattribute);
   return new modelandview("redirection", model);
 }

因此,圆满完工——如果你需要使用curl测试该功能:

curl -i http://localhost:8080/spring-rest/redirectwithredirectattributes

我们将会被重定向到新的位置:

http/1.1 302 found
server: apache-coyote/1.1
set-cookie: jsessionid=4b70d8fada2fd6c22e73312c2b57e381; path=/spring-rest/; httponly
location: http://localhost:8080/spring-rest/redirectedurl;
 jsessionid=4b70d8fada2fd6c22e73312c2b57e381?attribute=redirectwithredirectattributes

这样,使用redirectattribures代替modelmap,赋予我们仅在重定向操作中涉及的两种方法之间共享一些属性的能力。

7. 没有前缀的另一种配置

现在让我们探索另一种配置——没有前缀的重定向。

为了实现这一点,我们需要使用org.springframework.web.servlet.view.xmlviewresolver:

<bean class="org.springframework.web.servlet.view.xmlviewresolver">
  <property name="location">
    <value>/web-inf/spring-views.xml</value>
  </property>
  <property name="order" value="0" />
</bean>

代替我们在之前配置里使用的org.springframework.web.servlet.view.internalresourceviewresolver:

<bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
</bean>

我们还需要在配置里面定义一个redirectview bean:

<bean id="redirectedurl" class="org.springframework.web.servlet.view.redirectview">
  <property name="url" value="redirectedurl" />
</bean>

现在我们可以通过id来引用这个新的bean来触发重定向:

@controller
@requestmapping("/")
public class redirectcontroller {
   
  @getmapping("/redirectwithxmlconfig")
  public modelandview redirectwithusingxmlconfig(modelmap model) {
    model.addattribute("attribute", "redirectwithxmlconfig");
    return new modelandview("redirectedurl", model);
  }
}

为了测试它,我们再次使用curl命令:

curl -i http://localhost:8080/spring-rest/redirectwithredirectview

结果会是:

http/1.1 302 found
server: apache-coyote/1.1
location: 
 http://localhost:8080/spring-rest/redirectedurl?attribute=redirectwithredirectview

8. 重定向http post请求 request

对于类似银行付款这样的用例,我们可能需要重定向http post请求。根据返回的http状态码,post请求可以重定向到http get或post上。

根据http 1.1协议,状态码301(永久移除)和302(已找到)允许请求方法从post更改为get。该规范还定义了不允许将请求方法从post更改为get的相关的307(临时重定向)和308(永久重定向)状态码。
现在,我们来看看将post请求重定向到另一个post请求的代码:

@postmapping("/redirectposttopost")
public modelandview redirectposttopost(httpservletrequest request) {
  request.setattribute(view.response_status_attribute, httpstatus.temporary_redirect);
  return new modelandview("redirect:/redirectedposttopost");
}
@postmapping("/redirectedposttopost")
public modelandview redirectedposttopost() {
  return new modelandview("redirection");
}

现在,让我们使用curl命令来测试下重定向的post:

curl -l --verbose -x post http://localhost:8080/spring-rest/redirectposttopost

我们正在被重定向到目标地址:

> post /redirectedposttopost http/1.1
> host: localhost:8080
> user-agent: curl/7.49.0
> accept: */*
> 
< http/1.1 200 
< content-type: application/json;charset=utf-8
< transfer-encoding: chunked
< date: tue, 08 aug 2017 07:33:00 gmt
 
{"id":1,"content":"redirect completed"}

9. 结论

本文介绍了在spring中实现重定向的三种不同方法,在执行这些重定向时如何处理/传递属性以及如何处理http post请求的重定向。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网