Struts2+Mybatis+Freemarker+Tiles架构灵活的开发框架(二)

接着上面继续,这次我们来配置tiles

第一,导入包

struts2-tiles-plugin-2.3.7.jar

commons-digester-2.0.jar

commons-beanutils-1.8.0.jar

tiles-api-2.1.2.jar

tiles-compat-2.1.2.jar

tiles-core-2.1.2.jar

tiles-jsp-2.1.2.jar

tiles-servlet-2.1.2.jar

第二,修改web.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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">
	<!-- Tiles配置 -->
	<context-param>
		<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
		<param-value>/WEB-INF/tiles.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
	</listener>
	<!-- Struts2配置 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

第三,建立一个框架文件_layout/baseLayout.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>XXX管理系统</title>
</head>
<body>
	<tiles:insertAttribute name="body" />
</body>
</html>

第四,建立html文件_template/TEST.html

tiles!

第五,修改Struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<package name="basic" extends="struts-default">
		<result-types>
			<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
		</result-types>
		<action name="index" class="com.XXX.lib.action.IndexAction" method="index">
			<result name="success" type="tiles">index</result>
		</action>		
	</package>
</struts>

第六,在webinf下建立tiles.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="/_layout/baseLayout.jsp">
        <put-attribute name="body" value="/_template/TEST.html" />
    </definition>    
</tiles-definitions>

第七,测试http://localhost:8080/xxxx/index.action,输出tiles,ok

特别说明:不明白请在下面跟帖,有错误的地方麻烦指正

相关推荐