springMVC框架集成tiles模板
将tiles模板集成到springMVC框架下,大概流程如下:
1.在配置文件中加入tiles支持
我的servlet配置文件名为spring-mvc.xml.具体配置如下:
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
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-4.1.xsd"> <!-- 激活@controller模式 -->
<mvc:annotation-driven />
<!-- 配置包扫描位置 -->
<context:component-scan base-package="com.test.maven.controller" />
<!-- 配置视图解析器(jsp文件前缀后缀) -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- 配置tiles模板 -->
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/tiles-definitions.xml</value>
</list>
</property> </bean> </beans>
很简单,只是添加了模板配置文件路径。
2.配置tiles模板
我们需要给tiles添加一个默认的模板,将一些想要加入的模块加入进去
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="defaultTemplate" template="/WEB-INF/tiles/classic.jsp">
<put-attribute name="title" value="Hello world I'm title" />
<put-attribute name="menu" value="/WEB-INF/tiles/menu.jsp"/>
<put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp" />
</definition>
</tiles-definitions>
defaultTemplate就是默认模板的名字,template就是模板的路径
put-attribute表示加入模板的模块,value指向其路径
还可以再加入新的模板,如过想要继承某个模板,可以在definition处去掉template,添加extends(没试过)
注:在配置模板文件和调用模板时,都应该在文件头声明tiles标签库
模块文件就不赘述了,这里说一下模板文件,classic.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><tiles:getAsString name="title" /></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div class="page">
<tiles:insertAttribute name="menu" />
<div class="content">
<tiles:insertAttribute name="body" />
</div>
<tiles:insertAttribute name="footer" />
</div>
</body>
</html>
上边的body元素是模板没有的,就是所谓的正文内容,因为一般情况下每次调用body内的元素都不一样,所以无需在模板内指定,在模板文件中指明其位置,然后在调用的时候向里边填入内容就好了。
3.调用tiles模板
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<body>
<tiles:insertDefinition name="defaultTemplate">
<tiles:putAttribute name="body">hahahaha</tiles:putAttribute>
<tiles:putAttribute name="footer">hello</tiles:putAttribute>
</tiles:insertDefinition>
</body>
调用模板,首先是使用defaultTemplate模板,然后配置模板内的组件,若都使用默认,可以直接为空,如果重写了其中的组件,则会将原模板内的配置覆盖。但是如果是后加的组件,如上边的body,并没有默认的组件,则在调用时必须重写其内容。
到此,最简单的tiles配置就结束了。
insertDefinition
springMVC框架集成tiles模板的更多相关文章
- RabittMQ实践(二): RabbitMQ 与spring、springmvc框架集成
一.RabbitMQ简介 1.1.rabbitMQ的优点(适用范围)1. 基于erlang语言开发具有高可用高并发的优点,适合集群服务器.2. 健壮.稳定.易用.跨平台.支持多种语言.文档齐全.3. ...
- SpringMVC框架——集成RESTful架构
REST:Representational State Transfer 资源表现层状态转换 Resources 资源 Representation 资源表现层 State Transfer 状态转换 ...
- 易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试
Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试. 它是一个典型的教科书式的mvc ...
- SpringMVC 集成 Velocity 模板引擎
本文通过 maven 项目中集成 1.引入 SpringMVC 与 Velocity 需要的依赖 <!-- SpringMVC --> <dependency> <gro ...
- Spring+SpringMvc+Mybatis框架集成搭建教程
一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...
- Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试。
Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试. 它是一个典型的教科书式的mvc ...
- SpringMVC 集成 Freemarker 模板引擎
本文通过 maven 项目中集成 1.引入 SpringMVC 与 Freemarker 需要的依赖 <!-- SpringMVC --> <dependency> <g ...
- Spring+SpringMVC+Mybatis(SSM)框架集成搭建
Spring+SpringMVC+Mybatis框架集成搭建教程 一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以 ...
- myBatis,Spring,SpringMVC三大框架ssm整合模板
整合步骤 创建web工程 导入整合所需的所有jar包 编写各层需要的配置文件 1) mybatis的全局配置文件 <configuration> <!-- 批量别名的设置 -- ...
随机推荐
- Futures
Futures is a framework for expressing asynchronous code in C++ using the Promise/Future pattern. Ove ...
- 存储过程与SQL语句怎么选择
应用存储过程的优点:1.具有更好的性能存储过程是预编译的,只在创建时进行编译,以后每次执行存储过程都不需再重新编译,而一般 SQL 语句每执行一次就编译一次,因此使用存储过程可以提高数据库执行速度.2 ...
- OD 实验(十六) - 从对话框入手对程序的逆向
对话框: 对话框从类型上分为两类:modal 对话框和 modeless 对话框,就是模态对话框和非模态对话框,也有叫成模式和非模式 模态对话框不允许用户在不同窗口间进行切换,非模态对话框允许用户在不 ...
- OD 实验(一) - 修改程序标题
需要修改的程序 把 I love fishc.com 修改为 hello world sch01ar 用 OD 打开程序 在程序入口处开始一直按 F8 运行程序,看看在哪里弹出对话框 运行到该地址的时 ...
- 语义分析:C语言表达式的语法树生成——Python实现
令狐冲慢慢走近,那汉子全身发抖,双膝一屈,跪倒在雪地之中.令狐冲怒道:“你辱我师妹,须饶你不得.”长剑指在他咽喉之上,心念一动,走近一步,低声问道:“写在雪人上的,是些什么字?” 那汉子颤声道 ...
- iPhone开发随想:rand()还是arc4random()
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://bj007.blog.51cto.com/1701577/544006 今天在iP ...
- js中function的与众不同
js中function的与众不同在于可以被调用
- ElasticSearch的java api
pom <dependencies> <dependency> <groupId>org.elasticsearch.client</groupId> ...
- git的突出解决--git rebase之abort、continue、skip
(1)应用实例描述 假设在github或者gitoschina上建立了一个项目,默认分支为master分支,远程master分支上c.sh文件内容: 开发者A.B分别将项目拷贝到自己本地进行开发 某一 ...
- Flying Right POJ - 3038
有一条从南到北的航线,航线上有N个机场1-n从南到北分布,每天早上飞机从1飞到n,傍晚从n飞到1.有k组乘客,他们数量为M[k],从S飞到E,飞机上只有C个座位,计算每天飞机最多能拉多少乘客 贪心可以 ...