In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use mvc in SpringBoot, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
In the classical MVC mode, M refers to the business model, V refers to the user interface, and C is the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different forms of expression. Among them, the definition of View is relatively clear, which is the user interface.
About mvc in SpringBoot
The use of mvc in SpringBoot is basically the same as that of springmvc, and we can even implement the controller according to the standards in springmvc.
Package com.bdqn.lyrk.study.springboot.controller;import lombok.AllArgsConstructor;import lombok.Data;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody / * @ author chen.nie*/@Controller@RequestMapping ("/ index") public class IndexController {@ GetMapping ("/ index") public String index () {return "index";} @ GetMapping ("/ number/ {number} / Desc/ {desc}") @ ResponseBody public BeanEntity bean (@ PathVariable ("number") int number, @ PathVariable ("desc") String desc) {return new BeanEntity (number,desc) } @ Data@AllArgsConstructorclass BeanEntity {private int number; private String desc;}
When we visit the browser address, we get the corresponding result:
We can find that this is written in the same way as controller in springmvc, and the rest of the service and dao layers are also written in the usual way, marking service and dao with @ Service and @ Repository.
About mvc (static Resource-View) in SpringBoot
By default, Spring Boot provides static content from a directory named / static (or / public or / resources or / META-INF/resources) in the classpath or root directory of the ServletContext.
In static content, we can put files such as js,css style, in addition to Web services, we can also use Spring MVC to provide dynamic HTML content. Spring MVC supports a variety of template technologies, including Thymeleaf,FreeMarker and JSP. Of course, SpringBoot does not recommend using JSP as the view layer, usually we put the template under src/main/resources/templates.
The following directory is a typical template and static resource directory structure. According to the above rules, we put the static resource js file in the static directory and the template file (Freemarker is used here) in the specified directory:
SpringBoot adds support for jsp
In principle, SpringBoot does not recommend using Jsp as the view layer. If we want to use Jsp, we need to include the following dependencies:
Org.springframework.boot spring-boot-starter-tomcat provided org.apache.tomcat tomcat-jasper 8.5.28
Configure related views in application.properties:
Spring.mvc.view.suffix=/WEB-INF/jsp/spring.mvc.view.prefix=.jsp thank you for reading this article carefully. I hope the article "how to use mvc in SpringBoot" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you 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.