SpringBoot(五)spring-boot-web-Thymeleaf

spring boot 提供了另一种视图方式 Thymeleaf  官方网址:http://www.thymeleaf.org/
闲话不多说 看下示例
mave配置文件

  4.0.0
  com.julongtech
  SpringBoot-Thymeleaf
  jar
  0.0.1-SNAPSHOT
  SpringBoot-Thymeleaf Maven Webapp
  http://maven.apache.org
  
   
        org.springframework.boot
        spring-boot-starter-parent
        1.4.2.RELEASE
    
     
        1.7
    
  
  
     
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
        
            org.springframework.boot
            spring-boot-devtools
            true
        

        
        
            org.webjars
            bootstrap
            3.3.7
        

  
   
    SpringBoot-Thymeleaf
        
            
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

springboot 启动类

package com.julongtech.action;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWebApplication {


    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }
}

controller类

package com.julongtech.action;

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WelcomeController {

	  // inject via application.properties
    @Value("${welcome.message:test}")
    private String message = "Hello World";

    @RequestMapping("/")
    public String welcome(Map model) {
        model.put("message", this.message);
        return "welcome";
    }
}

创建页面wolcome.html 在src/main/resources/ 下创建 templates文件夹,将页面放入此文件夹中




Spring Boot Thymeleaf Hello World示例









    

    

Spring Boot Web Thymeleaf示例

创建 application.properties文件
welcome.message: Hello, Spring Boot
完成之后 启动项目
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.2.RELEASE)

2017-08-22 16:04:10.871  INFO 2712 --- [  restartedMain] c.j.action.SpringBootWebApplication      : Starting SpringBootWebApplication on julong-841deb9b with PID 2712 (started by Administrator in D:\Workspaces\OWER\SpringBoot-Thymeleaf Maven Webapp)
2017-08-22 16:04:10.887  INFO 2712 --- [  restartedMain] c.j.action.SpringBootWebApplication      : No active profile set, falling back to default profiles: default
2017-08-22 16:04:11.168  INFO 2712 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@830cbb: startup date [Tue Aug 22 16:04:11 CST 2017]; root of context hierarchy
2017-08-22 16:04:12.449  INFO 2712 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-08-22 16:04:12.480  INFO 2712 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-08-22 16:04:12.480  INFO 2712 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.6
2017-08-22 16:04:12.527  INFO 2712 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-08-22 16:04:12.527  INFO 2712 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1359 ms
2017-08-22 16:04:12.715  INFO 2712 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-08-22 16:04:12.715  INFO 2712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-08-22 16:04:12.715  INFO 2712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-08-22 16:04:12.715  INFO 2712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-08-22 16:04:12.715  INFO 2712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-08-22 16:04:12.980  INFO 2712 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@830cbb: startup date [Tue Aug 22 16:04:11 CST 2017]; root of context hierarchy
2017-08-22 16:04:13.027  INFO 2712 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.julongtech.action.WelcomeController.welcome(java.util.Map)
2017-08-22 16:04:13.043  INFO 2712 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-08-22 16:04:13.043  INFO 2712 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-08-22 16:04:13.058  INFO 2712 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-22 16:04:13.058  INFO 2712 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-22 16:04:13.090  INFO 2712 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-22 16:04:13.605  INFO 2712 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2017-08-22 16:04:13.652  INFO 2712 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-08-22 16:04:13.715  INFO 2712 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-08-22 16:04:13.715  INFO 2712 --- [  restartedMain] c.j.action.SpringBootWebApplication      : Started SpringBootWebApplication in 3.266 seconds (JVM running for 3.547)
2017-08-22 16:07:27.469  INFO 2712 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-08-22 16:07:27.469  INFO 2712 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-08-22 16:07:27.500  INFO 2712 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 31 ms
打开浏览器 http://localhost:8080/就会看到主界面


你可能感兴趣的:(springboot)