IDEA上面搭建一个SpringBoot的web-mvc项目

步骤一:在IDEA中新建一个常规的maven项目,具体步骤请看看下面的图示:



通过图上面的几个步骤,一个基本的maven项目就搭建完成了,接下来就是开始搭建SpringBoot中各种配置文件信息了。

步骤二:

1.先复制以下代码到pox.xml中去

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.     <groupId>com.example</groupId>  
  6.     <artifactId>demo</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packagingexample>jar</packagingexample>  
  9.     <name>demo</name>  
  10.     <description>Demo project for Spring Boot</description>  
  11.     <parent>  
  12.         <groupId>org.springframework.boot</groupId>  
  13.         <artifactId>spring-boot-starter-parent</artifactId>  
  14.         <version>1.4.0.RELEASE</version>  
  15.         <relativePath/> <!-- lookup parent from repository -->  
  16.     </parent>  
  17.     <properties>  
  18.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  19.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  20.         <java.version>1.8</java.version>  
  21.     </properties>  
  22.     <dependencies>  
  23.         <dependency>  
  24.             <groupId>org.springframework.boot</groupId>  
  25.             <artifactId>spring-boot-starter-web</artifactId>  
  26.         </dependency>  
  27.         <dependency>  
  28.             <groupId>org.springframework.boot</groupId>  
  29.             <artifactId>spring-boot-starter-test</artifactId>  
  30.             <scope>test</scope>  
  31.         </dependency>  
  32.         <dependency>  
  33.             <groupId>org.springframework.boot</groupId>  
  34.             <artifactId>spring-boot-starter-thymeleaf</artifactId>  
  35.         </dependency>  
  36.     </dependencies>  
  37.     <build>  
  38.         <plugins>  
  39.             <plugin>  
  40.                 <groupId>org.springframework.boot</groupId>  
  41.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  42.             </plugin>  
  43.         </plugins>  
  44.     </build>  
  45. </project>  
2.点击maven中jar包依赖更新按钮,具体操作看下面图示:


 3.配置resources下面的Web资源文件,这里我就配置两个文件,一个是用来存放静态文件夹的static文件,还有一个就是用来存放HTML的资源文件夹templates。

这里需要特别主要的是:static文件中一般存放css,js,image等静态资源文件,而templates文件中一般存放各种HTML文件。而且这两个文件都是默认存在的,路径不需要特别的配置就可以直接引用了。

application.properties是个配置文件,这里面可以配置SpringBoot的相关信息。大家需要注意的是这个文件名千万不要写错,也不要放错位置,不然都不会生效的。

下面看图示案例和代码案例:



csstest.css的代码信息:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. body {  
  2.     padding: 0px;  
  3.     margin: auto;  
  4.     font-family: "黑体", "仿宋", Arial, "Arial Unicode MS", System;  
  5.     background-color: #00F;  
  6.     font-size: 20px;  
  7.     text-align: left;  
  8. }  
welcome.html的代码信息:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <html>  
  2. <head>  
  3.     <title>Title</title>  
  4. </head>  
  5. <link href="css/csstest.css" rel="stylesheet"/>  
  6. <body>  
  7.     <p>welcome page is login.........</p>  
  8. </body>  
  9. </html>  
application.properties配置文件的代码信息:
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #修改tomcat的默认的端口号,将8080改为8888  
  2. server.port=8888  
4.编写SpringBoot中Web-Mvc的控制器和项目启动入口:

DemoApplication.Java具体代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package example;  
  2. import org.springframework.boot.SpringApplication;  
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  4.   
  5. @SpringBootApplication  
  6. public class DemoApplication {  
  7.     public static void main(String[] args) {  
  8.         SpringApplication.run(DemoApplication.class, args);  
  9.     }  
  10. }  

HelloController.java的具体代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package example;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. import org.springframework.web.bind.annotation.ResponseBody;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7. @Controller  
  8. public class HelloController {  
  9.     @RequestMapping("/index")  
  10.     public String index(){  
  11.         return "welcome";  
  12.     }  
  13. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值