Intellij+spring boot+spring MVC创建helloworld示例完整步骤

1. 创建spring boot项目

clip_image001

选择spring initializr,然后选择default

clip_image003

点击next,填写项目信息

clip_image005

点击“next”,选择web->web

clip_image007

点击“next”,填写项目信息

clip_image009

点击“finish”,在新窗口打开后项目结构如下

clip_image010

2. 添加rest controller

在com.spboot.mvcdemo右键添加new class

clip_image012

创建HelloController,代码如下

package com.spboot.mvcdemo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/")
public String Hello(){
return "hello world!";
}
}

clip_image014

然后直接运行,运行后访问http://localhost:8080

clip_image016

到此我们可以通过rest进行api的开发了。

3. 添加mvc支持

在pom中添加springmvc和themeleaf的依赖

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

clip_image017

创建mvcController类

clip_image018

代码如下:

package com.spboot.mvcdemo;

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

@Controller
public class HelloMVCController {

     @RequestMapping("/mvc")
     public String Hello(){
         return "hello";
     }

}

clip_image020

在resources/templates下创建hello.html文件

clip_image021

代码如下:

clip_image023

记住:html页面中一定要加入<html xmlns:th="http://www.thymeleaf.org"> 这句话,否则themeleaf引擎无法识别。

配置完成后,重新运行,访问http://localhost:8080/mvc,效果如下:

clip_image025

这种模式,对于新创建的项目,或是以前就是用html做为前端的view层的可以采用。但是对于一些老的项目,用的jsp做为view,要如何改造呢,继续看下面。

4. 添加对jsp的支持

在pom中添加对jsp的依赖,同时要把themeleaf的依赖注释掉

<!--用于编译jsp-->
<dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-jasper</artifactId>
     <!--<scope>provided</scope>-->
</dependency>

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
</dependency>
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot</artifactId>
     <version>2.0.1.RELEASE</version>
</dependency>

clip_image026

添加webapp的目录,结构如下:

clip_image028

在jsp目录下添加view页面。

在application.properties中添加如下的配置

spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp

clip_image029

在controller中添加一个mapping

@RequestMapping("/welcome")
public String welcome(){
     return "welcome";
}

clip_image030

在启动类添加ServletComponentScan的标注,同时要继承SpringBootServletInitializer
 

clip_image032

重新运行应用后访问http://localhost:8080/welcome

clip_image034

5. 发布到github

创建一个仓库名字为springbootMVCDemo,创建完成后,如图:

clip_image036

在本地找一个目录,clone一下先。

https://github.com/lileihappy123/springbootMVCDemo.git

clip_image037

完成后在本地会有一个springbootMVCDemo的目录

clip_image038

把代码copy到目录下

因为我这里有图形界面所以比较简单,直接右键git commit->master,弹出提交界面,全选,输入comments, 点击”commit & push”

clip_image040

等待上传完成

clip_image041

然后到github上查看即可查看到上传的代码

clip_image043

关注微信公众号”挨踢学霸”,更多IT姿势在等你

clip_image045

猜你喜欢

转载自blog.51cto.com/12482328/2136063
今日推荐