Spring MVC : Java模板引擎 Thymeleaf (二)
本文原计划直接介绍Thymeleaf的视图解析,但考虑到学习的方便,决定先构建一个spring-mvc。
以下的全部过程仅仅要一个记事本和JDK就够了。
第一步,使用maven构建一个web app。
<span style="font-size:18px;">mvn archetype:generate -DgroupId=org.nwpu.chen -DartifactId=spring-mvc -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false</span>
曾经没用过Maven也没关系。下载后配置好环境变量。简单使用还是非常快的。这里分别用參数给出了groupId(组织名),artifactID(项目名),关键是archetypeArtifactId,指出了项目类型是 maven-archetype-webapp,最后指出不启用交互模式(启用批处理模式)。
完毕后。会自己主动生成文件夹结构。
<span style="font-size:18px;">└──spring-mvc
└── src
└── main
└── resouces
└── webapp
pom.xml</span>
webapp以下就是我们web应用的部分。
第二步。改动pom.xml
随着之后项目的变化,pom还是要改动的。这里先加入编译的插件,和tomcat插件(意味着你不须要安装tomcat)
<span style="font-size:18px;"><build>
<finalName>spring-mvc</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build></span>
就这么简单,不借助IDE(甚至没有事先安装tomcat)。仅仅要记事本就能构建好一个web app。
执行 mvn clean tomcat7:run,就能够再浏览器打开 http://localhost:8080/spring-mvc/
第三步。加入spring-mvc的依赖
開始使用maven開始spring的朋友,可能对究竟该加入 spring-context、spring-core、spring-web、spring-webmvc...混淆不清,事实上非常easy。我们能够在类似 http://mvnrepository.com/的站点上查看我们加入的依赖的直接依赖。当然你也能够使用maven里面的命令查看。
以spring-webmvc为例,有以下的依赖(当然,依赖有其范围。这里不展开讨论)
或者。执行 mvn dependency:list,
说明,此时你仅依赖srping-webmvc是足够的。
所以,在pom.xml加入,
<span style="font-size:18px;"> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.6.RELEASE</version>
</dependency></span>
第四步。spring-mvc的配置
这方面最好的教程应该就是 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html。
MVC的过程实际就是一个请求流动的过程。在Spring,接受请求的开端就是 DispatcherServlet,可看成一个前端控制器。
看名字,它就是分派的作用。即把请求分派给合适的控制器。
接下来就是处理请求,返回视图了。
所以,在web.xml里面配置DispatcherServlet,
<span style="font-size:18px;"> <servlet>
<servlet-name>webmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet></span>
这里的servlet-name非常重要。由于默认情况下,Spring会让DispatcherServlet在载入时从一个基这个名字的xml文件载入上下文。
本例中,就去载入 webmvc-servlet.xml。
(既然有默认,肯定能够自己定义。之后的文章会介绍到)
以下我们匹配该servlet处理的URL,一般推荐直接使用 / 来匹配。
<span style="font-size:18px;"> <servlet-mapping>
<servlet-name>webmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping></span>
这时,你得考虑一个问题。不能使DispatcherServlet处理静态资源(css,js等)。Spring 3.0.4之后,能够使用<mvc:resources/>解决这一困扰。
尽管我们如今的Web项目还没有资源。有备无患,在和WEB-INF平行的文件夹下新建一个resources文件夹。
然后在webapp/WEB-INF下编辑webmvc-servlet.xml。
<span style="font-size:18px;"><?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:resources mapping="/resources/**"
location="/resources/" />
</beans></span>
mapping的值 /resources/** 表明匹配以/resouces開始的随意子路径,location指出资源实际文件夹。
上面提到,DispatcherServlet会选择合适的控制器去分派请求,这个过程是控制器映射去完毕的(细节能够去 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html了解)。这里仅仅使用Spring MVC的注解功能,
<span style="font-size:18px;"><mvc:annotation-driven /></span>
一句话就够了。
第四步。编写控制器
在和webapp平行的文件夹下,新建java文件夹,
└── src
└── main
└── java
└── webapp
包名 package org.webmvc.controller;
@Controller
public class HomeController{ @RequestMapping("/home")
public String showHomePage(Model model){ model.addAttribute("name","spring-mvc");
return "home";
} }
为实现自己主动检測和装配,我们在配置文件加上:
<context:component-scan base-package="org.webmvc.controller" />
以下的重点来了。就是逻辑视图如何和物理视图映射的问题。本例中。home这个字符串如何和一个物理文件发生关联?
第五步,视图解析
因为这里讲的是Thymeleaf和Spring的整合,先把thymeleaf-spring4-{version}.jar 放在Build Path以下,或加入以下的依赖:
(spring4表明是和spring4.0+整合,也有spring3)。
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
首先。要实例化一个 org.thymeleaf.spring4.SpringTemplateEngine的模板引擎,
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean> <bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
而典型的JSP + JSTL的视图,一个典型的配置是:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsps/" />
<property name="suffix" value=".jsp" />
<property name="order" value="2" />
<property name="viewNames" value="*jsp" />
</bean>
当中InternalResourceViewResolver是org.springframework.web.servlet.ViewResolver的一个实现,而在Thymeleaf这个工作是由org.thymeleaf.spring4.view.ThymeleafViewResolver完毕,
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*.html,*.xhtml" />
</bean>
完整的一个配置例如以下,
<!-- **************************************************************** -->
<!-- THYMELEAF-SPECIFIC ARTIFACTS -->
<!-- TemplateResolver <- TemplateEngine <- ViewResolver -->
<!-- **************************************************************** --> <bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</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>
在WEB-INF下新建views文件夹,并新建home.html,内容非常easy:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring MVC with thymeleaf</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + ' with thymeleaf !'" /> <br/>
run ok....
</body>
</html>
当中。xmlns:th="http://www.thymeleaf.org"是引入命名空间。也就th标签的使用。
到此,一个完整的MVC app 创建成功。
执行 mvn tomcat7:run。在浏览器输入 http://localhost:8080/spring-mvc/home,
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmxvdmVpdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
Spring MVC : Java模板引擎 Thymeleaf (二)的更多相关文章
- 新一代Java模板引擎Thymeleaf
新一代Java模板引擎Thymeleaf-spring,thymeleaf,springboot,java 相关文章-天码营 https://www.tianmaying.com/tutorial/u ...
- springboot:Java模板引擎Thymeleaf介绍
Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎.类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用 ...
- SpringBoot系列:Spring Boot使用模板引擎Thymeleaf
一.Java模板引擎 模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 在jav ...
- SpringBoot入门:新一代Java模板引擎Thymeleaf(理论)
Spring Boot 提供了spring-boot-starter-web来为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及SpringMVC的 ...
- Spring Boot整合模板引擎thymeleaf
项目结构 引入依赖pom.xml <!-- 引入 thymeleaf 模板依赖 --> <dependency> <groupId>org.springframew ...
- Spring MVC : Java模板引擎 Thymeleaf (三)
以下以构造一个表单開始,解说 Thymeleaf的使用方法. 为了演示方便,还是以经典的注冊为例. 这是Thymeleaf的form的形式, <form action="#" ...
- 转--Spring MVC : Java模板引擎 Thymeleaf (三)
原文:http://www.csdn.com/html/topnews201408/49/1349.htm 下面以构造一个表单开始,讲解 Thymeleaf的用法.为了演示方便,还是以经典的注册为例. ...
- SpringBoot入门:新一代Java模板引擎Thymeleaf(实践)
菜鸟教程:http://www.runoob.com/ http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js http://apps.b ...
- Spring Boot (四)模板引擎Thymeleaf集成
一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供XHTM ...
随机推荐
- dbcp数据源配置
<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource" de ...
- java.lang.RuntimeException: java.lang.NullPointerException...的错误
先FQ,让电脑能登上谷歌,然后重新安装,应该就好了,我的是这样解决的.如果下次安装又报:java.lang.RuntimeException: java.lang.NullPointerExcepti ...
- Filesystem Hierarchy Standard (Unix, Linux etc)
http://www.pathname.com/fhs/ /boot -- Static files of the boot loader Purpose: contains everything r ...
- POJ_2195_Going Home
题意:用'H','m','.'作出矩阵,'H'代表房子,'m'代表人,人一次只能水平或者垂直移动到相邻的点,问所有人一共走的步数的最小值. 分析:明显的求二分图最大权匹配.KM算法求得的是最大权匹配, ...
- 使用CocoaPods,文档中出现引用头文件找不到的问题。
现在很多人都会使用CocoaPods来管理自己使用的第三方开源代码,这两天我的工程中碰到了这样一个问题,当我使用CocoaPods来进行三方源码的引入,但是在实际的工程当中引入出现了这样一个问题,就是 ...
- Vue的特性
1.数据驱动视图 <div id="app"> <p> {{ message }}<p> </div> var app = new ...
- 浅谈GFC
Web页面的布局,我们常见的主要有“浮动布局(float)”.“定位布局(position)”.“行内块布局(inline-block)”.“CSS3的多栏布局(Columns)”.“伸缩布局(Fle ...
- rename命令中正则表达式的使用
rename命令用字符串替换的方式批量改变文件名. 格式如下: rename 原字符串 目标字符串 文件(列表) 原字符串:将文件名需要替换的字符串: 目标字符串:将文件名中含有的原字符替换成目标 ...
- for循环,字典遍历(一)
#items(): 返回字典中所有 key.value #keys(): 返回字典中所有 key 的列表 #values():返回字典中所有 value 的列表 my_dict = {'语文':89, ...
- 洛谷——P2158 [SDOI2008]仪仗队
P2158 [SDOI2008]仪仗队 找规律大水题嘛,如果你做过P1170 兔八哥与猎人 这题得到的规律是$a,b,c,d$,若$gcd(a-b,c-d)==1$ 那么$a,b$就能看到$c,d$ ...