• SpringMVC4+thymeleaf3的一个简单实例(篇二:springMVC与thymeleaf的整合)


    延续前篇内容。

    开始之前,我们首先要准备以下12个jar文件:
    spring-aop-4.3.3.RELEASE.jar
    spring-beans-4.3.3.RELEASE.jar
    spring-context-4.3.3.RELEASE.jar
    spring-core-4.3.3.RELEASE.jar
    spring-expression-4.3.3.RELEASE.jar
    spring-web-4.3.3.RELEASE.jar
    spring-webmvc-4.3.3.RELEASE.jar
    thymeleaf-3.0.2.RELEASE.jar
    thymeleaf-spring4-3.0.2.RELEASE.jar
    attoparser-2.0.1.RELEASE.jar
    slf4j-api-1.6.6.jar
    commons-logging-1.2.jar

    它们来自于spring-framework-4.3.3.RELEASE-dist.zip,thymeleaf-3.0.2.RELEASE-dist.zip,commons-logging-1.2-bin.tar.gz,请从官网下载
    spring: http://repo.spring.io/release/org/springframework/spring/
    thymeleaf: https://dl.bintray.com/thymeleaf/downloads/thymeleaf/
    common log: http://commons.apache.org/proper/commons-logging/download_logging.cgi

    1: 把12个jar文件拷贝到WEB-INF的lib目录下(eclipse支持鼠标拖拽),并添加到build path下:

    查看项目所在硬盘路径:鼠标右击项目 zoo,下拉菜单->Properties,弹出对话框即可查看:

    2: 修改web.xml文件,添加spring的dispatcherServlet;指定spring的配置文件:classpath下的com/xmlconfig/spring-mvc.xml文件;过滤所有以.html结尾的请求。

    load-on-startup参数设置为1,表示web应用启动的时候就会实例化这个servlet,数值必须是整数,如果是负整数或者没有设置,那么web容器自己会选择它的初始化时机,如果是大于等于0的整数,那么这个servlet会在web应用启动的时候初始化,并且数字越小越先被初始化,对于值相等的servlet,web容器会选择初始化顺序。

     内容如下:

     1 <?xml version="1.0" encoding="UTF-8"?>  
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"   
     3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
     4 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
     5   <display-name>zoo</display-name>  
     6   
     7   <servlet>  
     8     <servlet-name>springMVC</servlet-name>  
     9     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    10     <init-param>  
    11       <param-name>contextConfigLocation</param-name>  
    12       <param-value>classpath:com/xmlconfig/spring-mvc.xml</param-value>  
    13     </init-param>  
    14     <load-on-startup>1</load-on-startup>  
    15   </servlet>  
    16   
    17   <servlet-mapping>  
    18     <servlet-name>springMVC</servlet-name>  
    19     <url-pattern>*.html</url-pattern>  
    20   </servlet-mapping>  
    21     
    22   <welcome-file-list>  
    23     <welcome-file>index.html</welcome-file>  
    24     <welcome-file>index.htm</welcome-file>  
    25     <welcome-file>index.jsp</welcome-file>  
    26     <welcome-file>default.html</welcome-file>  
    27     <welcome-file>default.htm</welcome-file>  
    28     <welcome-file>default.jsp</welcome-file>  
    29   </welcome-file-list>  
    30 </web-app> 

    3: 在src目录下新建package:com.xmlconfig(其实就是硬盘里的文件夹src/com/config),并在这个package下面新建xml文件:
    spring-mvc.xml,内容为:

     1 <?xml version="1.0" encoding="UTF-8"?>  
     2   
     3 <beans xmlns="http://www.springframework.org/schema/beans"  
     4     xmlns:mvc="http://www.springframework.org/schema/mvc"  
     5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     6     xmlns:context="http://www.springframework.org/schema/context"  
     7     xsi:schemaLocation="http://www.springframework.org/schema/beans  
     8         http://www.springframework.org/schema/beans/spring-beans.xsd  
     9         http://www.springframework.org/schema/context  
    10         http://www.springframework.org/schema/context/spring-context.xsd  
    11         http://www.springframework.org/schema/mvc  
    12         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
    13     
    14   
    15   <!-- spring扫描com.zoo.web.controller下面所有带注解的类 -->  
    16   
    17   <context:component-scan base-package="com.zoo.web.controller"/>  
    18   <!-- 这个标签表示使用注解来驱动 -->  
    19   <mvc:annotation-driven/>  
    20   
    21   <!-- 使用thymeleaf解析 -->  
    22   <bean id="templateResolver"  
    23         class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">  
    24     <property name="prefix" value="/WEB-INF/pages/" />  
    25     <property name="suffix" value=".html" />  
    26     <property name="templateMode" value="HTML" />  
    27     <property name="cacheable" value="false" />  
    28   </bean>  
    29       
    30   <bean id="templateEngine"  
    31         class="org.thymeleaf.spring4.SpringTemplateEngine">  
    32     <property name="templateResolver" ref="templateResolver" />  
    33   </bean>  
    34   
    35   <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">  
    36     <property name="templateEngine" ref="templateEngine" />  
    37   </bean>  
    38   
    39 </beans> 

    解释:

    prefix,用于指定template所在目录;

    suffix,过滤请求,这里是处理所有以.html结尾的请求;

    templateMode,设置为html;

    cacheable,是否缓存页面,开发时设置为false,这样就可以在不重启服务器的情况下刷新页面即可查看修改效果;

    4: 在src目录下添加package:com.zoo.web.controller,并新建类ZooController,其内容是:

        package com.zoo.web.controller;  
          
        import org.springframework.stereotype.Controller;  
        import org.springframework.web.bind.annotation.RequestMapping;  
        import org.springframework.web.bind.annotation.RequestMethod;  
          
        @Controller  
        public class ZooController {  
          
            @RequestMapping(path = "/list", method = RequestMethod.GET)  
            public String showZooList(){  
                return "zoolist";  
            }  
              
        }  

    类上面的注解@Controller表示这个类是一个Controller类型的组件,spring根据这个注解就可以扫描到并将其注册到容器中,在有http请求过来的时候spring会根据url找到匹配的controller并执行对应的方法。
    @RequestMapping注解的是方法showZooList(),当浏览器访问http://localhost:8080/zoo/list.html地址时就会调用此函数。函数返回的是个字符串,这个字符串对应的是/WEB-INF/pages/下的文件名,记住是不带扩展名的,这个是在spring-mvc.xml中<property name="prefix" value="/WEB-INF/pages/" />配置的。其中method=RequestMethod.GET表示只响应get请求,如果同样的url换成post请求就不会触发这个方法。

    5: 在WEB-INF下新建文件夹pages,并在pages里新建文件zoolist.html,添加一些静态数据,内容为:

     1 <!DOCTYPE html>  
     2 <html>  
     3 <head>  
     4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
     5 <title>zoo list</title>  
     6 </head>  
     7 <body>  
     8     <table border="1">    
     9       <tr>  
    10         <th>序号</th>  
    11         <th>动物名称</th>    
    12         <th>数量</th>   
    13         <th>备注</th>  
    14       </tr>  
    15       <tr>  
    16         <td>1</td>  
    17         <td>大马猴</td>  
    18         <td>10</td>  
    19         <td>机灵古怪,俏皮活泼</td>  
    20       </tr>  
    21       <tr>  
    22         <td>2</td>  
    23         <td>大熊猫</td>  
    24         <td>80</td>  
    25         <td>体型笨重,喜欢吃竹子</td>  
    26       </tr>  
    27       <tr>  
    28         <td>3</td>  
    29         <td>澳洲羊驼</td>  
    30         <td>13</td>  
    31         <td>长相奇特,大国人俗称其草泥马</td>  
    32       </tr>  
    33       <tr>  
    34         <td>4</td>  
    35         <td>峨眉山猴</td>  
    36         <td>90</td>  
    37         <td>不怕人,有时候发贱抢游客面包吃</td>  
    38       </tr>  
    39     </table>  
    40 </body>  
    41 </html> 

    到此为止所需的材料基本上就全都整好啦,现在我们的项目结构大概是这个样子

    让我们启动tomcat试一试吧!

    启动完成后看看你的Console是不是一切正常,如果有出错信息,请自行排查。

    打开浏览器输入http://localhost:8080/zoo/list.html

    哎呀!怎么都是问号呀!?
    别担心,小问题,我们只需在spring-mvc.xml中添加一句话就ok啦拉拉,
    把<property name="characterEncoding" value="UTF-8"/>加进去,放在哪里呀?请使劲看:

     1 <?xml version="1.0" encoding="UTF-8"?>  
     2 <beans xmlns="http://www.springframework.org/schema/beans"  
     3     xmlns:mvc="http://www.springframework.org/schema/mvc"  
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     5     xmlns:context="http://www.springframework.org/schema/context"  
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans  
     7         http://www.springframework.org/schema/beans/spring-beans.xsd  
     8         http://www.springframework.org/schema/context  
     9         http://www.springframework.org/schema/context/spring-context.xsd  
    10         http://www.springframework.org/schema/mvc  
    11         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
    12           
    13   <context:component-scan base-package="com.zoo.web.controller"/>  
    14   <mvc:annotation-driven/>  
    15   
    16   <!-- 使用thymeleaf解析 -->  
    17   <bean id="templateResolver"  
    18         class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">  
    19     <property name="prefix" value="/WEB-INF/pages/" />  
    20     <property name="suffix" value=".html" />  
    21     <property name="templateMode" value="HTML" />  
    22     <property name="cacheable" value="false" />  
    23   </bean>  
    24       
    25   <bean id="templateEngine"  
    26         class="org.thymeleaf.spring4.SpringTemplateEngine">  
    27     <property name="templateResolver" ref="templateResolver" />  
    28   </bean>  
    29   
    30   <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">  
    31     <property name="templateEngine" ref="templateEngine" />  
    32     <!--解决中文乱码-->  
    33     <property name="characterEncoding" value="UTF-8"/>  
    34   </bean>  
    35   
    36 </beans> 

    完成后记得重新启动tomcat,再用浏览器看一看:

    哇,正常了,终于看到大马猴和草泥马啦拉拉!

    等一等,我们是不是忘记了什么?对了,我们再访问一下首页http://localhost:8080/zoo看看

    咿呀呀,搞什么飞机,怎么出错啦,还是404,怎么找不到页面啦,让阿拉想想怎么解决......

    好,有了,同样在spring-mvc.xml中添加一句话就ok,
    把<mvc:default-servlet-handler />加进去就能解决。这句话怎么这么列害!为什么呢?
    让我们想一想整个流程,当http://localhost:8080/zoo这个请求来到tomcat server时,实际上请求的是http://localhost:8080/zoo/index.html或者http://localhost:8080/zoo/default.jsp,总之是wellcome-file-list中的某个页面,可实际上在我们的应用中没有哪个类的哪个方法来响应/index.html或者default.jsp等,所以这句话的意思就是在这条路走不通的情况下使用默认servlet来处理,这个标签对应的是
    org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler 这个handler,它转而调用当前web容器默认的servlet,让当前默认servlet来处里。

    最后的内容如下:

     1 <?xml version="1.0" encoding="UTF-8"?>  
     2 <beans xmlns="http://www.springframework.org/schema/beans"  
     3     xmlns:mvc="http://www.springframework.org/schema/mvc"  
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     5     xmlns:context="http://www.springframework.org/schema/context"  
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans  
     7         http://www.springframework.org/schema/beans/spring-beans.xsd  
     8         http://www.springframework.org/schema/context  
     9         http://www.springframework.org/schema/context/spring-context.xsd  
    10         http://www.springframework.org/schema/mvc  
    11         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
    12           
    13   <context:component-scan base-package="com.zoo.web.controller"/>  
    14   <mvc:annotation-driven/>  
    15   <!-- 默认servlet -->  
    16   <mvc:default-servlet-handler />  
    17     
    18   <!-- 使用thymeleaf解析 -->  
    19   <bean id="templateResolver"  
    20         class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">  
    21     <property name="prefix" value="/WEB-INF/pages/" />  
    22     <property name="suffix" value=".html" />  
    23     <property name="templateMode" value="HTML" />  
    24     <property name="cacheable" value="false" />  
    25   </bean>  
    26       
    27   <bean id="templateEngine"  
    28         class="org.thymeleaf.spring4.SpringTemplateEngine">  
    29     <property name="templateResolver" ref="templateResolver" />  
    30   </bean>  
    31   
    32   <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">  
    33     <property name="templateEngine" ref="templateEngine" />  
    34     <!--解决中文乱码-->  
    35     <property name="characterEncoding" value="UTF-8"/>  
    36   </bean>  
    37   
    38 </beans>

    重启服务器再试试看,是不是好了呢。

    本篇的内容到此也就结束了,下一篇介绍页面参数获取。

    END.

    ---------------------------------------------------
    如果这些内容能给读者带来帮助,那将是莫大欢喜。
    ---------------------------------------------------
  • 相关阅读:
    02.ZooKeeper的Java客户端使用
    01.ZooKeeper安装和介绍
    02.Elasticsearch入门
    01.Elasticsearch安装
    01.ActiveMQ安装部署
    springboot项目打包时提示“程序包xxx不存在,找不到符号”
    Eclipse提交git代码 报错authentication not supported
    Eclipse提交git代码 报错authentication not supported
    utf8mb4_general_ci报错解决方案
    mysql开启远程访问
  • 原文地址:https://www.cnblogs.com/asdop/p/6093599.html
Copyright © 2020-2023  润新知