springboot 入門教程(4)--web開發(spring mvc和Thymeleaf模板,帶源碼)

首先回顧下前幾篇的內容:springboot 入門教程(1),springboot 入門教程-Thymeleaf(2), springboot 入門教程(3)-運行原理、關鍵註解和配置html

這篇呢就直接進入主題擼代碼。(項目使用了maven,你們最好也用maven進行管理哦,要否則很麻煩的)java

爲方便你們快速學習和理解,補充源碼下載地址:http://pan.baidu.com/s/1eSGPyDWweb

一、新建項目,建好目錄結構

咱們從新建一個maven web項目,而後命名好本身的包,個人目錄結構如圖:spring

二、引入springboot和springboot web的包

<dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter</artifactId>
  		<version>1.5.6.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  		<version>1.5.6.RELEASE</version>
  	</dependency>
  </dependencies>

三、編寫啓動類

package com.pxk.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

到這一步咱們就能夠啓動一個web項目了,可是啓動後是一個空的項目(啓動的web容器是spring-boot-starter-web中的自動配置的tomcat端口是8080,固然能夠根據須要修改爲其餘容器)。json

四、咱們要寫本身的bean,而後託管到spring容器中

    這個的用法其實很普通的spring項目沒太大區別,普通spring項目中咱們想託管bean,那能夠用xml配置也能夠用註解(@Repository,@Component,@Service、@Controller、@Bean)配置而後啓動註解掃描便可。springboot也同樣只不過不須要手動開啓註解掃描,默認會掃描項目src/java/main下面的全部類。tomcat

    例:   咱們在domain目錄下建一個User實體類(暫且只有id和name),而後在service目錄中新建一個UserService接口和UserServiceImpl的實現類,類中實現接口的一個方法springboot

UserServiceImplmvc

package com.pxk.springboot.serivce.imp;

import org.springframework.stereotype.Service;

import com.pxk.springboot.domain.User;
import com.pxk.springboot.serivce.UserService;
@Service//注入成service
public class UserServiceImpl implements UserService {

	@Override
	public User getUser(String name) {
		return new User(name);
	}
}

五、編寫一個conntroller

    採用@RestController注入爲一個控制器,而後調用service的業務方法app

package com.pxk.springboot.conntroller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.pxk.springboot.domain.User;
import com.pxk.springboot.serivce.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired//依賴注入service
	UserService userService;
	
	@RequestMapping("/getUser/{name}")//定義requestMapping rest風格
	@ResponseBody//返回json格式數據
	protected User getUser(@PathVariable(value="name") String name){
		return userService.getUser(name);
	}
	
	@RequestMapping("/getUserByName/{name}")//定義requestMapping rest風格
	protected String getUserByName(@PathVariable(value="name") String name,Model model){
		User user= userService.getUser(name);
		model.addAttribute(user);
		return "user";
	}
}

  getUser返回的是json格式呼叫 由於加了@ResponseBody註解,若是你這個conntroller都返回json能夠在類註解上加入@RestConntroller框架

    getUserByName是返回到視圖解析器,視圖名稱爲user,user.html模板默認位置在src/main/resources下。

以上註解都是spring 和spring mvc的註解,若是你有一個已有的spring項目你應該知道怎麼把他遷移爲springboot的項目了吧,移植完之後你會發現沒有那麼多繁瑣的xml配置文件真的是一件很爽的事情。

六、集成Thymeleaf模板引擎

    完成參考springboot 入門教程-Thymeleaf(2)進行集成

七、將後臺準備好的數據加載到模板中

      編寫模板頁面html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>userList</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<!--/*@thymesVar id="name" type="java.lang.String"*/-->
	<p th:text="'my name is:' + ${user.name} + '!'">姓名</p>
</body>
</html>

八、啓動並放

    run  APPlication  ,訪問http://localhost:8080/user/getUserByName/pxk

效果以下圖:

基本和第一篇文章有點相似,這篇只是補充說明了和spring 以及spring mvc的整合。

你們沒有看到一個xml文件吧,是否是很簡單啊。

下一篇springboot 入門教程(5) 基於ssm框架的crud操做(後臺部分-附源碼)

相關文章
相關標籤/搜索