In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to return to the view with springboot". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to return to View with springboot"!
Springboot returns the view instead of stringpackage com.example.demo.controller;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@EnableAutoConfigurationpublic class HelloController {@ RequestMapping ("/ hello") public String hello () {System.out.println ("enter controller"); return "hello";}}
Note that @ Controller is released instead of @ RestContreller
@ RestController returns json (JSON is the string representation of the JS object, which uses text to represent the information of a JS object, which is essentially a string. ) if you use @ RestController, do not use @ Responsebody
Another way is to use ModelAndViewimport org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;@Controller@EnableAutoConfigurationpublic class HelloController {@ RequestMapping ("/ hello") @ ResponseBody public ModelAndView hello () {System.out.println ("hello!"); ModelAndView mode = new ModelAndView () Mode.setViewName ("hello"); return mode;}}
Generally used to carry parameters and return to the view, if you want to take parameters, plus the mode.addObject () function
In addition, it should be noted that all tags in the html file must have a Terminator. Idea sometimes generates meta tags without a Terminator, so add
Finally, just enter http://localhost:8080/hello.
The way springboot returns the view the way Spring boot returns the view
1. Use ModelAndView
In controller
@ RequestMapping ("toTest") public ModelAndView toTest () {ModelAndView mv = new ModelAndView (); / / View name mv.setViewName ("login"); / / data mv.addObject ("o1", "data 1"); return mv;}
two。 Configure using webMVC
Create a configuration class
Package com.ch.exercise.config.webMvc;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer / * MVC configuration * @ author CH * @ date 2021-08-19 11:45 * / @ Configurationpublic class WebMvcConfig implements WebMvcConfigurer {@ Override public void addViewControllers (ViewControllerRegistry registry) {registry / / received request .addViewController ("/ toLogin") / / page name of the jump .setViewName ("login");}} to add
Get started quickly
1. Add dependencies in pom.xml
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf
two。 Create a page login.html
3. Configure thymeleaf
Add to application.yml
Spring: thymeleaf: # Page location prefix: classpath:/templates/ # whether to cache here is cache: false suffix: .html mode: LEGACYHTML5 template-resolver-order: 0
Then you can access it by configuring the view.
At this point, I believe you have a deeper understanding of "how to return to the view of springboot". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.