zoukankan      html  css  js  c++  java
  • thymeleaf 的hello world

    在研究一个模板引擎,选了这个thymeleaf 。中间遇到很多的问题。现在在这里记录一下。

    第一步:导入jar包。这里使用maven导入jar包

        <!-- thymeleaf 核心包  begin -->  
            <dependency>
                <groupId>org.thymeleaf</groupId>
                <artifactId>thymeleaf</artifactId>
                <version>2.1.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.thymeleaf</groupId>
                <artifactId>thymeleaf-spring4</artifactId>
                <version>2.1.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.thymeleaf.extras</groupId>
                <artifactId>thymeleaf-extras-springsecurity3</artifactId>
                <version>2.1.2.RELEASE</version>
         </dependency> 
      </dependencies>
      <!--  thymeleaf心包  end --> 

    第二步:spring-mvc-servlet.xml 中加入thymeleaf解析。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans  
        xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xmlns:context="http://www.springframework.org/schema/context"    
        xmlns:mvc="http://www.springframework.org/schema/mvc"    
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-3.2.xsd  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
        <!--配置自动扫描的包-->
        <context:component-scan base-package="com.sun.controller"/>
        
        <!-- 默认servlet -->  
       <mvc:default-servlet-handler /> 
    
        <mvc:annotation-driven>
    	   <mvc:message-converters>
    	            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
    	            <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
    	            <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    		            <property name="supportedMediaTypes">  
    		            <list>  
    		                <value>text/html;charset=UTF-8</value>  
    		            </list>  
    	        		</property>  
    	            </bean>  
    	   </mvc:message-converters>
    	</mvc:annotation-driven>
    
       <!--   使用jsp解析 -->  
        <!--配置视图解析器:如何把handle方法返回值解析为实际的物理视图-->
       <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/"/>
            <property name="suffix" value=".jsp"/>
        </bean> -->
        
       <!--   使用thymeleaf解析 -->  
       <bean id="templateResolver"  
             class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">  
         <property name="prefix" value="/WEB-INF/pages/" />  
         <property name="suffix" value=".html" />  
          <property name="templateMode" value="HTML5" />  
         <!-- 页面缓存,开发时,可设置false,生产中可设置为true -->
         <property name="cacheable" value="false" />  
       </bean> 
       
       <bean id="templateEngine"  
             class="org.thymeleaf.spring4.SpringTemplateEngine">  
         <property name="templateResolver" ref="templateResolver" />  
       </bean> 
       
        <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">  
         <property name="templateEngine" ref="templateEngine" />
         <!--解决中文乱码-->  
         <property name="characterEncoding" value="UTF-8"/>  
       </bean> 
    </beans>
    

      这里注意了,templateMode 一定要设置为HTML5 ,不然会出问题。

    第三步:控制器的方法

        @RequestMapping(path="/thymeleaftest", method = RequestMethod.GET)
        public String thymeleaftest(Model model){
            
            System.out.println("thymeleaftest is start");
            model.addAttribute("name", "你好,page name ====== thymeleaftest");
            
            return "thymeleaftest";
        }

    第四步:页面thymeleaftest.html

    <!DOCTYPE html>  
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:th="http://www.thymeleaf.org">  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
    <title>hello world</title>  
    </head>  
    <body>  
        <B>hello world</B>
        <B th:utext="${name}"></B>
        <p th:utext="${name}"></p>
    </body>  
    </html>
    

     然后访问,这里的访问和普通的springmvc的地址访问有点区别,thymeleaftest 是action名 + html ,而不是直接就是html名。

    http://localhost:8080/myblog/thymeleaftest.html

    就能够看到页面的效果了。

  • 相关阅读:
    SpeedPHP多入口设置 前台和后台入口分开
    php重定向 htaccess文件的编写
    SpeedPHP关于一对一和一对多关联关系的建立 model建立
    SpringMVC与Struts2关于controller线程安全问题
    编码问题 关于hibernate jdbc数据库连接在xml配置与在properties文件配置的差异
    SpringMVC可以配置多个拦截后缀*.html和.do等
    JavaSE replaceAll 方法
    Promise.race 的原理
    Promise.all 的原理
    标签联合
  • 原文地址:https://www.cnblogs.com/sunxun/p/9079898.html
Copyright © 2011-2022 走看看