首頁  >  文章  >  Java  >  分享幾個在IDEA上面搭建SpringBoot的web-mvc專案常見問題

分享幾個在IDEA上面搭建SpringBoot的web-mvc專案常見問題

Y2J
Y2J原創
2017-04-27 09:42:372752瀏覽

這篇文章主要介紹了IDEA上面搭建一個SpringBoot的web-mvc專案遇到的問題小結,需要的朋友可以參考下

這幾天一直在研究IDEA上面怎麼搭建一個web- mvc的SpringBoot項目,看網路上的教學一步步的搭建,可是還是出現一堆的問題。

為了讓大家以後少走一些彎路,我在這裡分享一下我這幾天研究的成果,也希望對大家能有所幫助。

這裡先介紹各種環境的設定資訊:idea2016.2.1  jdk1.8.0_31

因為SpringBoot中是內建tomcat的,所以也就不需要額外的tomcat配置了,現在開始講如何在idea上面搭建SpringBoot web-mvc項目了

步驟一:在IDEA中新建一個常規的maven項目,具體步驟請看看下面的圖示:


#透過圖上面的幾個步驟,一個基本的maven專案就搭建完成了,接下來就是開始搭建SpringBoot中各種設定檔資訊了。

步驟二:

1.先複製以下程式碼到pox.xml中去

<?xml version="1.0" encoding="UTF-8"?> 
<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>com.example</groupId> 
  <artifactId>demo</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
  <packagingexample>jar</packagingexample> 
  <name>demo</name> 
  <description>Demo project for Spring Boot</description> 
  <parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.4.0.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.8</java.version> 
  </properties> 
  <dependencies> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
    </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-starter-thymeleaf</artifactId> 
    </dependency> 
  </dependencies> 
  <build> 
    <plugins> 
      <plugin> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
    </plugins> 
  </build> 
</project>

2.點選maven中jar包依賴更新按鈕,具體操作看下面圖示:

 3.設定resources下面的Web資源文件,這裡我就設定兩個文件,一個是用來存放靜態資料夾的static文件,還有一個就是用來存放HTML的資源資料夾templates。

這裡需要特別主要的是:static文件中一般存放css,js,image等靜態資源文件,而templates文件中一般存放各種HTML文件。而且這兩個檔案都是預設存在的,路徑不需要特別的配置就可以直接引用了。

application.properties是個設定文件,這裡面可以配置SpringBoot的相關資訊。大家要注意的是這個檔名千萬不要寫錯,也不要放錯位置,不然都不會生效的。

下面看圖示案例和程式碼案例:

csstest.css的程式碼資訊:

body { 
  padding: 0px; 
  margin: auto; 
  font-family: "黑体", "仿宋", Arial, "Arial Unicode MS", System; 
  background-color: #00F; 
  font-size: 20px; 
  text-align: left; 
}

welcome.html的程式碼訊息:

<html> 
<head> 
  <title>Title</title> 
</head> 
<link href="css/csstest.css" rel="external nofollow" rel="stylesheet"/> 
<body> 
  <p>welcome page is login.........</p> 
</body> 
</html>

application.properties設定檔的程式碼資訊:

#修改tomcat的默认的端口号,将8080改为8888 
server.port=8888

4.編寫SpringBoot中Web-Mvc的控制器和專案啟動入口:

#DemoApplication.Java具體程式碼:

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

HelloController.java的具體程式碼:

package example; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 
import java.util.HashMap; 
import java.util.Map; 
@Controller 
public class HelloController { 
  @RequestMapping("/index") 
  public String index(){ 
    return "welcome"; 
  } 
}

這樣SpringBoot的Web-mvc專案就已經搭建成功了,具體步驟就是這樣的。

還有一點需要主要的是:因為我已經把連接埠號碼給修改了,所以存取的時候位址就要寫成 127.0.0.1:8888/index 。

以上是分享幾個在IDEA上面搭建SpringBoot的web-mvc專案常見問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn