最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何使用SpringMVC和Thymeleaf開發(fā)web應用

這期內容當中小編將會給大家?guī)碛嘘P如何使用 Spring MVC和 Thymeleaf 開發(fā) web 應用,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

為北川羌族等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及北川羌族網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都網(wǎng)站設計、做網(wǎng)站、北川羌族網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!


Spring MVC 是構建在 Servlet API 上的原生框架,并從一開始就包含在 Spring 框架中。本文主要通過簡述 Spring MVC 的架構及分析,并用 Spring Boot + Spring MVC + MyBatis (SSM)+ Thymeleaf(模板引擎) 框架來簡單快速構建一個 Web 項目。

Web MVC 架構及分析


MVC 三層架構如圖所示,紅色字體代表核心模塊。其中 MVC 各分層分別為:

  • **Model (模型層)**處理核心業(yè)務(數(shù)據(jù))邏輯,模型對象負責在數(shù)據(jù)庫中存取數(shù)據(jù)。這里的“數(shù)據(jù)”不僅限于數(shù)據(jù)本身,還包括處理數(shù)據(jù)的邏輯。

  • **View(視圖層)**用于展示數(shù)據(jù),通常數(shù)據(jù)依據(jù)模型數(shù)據(jù)創(chuàng)建。

  • **Controller(控制器層)**用于處理用戶輸入請求和響應輸出,從試圖讀取數(shù)據(jù),控制用戶輸入,并向模型發(fā)送數(shù)據(jù)。Controller 是在 Model 和 View 之間雙向傳遞數(shù)據(jù)的中間協(xié)調者。 如何使用 Spring MVC和 Thymeleaf 開發(fā) web 應用

Spring MVC 架構及分析


Spring MVC 處理一個 HTTP 請求的流程,如圖所示: 如何使用 Spring MVC和 Thymeleaf 開發(fā) web 應用 整個過程詳細介紹: 1.用戶發(fā)送請求至前端控制器 DispatcherServlet。 2.DispatcherServlet 收到請求調用處理器映射器 HandlerMapping。 3.處理器映射器根據(jù)請求 URL 找到具體的 Controller 處理器返回給 DispatcherServlet。 4.DispatcherServlet 通過處理器適配器 HandlerAdapter 調用 Controller 處理請求。 5.執(zhí)行 Controller 處理器的方法。 6.Controller 執(zhí)行完成返回 ModelAndView。 7.HandlerAdapter 將 Controller 執(zhí)行結果 ModelAndView 返回給 DispatcherServlet。 8.DispatcherServlet 將 ModelAndView 的 ViewName 傳給視圖解析器 ViewReslover。 9.ViewReslover 解析后返回具體的視圖 View。 10.DispatcherServlet 傳遞 Model 數(shù)據(jù)給 View,對 View 進行渲染(即將模型數(shù)據(jù)填充至視圖中)。 11-12.DispatcherServlet 響應用戶。

Spring Boot + Spring MVC + MyBatis + Thymeleaf


本段我們主要通過構建項目,實現(xiàn)一個分頁查詢。

1.項目構建

項目結構如圖所示: 如何使用 Spring MVC和 Thymeleaf 開發(fā) web 應用

1.1 pom 引入相關依賴


	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 配置
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;


@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

	/**
	 * 靜態(tài)資源配置
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {	
		registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");//靜態(tài)資源路徑 css,js,img等
		registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");//視圖
		registry.addResourceHandler("/mapper/**").addResourceLocations("classpath:/mapper/");//mapper.xml
		super.addResourceHandlers(registry);
		
	}

	/**
	 * 視圖控制器配置
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {	
		registry.addViewController("/").setViewName("/index");//設置默認跳轉視圖為 /index
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
		
		
	}
	/**
	 * 視圖解析器配置  控制controller String返回的頁面    視圖跳轉控制 
	 */
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
	   // registry.viewResolver(new InternalResourceViewResolver("/jsp/", ".jsp"));
	    super.configureViewResolvers(registry);
	}
	
}
1.3 application.properties 配置
#thymeleaf
spring.thymeleaf.cache=false
#datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=UTF-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
#mybatis
mybatis.mapper-locations=classpath:/mapper/*.xml
#logging
logging.path=/user/local/log
logging.level.cn.zwqh=debug
logging.level.org.springframework.web=info
logging.level.org.mybatis=error
1.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 及 ServiceImpl

UserService

public interface UserService {

	PageInfo getUserList(int pageNum, int pageSize);

}

UserServiceImpl

@Service
public 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("當前頁:"+pageData.getPageNum());
		System.out.println("頁面大?。?+pageData.getPageSize());
		System.out.println("總數(shù):"+pageData.getTotal());	
		System.out.println("總頁數(shù):"+pageData.getPages());	
		return pageData;
	}
}
1.6 Dao
public interface UserDao {
	/**
	 * 獲取所有用戶
	 * @return
	 */
	List getAll();
	
}

記得在啟動類里加上**@MapperScan**

@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 實體 UserEntity
public 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 頁面




Insert title here


Thymeleaf是一個用于Web和獨立環(huán)境的現(xiàn)代服務器端Java模板引擎。SpringBoot推薦使用Thymeleaf。

下面是表格示例:

ID 姓名 性別

=1?pageInfo.pageNum-1:1)+'&pageSize=10'}">上一頁      下一頁     總數(shù):

上述就是小編為大家分享的如何使用 Spring MVC和 Thymeleaf 開發(fā) web 應用了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


新聞標題:如何使用SpringMVC和Thymeleaf開發(fā)web應用
文章鏈接:http://fisionsoft.com.cn/article/jesohe.html