Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spring MVC and Thymeleaf to develop web applications

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use Spring MVC and Thymeleaf to develop web applications. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Spring MVC is a native framework built on Servlet API and is included in the Spring framework from the beginning. This paper mainly describes the architecture and analysis of Spring MVC, and uses Spring Boot + Spring MVC + MyBatis (SSM) + Thymeleaf (template engine) framework to simply and quickly build a Web project.

Web MVC Architecture and Analysis

The three-tier architecture of MVC is shown in the figure, and the red font represents the core module. The layers of MVC are as follows:

* * Model (model layer) * * deals with core business (data) logic, and model objects are responsible for accessing data in the database. The "data" here is not limited to the data itself, but also includes the logic of processing the data.

* * View (view layer) * * is used to display data, which is usually created based on model data.

* * Controller (controller layer) * * is used to process user input requests and response outputs, read data from attempts, control user input, and send data to the model. Controller is the intermediate coordinator for the bidirectional transfer of data between Model and View.

Spring MVC Architecture and Analysis

The flow of Spring MVC processing an HTTP request, as shown in the figure:

The whole process is introduced in detail: 1. The user sends a request to the front-end controller DispatcherServlet. 2.DispatcherServlet receives a request to call the processor mapper HandlerMapping. 3. According to the request URL, the processor mapper finds the specific Controller processor and returns it to DispatcherServlet. 4.DispatcherServlet invokes Controller to process the request through the processor adapter HandlerAdapter. 5. The method of executing the Controller processor. 6.Controller execution completes and returns ModelAndView. 7.HandlerAdapter returns the Controller execution result ModelAndView to DispatcherServlet. 8.DispatcherServlet passes the ViewName of ModelAndView to the view parser ViewReslover. After parsing, 9.ViewReslover returns to the specific view View. 10.DispatcherServlet passes the Model data to View and renders the View (that is, populating the model data into the view). 11-12.DispatcherServlet responds to the user.

Spring Boot + Spring MVC + MyBatis + Thymeleaf

In this paragraph, we mainly implement a paging query by building the project.

1. Project construction

The project structure is shown in the figure:

1.1 pom introduces dependency 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.9.RELEASE cn.zwqh spring-boot-ssm-thymeleaf 0.0.1-SNAPSHOT spring-boot-ssm-thymeleaf spring-boot-ssm -thymeleaf 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test Test org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-devtools True mysql mysql-connector-java runtime org.mybatis.spring.boot Mybatis-spring-boot-starter 2.1.0 com.github.pagehelper pagehelper-spring-boot-starter 1.2.12 Org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring- Boot-maven-plugin 1.2 WebMvcConfig configuration package cn.zwqh.springboot.config Import org.springframework.context.annotation.Configuration;import org.springframework.core.Ordered;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport @ Configurationpublic class WebMvcConfig extends WebMvcConfigurationSupport {/ * static resource configuration * / @ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ statics/**") .addResourceLocations ("classpath:/statics/"); / / static resource path registry.addResourceHandler such as css,js,img ("/ templates/**") .addResourceLocations ("classpath:/templates/") / / View registry.addResourceHandler ("/ mapper/**") .addResourceLocations ("classpath:/mapper/"); / / mapper.xml super.addResourceHandlers (registry) } / * View Controller configuration * / @ Override public void addViewControllers (ViewControllerRegistry registry) {registry.addViewController ("/") .setViewName ("/ index"); / / set default jump view to / index registry.setOrder (Ordered.HIGHEST_PRECEDENCE); super.addViewControllers (registry) } / * View parser configuration controls page view jump control returned by controller String * / @ Override public void configureViewResolvers (ViewResolverRegistry registry) {/ / registry.viewResolver (new InternalResourceViewResolver ("/ jsp/", ".jsp")); super.configureViewResolvers (registry) }} 1.3 application.properties configuration # thymeleafspring.thymeleaf.cache=false#datasourcespring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=UTF-8&useSSL=truespring.datasource.username=rootspring.datasource.password=root#mybatismybatis.mapper-locations=classpath:/mapper/*.xml#logginglogging.path=/user/local/loglogging.level.cn.zwqh=debuglogging.level.org.springframework.web=infologging.level. Org.mybatis=error1.4 Controller@Controller@RequestMapping ("/ user") public class UserController {@ Autowired private UserService userService @ GetMapping ("/ list") public ModelAndView showUserList (int pageNum, int pageSize) {PageInfo pageInfo = userService.getUserList (pageNum, pageSize); ModelAndView modelAndView=new ModelAndView (); modelAndView.setViewName ("index"); modelAndView.addObject ("pageInfo", pageInfo); return modelAndView;}} 1.5 Service and ServiceImpl

UserService

Public interface UserService {PageInfo getUserList (int pageNum, int pageSize);}

UserServiceImpl

Servicepublic class UserServiceImpl implements UserService {@ Autowired private UserDao userDao; @ Override public PageInfo getUserList (int pageNum, int pageSize) {PageHelper.startPage (pageNum, pageSize); List list=userDao.getAll (); PageInfo pageData= new PageInfo (list); System.out.println ("current Page:" + pageData.getPageNum ()) System.out.println ("Page size:" + pageData.getPageSize ()); System.out.println ("Total:" + pageData.getTotal ()); System.out.println ("Total pages:" + pageData.getPages ()); return pageData } 1.6 Daopublic interface UserDao {/ * get all users * @ return * / List getAll ();}

Remember to add * * @ MapperScan** to the startup class

@ SpringBootApplication@MapperScan ("cn.zwqh.springboot.dao") public class SpringBootSsmThymeleafApplication {public static void main (String [] args) {SpringApplication.run (SpringBootSsmThymeleafApplication.class, args) }} 1.7 Mapper.xml select * from t_user 1.8 entity UserEntitypublic class UserEntity {private Long id; private String userName; private String userSex; public Long getId () {return id } public void setId (Long id) {this.id = id;} public String getUserName () {return userName;} public void setUserName (String userName) {this.userName = userName;} public String getUserSex () {return userSex } public void setUserSex (String userSex) {this.userSex = userSex;}} 1.9 html Page Insert title here

Thymeleaf is a modern server-side Java template engine for Web and stand-alone environments. Thymeleaf is recommended for SpringBoot.

The following is an example of a table:

ID name, gender

Total number of previous pages and next pages:

The above is the editor for you to share how to use Spring MVC and Thymeleaf to develop web applications, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report