Cómo hacer una plantilla en Struts 2 con tiles

index.jsp

<meta http-equiv="Refresh" content="0;url=homePage.action">
web.xml
<listener>
	<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
struts.xml<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="default" extends="struts-default">
		<result-types>
			<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
		</result-types>
		<action name="*Page">
			<result  type="tiles">{1}</result>
		</action>
	</package>
</struts>
tiles.xml (situado en la raíz de la carpeta WEB-INF)
<?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="baseLayout" template="/layouts/baseLayout.jsp">
                <put-attribute name="title" value="Template" />
                <put-attribute name="header" value="/templates/header.jsp" />
                <put-attribute name="menu" value="/templates/menu.jsp" />
                <put-attribute name="body" value="/templates/body.jsp" />
        </definition>
        <definition name="home" extends="baseLayout">
                <put-attribute name="title" value="Home Page" />
                <put-attribute name="body" value="/pages/home.jsp" />
        </definition>
        <definition name="contactUs" extends="baseLayout">
                <put-attribute name="title" value="Contact Us Page" />
                <put-attribute name="body" value="/pages/aboutUs.jsp" />
        </definition>
</tiles-definitions>

baselayout.jsp

<%@ 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>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title><tiles:insertAttribute name="title" ignore="true" /></title>
</head>
<body>
	<div style="width:100%; height:70px; background:salmon; float:left">
	<tiles:insertAttribute name="header" />
	</div>
	<div style="width:200px; height:500px; background:pink; float:left">
		<tiles:insertAttribute name="menu" />
	</div>
	<div style="width:800px; height:500px; background:green; float:left">
		<tiles:insertAttribute name="body" />
	</div>
</body>
</html>

Problemas de codificación usando tiles

Puede ser útil añadir el siguiente código al web.xml, en el caso de que tengamos problemas con los acentos, para corregir problemas u obtener información de ellos:

<web-app>
    ...
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <trim-directive-whitespaces>true</trim-directive-whitespaces>
        </jsp-property-group>
    </jsp-config>    
</web-app>
← Ejercicio futbol con Struts 2
Upload file (subir fichero) →

Aviso Legal | Política de privacidad