Spring boot快速搭建

来源:互联网 发布:百视通网络电视直播 编辑:程序博客网 时间:2024/06/09 15:17

Spring boot微框架的搭建


意义:

简化spring应用的创建,运行,调试,部署等出现的,让开发者不再为xml配置文件而花费时间,可以快速提升开发过程


使用(基于spring boot1.5.7.RELEASE版本):

首先在maven项目依赖中引入spring-boot-starter-parent

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>Demo</groupId>  <artifactId>demo-springboot</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>war</packaging>  <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.7</java.version><!-- Additionally, Please make sure that your JAVA_HOME is pointing to 1.8 when building on commandline --></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>


再创建启动类src/main/java/com/controller/BootApplication.java:

package com.controller;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class BootApplication {public static void main(String[] args) {SpringApplication.run(BootApplication.class, args);}}

这儿要注意一点:启动类需要在项目路径最高级,不然在处理请求或者调用方法时会报错

还有controller类:src/main/java/com/controller/DemoController.java:

package com.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.RestController;import org.springframework.web.servlet.ModelAndView;@RestController//包含@Controller与@ResponseBody,每次都会返回String或json,无法跳转页面public class DemoController {//直接返回String@RequestMapping("/test")public String test(){return "test";}//若想返回页面,可以通过返回ModelAndView视图数据,来完成跳转@RequestMapping("/hello")public ModelAndView hello(Model model){ModelAndView view = new ModelAndView("hello");return view;}@RequestMapping("/hello/{name}")public ModelAndView helloname(@PathVariable("name") String name,Model model){ModelAndView view = new ModelAndView("hello");model.addAttribute("name", name);return view;}}

@RestController包含了@Controller@ResponseBody,每次会默认返回个Stringjson数据,会导致无法跳转页面。若想跳转页面,可以通过返回ModelAndView视图数据来完成

若要返回页面,pom.xml中需要引入thymeleaf模板依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
接下来在src/main/resources/templates/下添加html文件hello.html:
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"></meta><title>Insert title here</title></head><body><h1>Hello World</h1><h2 th:text="${name}"></h2></body></html>

<h2 th:text="${name}"></h2>是处理请求中添加至Modelname。关于模板的更多用法,请参考Thymeleaf官方文档


最后,项目结构图:






原创粉丝点击