How to pass values ​​from JSP to tile attribute?

I am converting an existing Tiles 1 panel into a Tiles 2 architecture. I am having problems transferring values ​​from a JSP page to tile attributes.

Here is my tile definition file (tiles-definition.xml)

<tiles-definitions> <definition name="cda.layout" template="/jsp/layouts/layout.jsp"> <put-attribute name="pageTitle" value="StoryTitle" type="string"/> <put-attribute name="pageHeader" value="StoryHeader" type="string"/> <put-attribute name="resources" value="" type="string"/> </definition> </tiles-definitions> 

layout.jsp looks like this:

 <html> <head> <title><tiles:insertAttribute name="pageTitle" flush="true"/></title> </head> <body> ... ... <div class="content"> <h1><tiles:insertAttribute name="pageHeader" flush="true"/></h1> </div> ... ... </body> </html> 

I have a history page that uses a layout and should pass values ​​to the attributes of the template.

  <% // create a business object and populate String mytitle= story.getTitle(); String myheader = story.getHeader(); %> <tiles:insertTemplate template="../layouts/layout.jsp" flush="false" > <tiles:putAttribute name="pageTitle" value="${mytitle}"/> <tiles:putAttribute name="pageHeader"value="${myheader}"/> </tiles:insertTemplate> 

In story.jsp, I can System.out.print () the values ​​mytitle, myheader, and they show the correct one. But these values ​​are NOT passed to the tile attributes.

Any idea how to fix this?

+8
spring jsp tiles
source share
1 answer

${mytitle} is an JSP EL expression that means: find an attribute in the page area or request area or session area or application area called "mytitle".

By defining a scriptlet variable, you did not define an attribute in any of these areas. This will work if you have

 pageContext.setAttribute("mytitle", mytitle); 

But using scripts in JSP is bad practice. I don't know where your bean story comes from, but this is probably a request attribute. If so, you can define a new page area attribute this way using JSTL:

 <c:set var="mytitle" value="${story.title}"/> 

This is optional since you can use this expression directly in the tile tag:

 <tiles:putAttribute name="pageTitle" value="${story.title}"/> 

Learn more about JSP EL in this tutorial .

+17
source share

All Articles