延续前篇内容。

开始之前,我们首先要准备以下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容器会选择初始化顺序。

内容如下:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>zoo</display-name> <servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/xmlconfig/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

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

 <?xml version="1.0" encoding="UTF-8"?>  

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- spring扫描com.zoo.web.controller下面所有带注解的类 --> <context:component-scan base-package="com.zoo.web.controller"/>
<!-- 这个标签表示使用注解来驱动 -->
<mvc:annotation-driven/> <!-- 使用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="HTML" />
<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" />
</bean> </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,添加一些静态数据,内容为:

 <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>zoo list</title>
</head>
<body>
<table border="1">
<tr>
<th>序号</th>
<th>动物名称</th>
<th>数量</th>
<th>备注</th>
</tr>
<tr>
<td>1</td>
<td>大马猴</td>
<td>10</td>
<td>机灵古怪,俏皮活泼</td>
</tr>
<tr>
<td>2</td>
<td>大熊猫</td>
<td>80</td>
<td>体型笨重,喜欢吃竹子</td>
</tr>
<tr>
<td>3</td>
<td>澳洲羊驼</td>
<td>13</td>
<td>长相奇特,大国人俗称其草泥马</td>
</tr>
<tr>
<td>4</td>
<td>峨眉山猴</td>
<td>90</td>
<td>不怕人,有时候发贱抢游客面包吃</td>
</tr>
</table>
</body>
</html>

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

让我们启动tomcat试一试吧!

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

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

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

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.zoo.web.controller"/>
<mvc:annotation-driven/> <!-- 使用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="HTML" />
<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>

完成后记得重新启动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来处里。

最后的内容如下:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.zoo.web.controller"/>
<mvc:annotation-driven/>
<!-- 默认servlet -->
<mvc:default-servlet-handler /> <!-- 使用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="HTML" />
<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>

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

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

END.

SpringMVC4+thymeleaf3的一个简单实例(篇二:springMVC与thymeleaf的整合)的更多相关文章

  1. SpringMVC4+thymeleaf3的一个简单实例(篇三:页面参数获取)

    本篇将通过示例介绍页面参数是如何传递到后台的.我们继续沿用之前搭好的程序结构,如果你不知道,请参照前两篇.为方便跳转页面,我们在首页以及zoolist.html页面都加上彼此地址的链接:首页: zoo ...

  2. SpringMVC4+thymeleaf3的一个简单实例(篇一:基本环境)

    首语:用SpringMVC和thymeleaf实现一个简单的应用,包括基本环境搭建,SpringMVC4和thymeleaf3的整合,页面参数的获取,页面参数验证,以及用MySQL保存数据.我会把步骤 ...

  3. SpringMVC4+thymeleaf3的一个简单实例(篇五:页面和MySql的数据交互-展示以及存储)

    这一篇将介绍怎样把页面数据保存的MySQL数据库,并将数据库内容展示到页面上.首先做一个基础工作,添加以下jar到lib:1: mysql-connector-Java-5.1.40-bin.jar ...

  4. SpringMVC4+thymeleaf3的一个简单实例(篇四:form表单数据验证)

    关于表单数据验证有很多中方法,这里我仅介绍JSR303注解验证.JSR303仅仅是一个规范,这里我们要用到它的一个实现:hibernate-validator. 注意在spring的配置文件sprin ...

  5. (Hibernate进阶)Hibernate搭建开发环境+简单实例(二)

    hibernate是非常典型的持久层框架,持久化的思想是非常值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和 ...

  6. 【SSH进阶之路】Hibernate搭建开发环境+简单实例(二)

    Hibernate是很典型的持久层框架,持久化的思想是很值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和原理 ...

  7. 自己制作一个简单的操作系统二[CherryOS]

    自己制作一个简单的操作系统二[CherryOS] 我的上一篇博客 自己制作一个简单的操作系统一[环境搭建], 详细介绍了制作所需的前期准备工作 一. 一点说明 这个操作系统只是第一步, 仅仅是开机显示 ...

  8. C++ 容器的综合应用的一个简单实例——文本查询程序

    C++ 容器的综合应用的一个简单实例——文本查询程序 [0. 需求] 最近在粗略学习<C++ Primer 4th>的容器内容,关联容器的章节末尾有个很不错的实例.通过实现一个简单的文本查 ...

  9. Ajax实现局部数据交互的一个简单实例

    想要实现的功能:利用Ajax技术通过点击一个<button>按钮,然后在指定的文本框中输出想要的值. 1.使用Jsp创建一个前端页面. <body> <div style ...

随机推荐

  1. 关于HttpsURLConnection的连接问题

    本地测试好的项目拿到服务器上后,通过SSL连接,将Http改成Https,并指定了服务器的IP,结果连接失败.查了资料后发现,直接指定IP,SSL是无法定位连接的,实际上应该指定服务器端配置好的Hos ...

  2. Linux学习笔记18——信号1

    一 信号的基本概念 信号:是向进程发送的软件通知,通知进程有事件发生. 生成:表示一个信号的产生. 捕获:表示接收到一个信号. 信号的寿命:信号的生成和传递之间的时间间隔. 挂起的信号:已经生成但还未 ...

  3. 在MacOSX下用管理员权限打开App应用程序

    最近但疼的事情比较多,特别是升级了10.9以后. 难怪10.9会免费,它喵的当我们所有人都是测试开发者,那我们做实验,到处都是BUG...虽然是这么吐槽了,但是实际上也没有特别大的,能够影响到我的生活 ...

  4. Java IO复习(一)

    package com.zyw.file; import java.io.*; /** * Created by zyw on 2016/3/10. */ public class IOTest { ...

  5. hdu 4287

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4287 #include<cstdio> #include<cstring> # ...

  6. FastDfs 说明、安装、配置

    fastdfs是一个开源的,高性能的的分布式文件系统,他主要的功能包括:文件存储,同步和访问,设计基于高可用和负载均衡,fastfd非常适用于基于文件服务的站点,例如图片分享和视频分享网站 fastf ...

  7. mahout算法源码分析之Collaborative Filtering with ALS-WR拓展篇

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 额,好吧,心头的一块石头总算是放下了.关于Collaborative Filtering with AL ...

  8. 容斥原理应用(求1~r中有多少个数与n互素)

    问题:求1~r中有多少个数与n互素. 对于这个问题由容斥原理,我们有3种写法,其实效率差不多.分别是:dfs,队列数组,位运算. 先说说位运算吧: 用二进制1,0来表示第几个素因子是否被用到,如m=3 ...

  9. memcache基本讲解

    Memcached技术 介绍: memcached是一种缓存技术, 他可以把你的数据放入内存,从而通过内存访问提速,因为内存最快的, memcached技术的主要目的提速, 在memachec 中维护 ...

  10. maven tomcat1.7环境下构建javaweb 项目

    tomcat用户权限设置 在tomcat安装路径\conf目录下tomcat-users.xml添加: <role rolename="admin-gui"/> < ...