学习搭建springboot框架

springboot最近比较流行吧,少了很多spring配置,以前要搭建spring框架,配置时间挺头疼的事。springboot相对来说就要方便很多,而且后期使用springcould微服务,也是很好的扩展。下面把我的搭建分享给大家

这里用maven搭建

maven项目创建不会参考我这一篇文章http://blog.csdn.net/qq_28345313/article/details/76796050

好了,接下来开始导包了。

springboot搭建mvc需要的jar包很少

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>mx</groupId>
  <artifactId>mx3</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mx3 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <!--springboot父依赖包  -->
  <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.7</java.version>
  <thymeleaf>1.3.3.RELEASE</thymeleaf>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
      <!--web包  一般配置基本springbootmvc项目 一个父包一个web包就够了-->
    <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

<!-- 支持常规的测试依赖,包括JUnit、Hamcrest、Mockito以及spring-test模块。 -->
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
  </dependency>
  <!--引入了Spring Boot默认的HTTP引擎Tomcat。  -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<!-- 用于编译jsp,springboot内置tomcat,不需要再部署到web容器进行编译jsp文件 -->
<dependency>  
   <groupId>org.apache.tomcat.embed</groupId>  
   <artifactId>tomcat-embed-jasper</artifactId>  
   <scope>provided</scope>  
</dependency>  
  
<!-- servlet 依赖 -->  
<dependency>  
   <groupId>javax.servlet</groupId>  
   <artifactId>javax.servlet-api</artifactId>  
   <scope>provided</scope>  
</dependency>  
  
<!-- 添加 JSTL 支持 -->  
<dependency>  
   <groupId>javax.servlet</groupId>  
   <artifactId>jstl</artifactId>  
</dependency>  
  </dependencies>
  <build>
    <finalName>mx3</finalName>
  </build>
</project>

好了下面新建一个配置文件

application.properties

里面加入

spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.jsp

跳转路径和跳转后文件后缀

然后创建controller

package com.mx3.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class IndexController {

@RequestMapping("/index1")
public String index(HttpServletRequest request,HttpServletResponse response){
request.setAttribute("name", "测试传输数据");
return "index1";
}

}


然后创建启动类,springboot启动采用的是启动类启动,不需要在部署到tomcat

package com.mx3.controller;


import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;


@Configurable
@ComponentScan
@EnableAutoConfiguration
public class StartSpring {


public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(StartSpring.class);
}


}

然后在

/webapp/views/下创建

index1.jsp文件。这个路径是上面application.properties中配置的views文件夹下

项目文件大概就是这样


好了,启动项目

在StartSpring.java中右键run运行Java Application

启动成功你会看到




这样就代表启动成功

 Tomcat started on port(s): 8080 (http)代表你项目tomcat端口,然后在浏览器输入

http://localhost:8080/index1/

这里注意,不需要加项目名