当前位置:网站首页>Doraemon teach you forwarded and redirect page
Doraemon teach you forwarded and redirect page
2022-07-31 08:32:00 【prosperous】
Hi, my name is Gao Guo~
spring MVC view
1. Simple example
In the controller methods of Spring MVC, the common return types are ModelAndView, String, void.The ModelAndView type can add Model data and specify the view; the return value of the String type can jump to try, but cannot carry data; the void type is mainly used in asynchronous requests, only returning data, not jumping to the view.
Because the ModelAndView type fails to realize the decoupling between the data and the view, usually the return type is String, so when using String as the return value type, how to bring the data into the view page?
This requires the Model parameter type to achieve, through this parameter type, you can add the information that needs to be displayed in the view.
The controller method is as follows:
@RequestMapping("/modelTest")public String modelTest(Model model){model.addAttribute("msg","This is a Spring MVC program!");return "success";}
In addition to returning the view page of the above code, the String type can also perform redirection and request forwarding
2. forward request forwarding view
When sending a request, forward: can be used to realize the forwarding function
forward: can also jump from one request to another request
The controller method is as follows:
@RequestMapping("/forwardTest")public String forwardTest(){return "forward:/modelTest";}
Note: When the view name set in the controller method is prefixed with "forward:", the InternalResourceView view is created. The view name at this time will not be parsed by the view resolver configured in the SpringMVC configuration file, butThe prefix "forward:" will be removed, and the remaining part will be used as the final path to jump through forwarding.
3. redirect redirect view
redirect : The redirected path
The controller method is as follows:
@RequestMapping("/redirectTest")public String redirectTest(){return "redirect:/modelTest";}
Note: When the view name set in the controller method is prefixed with "redirect:", a RedirectView view is created. The view name at this time will not be parsed by the view resolver configured in the SpringMVC configuration file, butThe prefix "redirect:" will be removed, and the remaining part will be redirected as the final path.
4. The difference between redirection and forwarding
4.1 Retweet
The page is jumped by the server, and the client does not need to re-send the request:
Features are as follows:
1. The request in the address bar will not change, and the address of the first request is displayed
2. The number of requests, there is only one request
3. The data in the request domain will not be lost
4. The root directory: localhost:8080/project address/, which contains the access address of the project
4.2 redirects
For page jumping on the browser side, two requests need to be sent (the first time is artificial, the second time is automatic)
The features are as follows:
1. The address in the address bar changes, display the address of the latest request
2. The number of requests: 2 times
3. The data in the request domain will be lost because it is a different request
4. The root directory: localhost:8080/ does not contain itemsName of
4.3 Comparison
Difference
Forward
Redirect
root directory
Include project access address
No project access address
Address bar
No change
Changes will occur
Jump location
Server side
Browser side
Data in the request field
Not lost
will be lost
Let me introduce myself first. The editor graduated from Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Ali in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢
Small application project development, jingdong mall 】 【 uni - app custom search component (below) - search history
NK - RTU980 burning bare-metal program
奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些核心技能是你必须要掌握的!完整学习路线!
Flutter Paystack 所有选项实现
TypeError The view function did not return a valid response. The function either returned None 的解决
C语言三子棋(井字棋)小游戏
Regarding "computing power", this article is worth reading
《c语言小游戏》入门级三子棋游戏(机器人加强版)
google搜索技巧——程序员推荐
WLAN部署(AC+AP)配置及常见问题记录
随机推荐
如何在一台机器上(windows)安装两个MYSQL数据库
[Interview: Concurrency 37: Multithreading: Thread Pool] Custom Thread Pool
google搜索技巧——程序员推荐
Client navicat installation tutorial
关于“算力”,这篇文章值得一看
linux redis6.2.6配置文件
Small application project development, jingdong mall 】 【 uni - app custom search component (below) - search history
SQL语句知识大全
会话技术之Coookie && Session详解
动态顺序表的增删查改(C语言实现)
【idea 报错】 无效的目标发行版:17 的解决参考
The Spark run on Yarn Spark application
哪些字符串会被FastJson解析为null呢
模块化规范
使用PageHelper实现分页查询(详细)
报销流程|By天放师兄
MySql数据库优化查询工具
UML图及在drawio中的绘制
Shell编程之条件语句
First acquaintance with NK-RTU980 development board