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,每次会默认返回个String或json数据,会导致无法跳转页面。若想跳转页面,可以通过返回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>是处理请求中添加至Model的name。关于模板的更多用法,请参考Thymeleaf官方文档
最后,项目结构图:
阅读全文
0 0
- Spring boot快速搭建
- Spring boot 快速搭建
- Spring boot快速搭建
- 快速搭建Spring Boot项目
- 快速搭建Spring-boot Demo
- Spring Boot 项目快速搭建
- 快速搭建Spring Boot+Spring MVC
- Spring Boot快速搭建Spring框架
- Spring Boot介绍 Spring Boot RESTfull API快速搭建
- 【Spring Boot教程】快速搭建spring boot项目工程
- Spring boot 快速入门 - 环境搭建
- Eclipse+Maven+Spring-boot快速搭建
- Spring Boot 快速搭建及简单演示
- WEB开发----Spring Boot项目快速搭建
- 使用Spring boot 快速 搭建 OAuth2 Server
- 快速搭建Spring boot CRUD-HelloWorld程序
- Spring-boot:快速搭建微服务框架
- Spring Boot的快速搭建(IDEA)
- 产品经理之交互设计
- Windows打开dcm文件、dcm文件批量转其它图片文件格式
- (思考)谈谈学习这事
- 快速解决tomcat的自动加载问题!
- UVA7267 Mysterious Antiques in Sackler Museum (强行模拟)
- Spring boot快速搭建
- 指针的使用二级指针
- 远程服务器后台运行命令
- List浅拷贝,与深的拷贝探讨
- struts2笔记
- JavaScript闭包
- android拒绝来电的实现demo
- 所有博客
- 嵌入式ARM平台基于Linux配置24bit RGB输出