5

I am using tiles in struts2 application. While defining base layout I have defined an attribute "scriptFile".

<definition name="baseLayout" template="/application/base-layout.jsp" >
    ... 
</definition>

<definition name="custom.tiles" extends="baseLayout">
    <put-attribute name="scriptFile" value="js/custom-script.js"></put-attribute>
</definition>

If developer provides "scriptFile" in tiles definition file, "tiles.xml", this script file should be included using following line

<script language="javascript" src="<tiles:insertAttribute name="scriptFile"></tiles:insertAttribute>"></script>

But if scriptFile attribute is not defined, this line must be skipped.

How can I check the existence of "scriptFile" attribute in tiles. Is there any better way to do this thing?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bilal Mirza
  • 2,576
  • 4
  • 30
  • 57
  • What version of tiles are you using? Where does scriptFile attribute come from? Is it something that is dynamic at run time? If this is just tiles defintions you should create several "head" templates, and include the appropriate header, if you use a head that requires you to provide a scriptFile then you will need to provide a script file. If a struts2 action requires an id to do it's job you can't very well not send an id and expect it to work (same for tiles). If this is dynamic content there are other possibilities. – Quaternion Nov 13 '12 at 06:39
  • version = tiles 2.0.6. attribute comes from tiles.xml, and it is not dynamic. Layout consists of Page Heading, Menu, Contents and Footer. only contents will be changed for every page. If contents require any javascript, developer will provide that JS file and define it in tiles.xml. This javascript file will then be included in page header. – Bilal Mirza Nov 13 '12 at 07:11

4 Answers4

3

I tried

<t:useAttribute name="scriptFile" id="script" classname="java.util.List" ignore="true"/>
<%
    if(script != null) {
        ... include script
    } 
%>

JSTL can also be used here.

Bilal Mirza
  • 2,576
  • 4
  • 30
  • 57
  • http://mail-archives.apache.org/mod_mbox/struts-user/200304.mbox/%3C41669DC6FE3B80449A33A4DD46DB370A0224F065@Entcoexch15%3E – Bilal Mirza Nov 13 '12 at 10:41
1

I was able to solve a similar problem by using the defaultValue attribute. This way, you won't get an exception if the attribute doesn't exist. In this case, if the page doesn't need to include extra JavaScript then the tiles template will just add an empty string

JSP

<tiles:insertDefinition name="portalTemplate">

    <tiles:putAttribute name="body">
        <h1>Body</h1>
    </tiles:putAttribute>  

    <tiles:putAttribute name="js">
        <script src="script.js"></script>
    </tiles:putAttribute>   
</tiles:insertDefinition>

Tiles Template JSP

<html>
<body>
   <tiles:insertAttribute name="body"/>
   <script src="/public/libs/jquery/jquery-2.1.1.min.js"></script>

   <!-- Include extra JS after JQuery -->
   <tiles:insertAttribute name="js" defaultValue=""/>
</body>
</html>
Ian
  • 7,480
  • 2
  • 47
  • 51
0

Just use ignore attribute

<script language="javascript" src="<tiles:insertAttribute name='scriptFile'  ignore='true'/>"></script>

and do not put scriptFile in your definition.

Update

How about using getAsString tag in layout then

<tiles:getAsString name="jscript" ignore="true" />

and in definition put the whole escaped script tag.

<put-attribute name="jscript" value="&lt;script src='js/custom-script.js' type='text/javascript'>&lt;/script>" />
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • ignore attribute just skips placing the value of attribute if it is not present. In this case it surrounding `. – Bilal Mirza Nov 13 '12 at 10:02
  • It may work but I think it is not a good idea to provide complete tag as a string each time instead of defining just file name. If file name is given, surrounding script tag should be placed by the template reduces the effort and chances of error. – Bilal Mirza Nov 13 '12 at 10:29
0

I used the following code to solve that problem (just according to the info I got from tags description: ignore - If this attribute is set to true, and the attribute specified by the name does not exist, simply return without writing anything. The default value is false, which will cause a runtime exception to be thrown. ):

<tiles:importAttribute name="stylesheets" ignore="${true}"/>

P.S. Be careful, use ${true} intead of only "true" value.