在Spring和Struts2项目中整合Tiles解决方案

Tiles是一种layout的概念,可以将一个页面拆分成多个部分。比如:title,header,body,bottom等等。在一个项目中,这些部分有可能是相同的。这时使用Tiles配置相同的部分再好不过了。下面是配置Tiles的过程:

1,在项目中引入tiles依赖的包,比如:

tiles-api-2.0.6.jar,tiles-jsp-2.0.6.jar,struts2-tiles-plugin-2.2.3.jar,tiles-core-2.0.6.jar等等。

包下载地址:http://www.javaxxz.com/file.php?id=15561145

2,创建一个模板页面,抽取出公共的部分。比如:

<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%>

//引入tiles标签
<%@ taglib prefix=”tiles” uri=”http://tiles.apache.org/tags-tiles” %>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<base href=”<%=basePath %>” />
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>

//公共部分title
<title><tiles:getAsString name=”title”/></title>
<!– 公共资源引入 –>
<tiles:insertAttribute name=”links” ignore=”true” />
</head>
<body>
<div data-role=”page”>

//公共部分head
<tiles:insertAttribute name=”head” />

//公共部分middle
<tiles:insertAttribute name=”middle”/>

//公共部分foot
<tiles:insertAttribute name=”foot” />
</div>
</body>
</html>
3,创建一个tiles配置文件main.xml配置如下:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE tiles-definitions PUBLIC “-//Apache Software Foundation//DTD Tiles Configuration
2.0//EN” “http://tiles.apache.org/dtds/tiles-config_2_0.dtd”>

<tiles-definitions>
<definition name=”index” template=”/pgapp/main/template.jsp”>
<put-attribute name=”title” value=”标题” />
<put-attribute name=”links” value=”/pgapp/main/links.jsp”/>
<put-attribute name=”head” value=”/pgapp/main/head.jsp”/>
<put-attribute name=”middle” value=”/pgapp/main/index.jsp”/>
<put-attribute name=”foot” value=”/pgapp/main/foot.jsp” />
</definition>
</tiles-definitions>

文件中配置了模板文件中,各个部分所对应的内容页面。

4,在web.xml中配置加载tiles中的main.xml文件

<context-param>
<param-name>org.apache.tiles.CONTAINER_FACTORY</param-name>
<param-value>
org.apache.struts2.tiles.StrutsTilesContainerFactory
</param-value>
</context-param>
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>
/WEB-INF/tiles/main.xml
</param-value>
</context-param>

5,在struts.xml文件中配置。

<!– struts2配置tiles–>
<package name=”cms” namespace=”/cms” extends=”tiles-default” >
<!– 导航配置 –>
<action name=”*” class=”xxxAction” method=”{1}”>
<result type=”tiles”>${targetUrl}</result>
<result name=”form_submit”  type=”redirectAction”>${targetUrl}</result>
</action>
</package>

配置中的${targetUrl}需要在xxxAction中返回才行。

6,在action中返回targetUrl。

在action中创建一个属性targetUrl,并设置其setter和getter方法。然后在return SUCCESS;前设置如:setTargetUrl(“index”);则struts2就知道去main.xml文件中找name=”index”的配置,并根据模板页封装好整个页面。

更多:http://www.javaxxz.com/thread-67444-1-1.html

转载于:https://www.cnblogs.com/javaxxz/archive/2013/01/15/2860779.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值