当前位置: 首页 > wzjs >正文

扫二维码做自己网站快捷的网站建设排行榜

扫二维码做自己网站,快捷的网站建设排行榜,wordpress后台点击菜单没反应应,网站修改用什么工具当处理请求时,有时请求到不同的资源,那重定向和转发是必不可少的操作。本文将深入探讨重定向和转发的使用方法、区别、适用场景。 本文目录 一、转发1. 转发实现2. 转发时如何携带参数3. 转发的特点 二、重定向1. 实现重定向2. 重定向时如何携带参数2.1 …

        当处理请求时,有时请求到不同的资源,那重定向和转发是必不可少的操作。本文将深入探讨重定向和转发的使用方法、区别、适用场景。

本文目录

    • 一、转发
      • 1. 转发实现
      • 2. 转发时如何携带参数
      • 3. 转发的特点
    • 二、重定向
      • 1. 实现重定向
      • 2. 重定向时如何携带参数
        • 2.1 在URL中拼接参数
        • 2.2 使用 RedirectAttributes
      • 3. 重定向的特点
    • 三、重定向和转发的区别

一、转发

        转发是服务器内部的一种操作。当服务器接收到客户端的请求后,会把这个请求直接转发到另一个资源,如JSP、Servlet等进行处理。在这个过程中,客户端对请求被转发这件事毫不知情,其地址栏中的URL也不会发生任何变化。


1. 转发实现

        通过返回一个带有 forward: 前缀的字符串来实现转发。

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@Controller
public class ForwardController {@RequestMapping(value = "/forward", method = RequestMethod.GET)public String forward() {// 转发到另一个请求处理方法return "forward:/target";}@RequestMapping(value = "/target", method = RequestMethod.GET)public String targetPage() {return "target";}
}

2. 转发时如何携带参数

        由于转发是服务器内部操作,请求域中的数据会被保留,所以可以直接在请求域中设置参数。代码如下:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;@Controller
public class ForwardWithParamsController {@RequestMapping(value = "/forward", method = RequestMethod.GET)public String forward(HttpServletRequest request) {// 设置请求域中的参数request.setAttribute("message", "123");return "forward:/target";}@RequestMapping(value = "/target", method = RequestMethod.GET)public String targetWithParams(HttpServletRequest request) {String message = (String) request.getAttribute("message");System.out.println("接收到的参数: " + message);return "target";}
}

3. 转发的特点

  • 服务器内部操作
  • 共享请求域
  • 地址栏不变



二、重定向

        重定向是服务器向客户端发送一个状态码,通常是302,以及一个新的URL,客户端接收到这些信息后会自动向新的URL发起请求。因此,重定向会使客户端的地址栏发生改变。


1. 实现重定向

        可以通过返回一个带有 redirect: 前缀的字符串来实现重定向。代码如下:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@Controller
public class RedirectController {@RequestMapping(value = "/redirect", method = RequestMethod.GET)public String redirect() {// 重定向到另一个请求处理方法return "redirect:/target";}@RequestMapping(value = "/target", method = RequestMethod.GET)public String target() {return "target";}
}

2. 重定向时如何携带参数

        由于重定向是两次请求,请求域中的数据不会被保留,所以需要采用其他方式携带参数。

2.1 在URL中拼接参数
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@Controller
public class RedirectWithParamsController {@RequestMapping(value = "/redirect", method = RequestMethod.GET)public String redirect() {String message = "123";return "redirect:/target?message=" + message;}@RequestMapping(value = "/target", method = RequestMethod.GET)public String target(String message) {System.out.println("接收到的参数: " + message);return "target";}
}
2.2 使用 RedirectAttributes
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;@Controller
public class RedirectWithRedirectAttributesController {@RequestMapping(value = "/redirect", method = RequestMethod.GET)public String redirectWithRedirectAttributes(RedirectAttributes redirectAttributes) {redirectAttributes.addFlashAttribute("message", "123");return "redirect:/target";}@RequestMapping(value = "/target", method = RequestMethod.GET)public String target() {return "target";}
}

使用 RedirectAttributesaddFlashAttribute 方法添加参数,这些参数会被存储在会话中,在重定向后的第一次请求中可以获取到,之后会自动从会话中清除。


3. 重定向的特点

  • 客户端重新请求
  • 不共享请求域
  • 地址栏改变



三、重定向和转发的区别

比较项转发重定向
操作主体服务器内部服务器和客户端之间
请求次数一次两次
请求域数据共享不共享
地址栏不变改变
适用场景同一应用内的资源跳转,需要共享请求数据不同应用之间的跳转,或者需要刷新页面



← 上一篇 Java进阶——常用类及常用方法详解
记得点赞、关注、收藏哦!
下一篇 Java进阶——数组超详细整理 →

文章转载自:

http://A4HiAOrQ.txkrc.cn
http://ikVDDy4E.txkrc.cn
http://PhyvT77C.txkrc.cn
http://pYeVlygM.txkrc.cn
http://mHolvzFl.txkrc.cn
http://Mdp19Skb.txkrc.cn
http://3m7tUTiz.txkrc.cn
http://yRq6qICU.txkrc.cn
http://qOPhWey2.txkrc.cn
http://jHcOXpCv.txkrc.cn
http://xJmmHfST.txkrc.cn
http://n6k6V0KA.txkrc.cn
http://DESaq8gE.txkrc.cn
http://29dxy0I7.txkrc.cn
http://ofxfnThp.txkrc.cn
http://RS1ROWrN.txkrc.cn
http://GA5IYqBj.txkrc.cn
http://PYWQqvhR.txkrc.cn
http://90FEpNnk.txkrc.cn
http://cFyKgQkV.txkrc.cn
http://szS21yrC.txkrc.cn
http://Elzvp5Bt.txkrc.cn
http://k1rJGmHo.txkrc.cn
http://V6tf46Sw.txkrc.cn
http://RcxEaIRc.txkrc.cn
http://TRq5F8mn.txkrc.cn
http://lddxYV3M.txkrc.cn
http://Fhx58O0S.txkrc.cn
http://tnSBxpq8.txkrc.cn
http://nxcdNgvX.txkrc.cn
http://www.dtcms.com/wzjs/671626.html

相关文章:

  • 制作网站要花多少钱wordpress块引用美化
  • 共享ip做网站iis .htaccess wordpress
  • 淄博网站建设网站推广优化维护网站的职位叫什么
  • 动态链接做网站外链图网站模版切换
  • 上海市建设执业资格注册中心网站网络小说排行榜
  • 收费报名网站怎么做黄浦网站推广公司
  • 陕西有没有做网站普查公司安远网络推广公司
  • 营销型网站架构师凡科网手机版下载
  • 网站免费高清素材软件小游戏好看的移动端网页
  • 上海英文网站制作广州网站开发创意设计
  • 江油市建设局网站网站服务商
  • wordpress 快速编辑网站优化建设苏州
  • 1800做网站因为专业wordpress 新网站 代码
  • 怎么用apache做网站毕业生网站建设方案书
  • 代码网站开发建设局网站模板
  • 网站建设制作哪家好济南又出了一例
  • 做海报去哪个网站找素材比较好呢百度搜索指数
  • 玩具网站规划说明书购物网站北京云无限优化
  • 做平台的网站有哪些功能吗六安网站设计公司
  • wordpress地图怎么实现海淀区seo多少钱
  • 怎样做网站推wordpress 手动采集
  • 网站设计 联系商城开源免费商用
  • 帝国网站地图模板wordpress 字体设置
  • 社交网站金山做网站的公司
  • 个人做地方门户网站关于建设门户网站的请示
  • django做待办事项网站会员管理系统手机免费版
  • 中山网站设计素材哪些网站可以做淘宝店招
  • 网站建设公司擅自关闭客户网络网站模板购买
  • 网站功能需求表wordpress企业
  • 做漂亮的网站上海建设公司网站