zoukankan      html  css  js  c++  java
  • spring boot: thymeleaf模板引擎使用

    spring boot: thymeleaf模板引擎使用

    在pom.xml加入thymeleaf模板依赖

    <!-- 添加thymeleaf的依赖 -->  
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
        
    

      

    在applicationContext.properties中增加thymeleaf配置

    ########################################################
    ###THYMELEAF (ThymeleafAutoConfiguration)
    ########################################################
    spring.thymeleaf.prefix=classpath:/templates/
    spring.thymeleaf.suffix=.html
    #spring.thymeleaf.mode=HTML5
    spring.thymeleaf.encoding=UTF-8
    # ;charset=<encoding> is added
    spring.thymeleaf.content-type=text/html
    # set to false for hot refresh 
    spring.thymeleaf.cache=false
    

      

    #<!-- 关闭thymeleaf缓存 开发时使用 否则没有实时画面-->
    spring.thymeleaf.cache=false
    ## 检查模板是否存在,然后再呈现
    spring.thymeleaf.check-template-location=true
    #Content-Type值
    spring.thymeleaf.content-type=text/html
    #启用MVC Thymeleaf视图分辨率
    spring.thymeleaf.enabled=true
    ## 应该从解决方案中排除的视图名称的逗号分隔列表
    ##spring.thymeleaf.excluded-view-names=
    #模板编码
    spring.thymeleaf.mode=LEGACYHTML5
    # 在构建URL时预先查看名称的前缀
    spring.thymeleaf.prefix=classpath:/templates/
    # 构建URL时附加查看名称的后缀.
    spring.thymeleaf.suffix=.html
    # 链中模板解析器的顺序
    #spring.thymeleaf.template-resolver-order= o
    # 可以解析的视图名称的逗号分隔列表
    #spring.thymeleaf.view-names=
    #thymeleaf end
    

      

    来看下我的实例:

    pom.xml参考

    <parent>
      	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-starter-parent</artifactId>
      	<version>1.5.9.RELEASE</version>
      </parent>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
      </properties>
    
      <dependencies>
      
      
      <!-- spring mvc,aop,restful,fastjson -->
      <dependency>
      	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      
      
      <!-- jstl -->
      <dependency>
      	<groupId>javax.servlet</groupId>
      	<artifactId>jstl</artifactId>
      </dependency>
      
      <!-- tomcat的支持 -->
      <!-- dependency>
      	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-starter-tomcat</artifactId>
      	<scope>provided</scope>
      </dependency>
      <dependency>
      	<groupId>org.apache.tomcat.embed</groupId>
      	<artifactId>tomcat-embed-jasper</artifactId>
      	<scope>provided</scope>
      </dependency-->
      
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
      </dependency>
      
      <!-- mysql支持 -->
      <dependency>
      	<groupId>mysql</groupId>
      	<artifactId>mysql-connector-java</artifactId>
      </dependency>
      
     <!-- 添加spring-data-jpa依赖 -->
     <!-- 引入了 spring-data-jpa就不需要引入spring-boot-starter-jdbc -->
     <dependency>
     	<groupId>org.springframework.boot</groupId>
     	<artifactId>spring-boot-starter-data-jpa</artifactId>
     </dependency>
     
        
      <!-- 添加thymeleaf的依赖 -->  
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
        
        
        
        <!-- fastjson -->  
        <dependency>
    			<groupId>com.alibaba</groupId>
    			<artifactId>fastjson</artifactId>
    			<version>1.2.15</version>
    		</dependency>
        
        <!-- 热部署 -->
        <dependency>
        	<groupId>org.springframework.boot</groupId>
        	<artifactId>spring-boot-devtools</artifactId>
        	<optional>true</optional>
        	<scope>true</scope>
        </dependency>
        
      </dependencies>
      
      <build>
      	<plugins>
      	
      		<!-- 热部署  -->
      		<plugin>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-maven-plugin</artifactId>
      			<configuration>
      				<!--fork :  如果没有该项配置,呢个devtools不会起作用,即应用不会restart -->
      				<fork>true</fork>
      			</configuration>
      		</plugin>
      		
      	</plugins>
      </build>
      
    

      

    App.java参考

    package com.muyang.boot22;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.http.converter.HttpMessageConverter;
    
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    
    /**
     * Hello world!
     *
     */
    
    @SpringBootApplication
    public class App 
    {
    	
    	//fastJson配置
    	@Bean
    	public HttpMessageConverters fastJsonHttpMessageConverters()
    	{
    		
    		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    		
    		FastJsonConfig fastJsonConfig = new FastJsonConfig();
    		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    		
    		fastConverter.setFastJsonConfig(fastJsonConfig);
    		
    		HttpMessageConverter<?> converter = fastConverter;
    		return new HttpMessageConverters(converter);
    	
    	}
    	
        public static void main( String[] args )
        {
            //System.out.println( "Hello World!" );
        	SpringApplication.run(App.class, args);
        }
    }
    

      

    applicationContext.properties

    #####################################
    ###default-view
    #####################################
    #spring.mvc.view.prefix=/WEB-INF/jsp/
    #spring.mvc.view.suffix=.jsp
    
    
    ################################
    ### spring port
    ################################
    server.port = 8081
    server.context-path=/springboot
    
    
    ################################
    ### zh/cn
    ###############################
    spring.http.encoding.force=true
    spring.http.encoding.charset=UTF-8
    spring.http.encoding.enabled=true
    server.tomcat.uri-encoding=UTF-8
    
    
    ########################################################
    ###THYMELEAF (ThymeleafAutoConfiguration)
    ########################################################
    spring.thymeleaf.prefix=classpath:/templates/
    spring.thymeleaf.suffix=.html
    #spring.thymeleaf.mode=HTML5
    spring.thymeleaf.encoding=UTF-8
    # ;charset=<encoding> is added
    spring.thymeleaf.content-type=text/html
    # set to false for hot refresh 
    spring.thymeleaf.cache=false
    
    
    
    
    #######################################
    ####mysql
    #######################################
    
    spring.datasource.url=jdbc:mysql://localhost:3306/spring
    spring.datasource.username=root
    spring.datasource.password=
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.max-idle=10
    spring.datasource.max-wait=10000
    spring.datasource.min-idle=5
    spring.datasource.initial-size=5
    

      

    HelloController.java

    package com.muyang.boot22.controller;
    
    import java.io.IOException;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping(value = "/")
    public class HelloController 
    {
    
    	/*@RequestMapping("/hello")
    	public String index(Map<String, Object>map)
    	{
    		map.put("name", "张三");
    		return "hello";
    		//int i = 1024/0;
    	}*/
    	
    	
    	@RequestMapping(value="/hello")
    	public String thyhello(Map<String, Object> map)
    	{
    		map.put("name", "张三");
    		map.put("age", "56");
    		map.put("gender", "man");
    		return "hello";
    		
    	}
    	
    	@RequestMapping(value = "/home")
         public String homePage(Map<String, Object> map)throws IOException{
    		return "home";
         }
    	
    	//跳转
    	 @GetMapping(value = "/index")
         public void homePage(HttpServletResponse response)throws IOException{
             response.sendRedirect("home.html");
    //         return "index";
         }
    	
    }
    

      

    hello.html

    <!DOCTYPE html>
    <html>
    <head>thymeleaf - views</head>
    <body>
    
          <h1>
    	 	Hello,thymeleaf
    	 	<br />
    	 	This is my first thymeleaf demo.
    	 	
    	 	
    	 	<hr />
    	 	 	name: <span th:text="${name}"></span>
    	 	age: <span th:text="${age}"></span>
    	 	gender: <span th:text="${gender}"></span>
    	 	
    	 	
    	 	</h1>
    	 	
    	 	ssssssssssss
    </body>
    </html>
    

      

    访问地址:

    http://localhost:8081/springboot/hello

    http://localhost:8081/springboot/home.html

    http://localhost:8081/springboot/index

  • 相关阅读:
    编写BinIoDemo.java的Java应用程序,程序完成的功能是:完成1.doc文件的复制,复制以后的文件的名称为自己的学号姓名.doc。
    编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt
    编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。
    事物 视图 与索引
    大家跟我一起涨知识(高级查询与分页)
    涨知识Style
    数据库打印图形
    SQL 编程
    新闻发布系统
    数据库分页储存
  • 原文地址:https://www.cnblogs.com/achengmu/p/9373637.html
Copyright © 2011-2022 走看看