SpringBootcss
springboot的目的是爲了簡化spring應用的開發搭建以及開發過程。內部使用了特殊的處理,使得開發人員不須要進行額外繁鎖的xml文件配置的編寫,其內部包含不少模塊的配置只須要添加maven依賴便可使用,這項功能可謂對開發人員提供了大大的好處。使用springboot只須要簡單配置一下就能夠完成以前複雜的配置過程。能夠到https://start.spring.io/此網站上,下載一個最簡單的springboot應用,而後一步一步實現自已的應用。html
能夠看出當前的穩定版本爲2.1.0,點擊Generate Project 按鈕,便可下載一個可用的springboot應用。java
這個是我下載下來後,雙擊後出來的。能夠看出以工程是一個基於maven的項目。你能夠將其解壓到任何一個目錄下,經過eclipse或其餘IDE進行導入後運行,eclipse導入流程爲File->import->maven->existing maven projects,查找到本身的項目目錄。也能夠基於此工程來創建自已的maven項目。web
下面以創建本身的maven項目spring
創建本身的springboot項目後端
在創建項目時,能夠建立一個多模塊聚合項目,即在建立項目時選中瀏覽器
選擇爲pom。springboot
建立後的工程結構爲app
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.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>
將此段代碼複製到 spring-boot-study工程中的pom文件中eclipse
將下面的依賴複製到spring-boot-web工程中的pom文件中
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
eclipse自動完成項目工程的配置。完成後項目中全部須要依賴的jar包自動配置完成。
@SpringBootApplication @RestController public class WebApplication { @RequestMapping("/hello") public String helloWorld() { return "Hello World"; } public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } }
HelloWold就已經完成後。能夠在瀏覽器中輸入localhost:8080/hello便可看到效果
springboot默認啓動後的端口爲8080,但能夠在application.properties文件中進行修改。
server.port=9001
將端口修改成9001,從新啓動項目後,在瀏覽器中輸入入localhost:9001/hello一樣能夠看到相同的結果。
<!-- 加入thymeleaf的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
但在Spring Boot項目中,通常src/main/resources/static目錄用於存放各種靜態資源文件,例如css、js和image等。src/main/resources/templates用於存放頁面文件,例如html,jsp等。因此在spring-boot-web中的resources目錄下建立static目錄與templates目錄,並將相應的資源文件放置在各自的目錄下。
配置thymeleaf
#thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.servlet.content-type=text/html spring.thymeleaf.cache=false
html文件修改,增長xmlns:th="http://www.thymeleaf.org" 屬性,資源文件的引入要修改。
<link href="../static/css/style.css" th:href="@{/css/style.css}" rel="stylesheet" /> <link href="../static/css/login.css" th:href="@{/css/login.css}" rel="stylesheet" />
而後編寫 java代碼
@Controller public class IndexController { @RequestMapping("/") public String index() { return "login"; } }
從新啓動程序,訪問localhost:9001/就可成功跳轉至login.html登錄界面上。
注:thymeleaf對html標籤要求很嚴格,每個標籤都須要成對出現。
調試過程當中遇到下面異常信息
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) [thymeleaf-spring5-。。。。。。。。。。。
由於錯將templates寫成templatse致使。
至此實現從後端服務訪問到登錄界面的搭建,尚未具體登錄邏輯實現。
有興趣能夠加一下854630135這個羣去交流一下噢