I'm using Spring 3 mvc and Apache Tiles 3.0.
I don't know how to insert some data in *jsp page which I will later use for insert in template tiles *.jsp page.
Tiles defiontions file looks like this:
<tiles-definitions>
<definition name="base.definition" template="/WEB-INF/views/layout.jsp">
<put-attribute name="title" value="" />
<put-attribute name="banner" value="/WEB-INF/views/banner.jsp" />
<put-attribute name="path" value="" />
<put-attribute name="menu" value="/WEB-INF/views/menu.jsp" />
<put-attribute name="data" value="" />
<put-attribute name="position" value="" />
</definition>
<definition name="/" extends="base.definition">
<put-attribute name="title" value="Home" />
<put-attribute name="path" value="/WEB-INF/views/path.jsp" />
<put-attribute name="data" value="/WEB-INF/views/data.jsp" />
<put-attribute name="position" value="/WEB-INF/views/position.jsp" />
</definition>
</tiles-definitions>
I want to populate "/WEB-INF/views/data.jsp" page with some data, for example - rows form database, and after that to use it like part of template page (in this case it's layout page).
Layout.jsp page looks like this:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><tiles:insertAttribute name="title" /></title>
<link rel="stylesheet" href="<spring:theme code='body' />" type="text/css" />
<link rel="stylesheet" href="<spring:theme code='page' />" type="text/css" />
<link rel="stylesheet" href="<spring:theme code='banner' />" type="text/css" />
<link rel="stylesheet" href="<spring:theme code='path' />" type="text/css" />
<link rel="stylesheet" href="<spring:theme code='main' />" type="text/css" />
<link rel="stylesheet" href="<spring:theme code='menu' />" type="text/css" />
<link rel="stylesheet" href="<spring:theme code='source' />" type="text/css" />
<link rel="stylesheet" href="<spring:theme code='position' />" type="text/css" />
<link rel="stylesheet" href="<spring:theme code='data' />" type="text/css" />
</head>
<body>
<div id="page">
<div id="banner">
<tiles:insertAttribute name="banner" />
</div>
<div id="path">
<tiles:insertAttribute name="path" />
</div>
<div id="main">
<span id="menu">
<tiles:insertAttribute name="menu" />
</span>
<span id="source">
<span id="position">
<tiles:insertAttribute name="position" />
</span><br/>
<span id="data">
<span id="dataSource">
<tiles:insertAttribute name="data" />
</span>
<span id="dataSource">
<tiles:insertAttribute name="data" />
</span>
<span id="dataSource">
<tiles:insertAttribute name="data" />
</span>
</span>
</span>
</div>
</div>
</body>
</html>
If I put <tiles:insertAttribute name="rowsFromDb"
in "/WEB-INF/views/data.jsp"
I've gotten an error:
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.tiles.template.NoSuchAttributeException: Attribute 'rowsFromDb' not found.
I don't know to how to send data to this attribute 'rowsFromDb'! I only know how to send data to variables that are on the layout page (template - parrent)
Founded solution. Thanks all for helped me!
Code in controller:
...
Code in *.jsp page
Conclusion: data for page that isn't a template it need to send from controller and on the target page use 'jsp' or 'jstl' or ...
If you're intending to hold a list of records in the attribute
rowsFromDb
, then you should populate this in your controller and set the list in the model. Then indata.jsp
you can iterate this list using the<c:forEach>
tag. For example (a madeup example to illustrate the point):Controller
data.jsp
You should avoid doing any database query directly in the JSP itself (and avoid any Java in the JSP where possible). The JSP should just deal with rendering the data it is given by the controller.
You can use Tiles
org.apache.tiles.preparer.ViewPreparer
for this purpose.Create a new class that implements
org.apache.tiles.preparer.ViewPreparer
in your source folder. For example:In your Tiles definition, you must add
preparer
attribute in thedefinition
, such as: