springboot整合thymeleaf模板

时间:2022-11-17 17:54:43

一:在springboot框架基础上整合thymeleaf模板 springboot框架搭建点击查看  “springboot框架搭建”。

二:整合thymeleaf模板

    1:pom文件加入

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

    2:application.properties文件中加入

#THYMELEAF    (ThymeleafAutoConfiguration) 
spring.thymeleaf.check-template-location=false
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.excluded-view-names=
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#\u7F13\u5B58\u5F00\u5173
spring.thymeleaf.cache=true

    3:新建helloHtml.html

springboot整合thymeleaf模板

    内容为:

<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello</h1>
<p th:text="${hello}"></p>
<p th:text="${user.name}"></p>
</body>
</html>



    4:新建HelloController.java

springboot整合thymeleaf模板

    内容为:

package boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
@Controller
@SpringBootApplication
public class HelloController {
@RequestMapping("/helloHtml")
public String helloHtml(Map<String,Object> map){
map.put("hello","from HelloController.helloHtml");
return"/helloHtml";
}

@RequestMapping("/helloHtml2")
public String helloHtml2(User user,HttpServletRequest request){
user.setName("zhangsanss");
request.setAttribute("user", user);
return"/helloHtml";
}
public static void main(String[] args) {
SpringApplication.run(HelloController.class, args);
}
}


    5:运行HelloController.java中main方法启动成功

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.2.5.RELEASE)

2017-02-17 15:38:13.576 INFO 106016 --- [ main] boot.HelloController : Starting HelloController on Zones-LR with PID 106016 (D:\Workspaces2014\springbootdemo\target\classes started by sks in D:\Workspaces2014\springbootdemo)
2017-02-17 15:38:13.630 INFO 106016 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@dc4734b: startup date [Fri Feb 17 15:38:13 CST 2017]; root of context hierarchy
2017-02-17 15:38:14.406 INFO 106016 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2017-02-17 15:38:15.319 INFO 106016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$14d09f53] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-17 15:38:15.342 INFO 106016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'transactionAttributeSource' of type [class org.springframework.transaction.annotation.AnnotationTransactionAttributeSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-17 15:38:15.349 INFO 106016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'transactionInterceptor' of type [class org.springframework.transaction.interceptor.TransactionInterceptor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-17 15:38:15.352 INFO 106016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.config.internalTransactionAdvisor' of type [class org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-17 15:38:15.928 INFO 106016 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-02-17 15:38:16.174 INFO 106016 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-02-17 15:38:16.175 INFO 106016 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.23
2017-02-17 15:38:16.284 INFO 106016 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-02-17 15:38:16.284 INFO 106016 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2656 ms
2017-02-17 15:38:16.857 INFO 106016 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-02-17 15:38:16.862 INFO 106016 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-02-17 15:38:16.863 INFO 106016 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-02-17 15:38:17.158 INFO 106016 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-02-17 15:38:17.166 INFO 106016 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-02-17 15:38:17.230 INFO 106016 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {4.3.10.Final}
2017-02-17 15:38:17.231 INFO 106016 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-02-17 15:38:17.232 INFO 106016 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-02-17 15:38:17.393 INFO 106016 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2017-02-17 15:38:17.628 INFO 106016 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2017-02-17 15:38:17.722 INFO 106016 --- [ main] o.h.h.i.ast.ASTQueryTranslatorFactory : HHH000397: Using ASTQueryTranslatorFactory
2017-02-17 15:38:17.958 INFO 106016 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000228: Running hbm2ddl schema update
2017-02-17 15:38:17.958 INFO 106016 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000102: Fetching database metadata
2017-02-17 15:38:17.960 INFO 106016 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000396: Updating schema
2017-02-17 15:38:17.999 INFO 106016 --- [ main] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000261: Table found: springboot.user
2017-02-17 15:38:17.999 INFO 106016 --- [ main] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000037: Columns: [id, name]
2017-02-17 15:38:18.000 INFO 106016 --- [ main] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000108: Foreign keys: []
2017-02-17 15:38:18.000 INFO 106016 --- [ main] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000126: Indexes: [primary]
2017-02-17 15:38:18.000 INFO 106016 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000232: Schema update complete
2017-02-17 15:38:18.433 INFO 106016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@dc4734b: startup date [Fri Feb 17 15:38:13 CST 2017]; root of context hierarchy
2017-02-17 15:38:18.499 INFO 106016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/helloHtml]}" onto public java.lang.String boot.HelloController.helloHtml(java.util.Map<java.lang.String, java.lang.Object>)
2017-02-17 15:38:18.499 INFO 106016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/helloHtml2]}" onto public java.lang.String boot.HelloController.helloHtml2(boot.User,javax.servlet.http.HttpServletRequest)
2017-02-17 15:38:18.500 INFO 106016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/getbyname]}" onto public java.lang.String boot.UserController.getByName(java.lang.String)
2017-02-17 15:38:18.500 INFO 106016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/save]}" onto public java.lang.String boot.UserController.save()
2017-02-17 15:38:18.500 INFO 106016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/update]}" onto public java.lang.String boot.UserController.update()
2017-02-17 15:38:18.501 INFO 106016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/delete]}" onto public java.lang.String boot.UserController.delete()
2017-02-17 15:38:18.502 INFO 106016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-02-17 15:38:18.502 INFO 106016 --- [ main] 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)
2017-02-17 15:38:18.529 INFO 106016 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-17 15:38:18.529 INFO 106016 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-17 15:38:18.575 INFO 106016 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-17 15:38:18.834 INFO 106016 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-02-17 15:38:18.901 INFO 106016 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-02-17 15:38:18.904 INFO 106016 --- [ main] boot.HelloController : Started HelloController in 5.623 seconds (JVM running for 6.095)

    6:访问http://localhost:8080/helloHtml2

springboot整合thymeleaf模板


三:注意事项

    1:修改模板在浏览器中查看不生效

springboot整合thymeleaf模板


    2:修改java文件重启服务才生效

    点击查看“springboot热部署”。