user321068
user321068

Reputation:

Inserting Struts Tiles attribute into a JavaScript function

I'm trying to insert a Struts Tiles attribute into a JavaScript function. The JS function is called on load and certain JSP pages should be allowed to append additional code to that function.

The template looks like this:

<html>
<head>
<script>
  function onLoad(){
    ...
    <tiles:insert attribute="html.head.onLoad" ignore="true" />
    ...
  }
</script>
</head>
</html>

And the JSP site that implements this template like this:

<html> 
<head> 
<script>
<tiles:put type="string" name="html.head.onLoad">
        document.getElementById("report").style.height = (getWindowHeight() - 200) + "px";
    </tiles:put>
...
</script>
</head>
</html>

Unfortunately, the attribute is inserted not into the JS function, but somewhere outside in the HTML head.

Do you know a way how I can achieve my goal?

Upvotes: 0

Views: 2521

Answers (1)

josemaria_liso
josemaria_liso

Reputation: 121

I managed this just inserting the attribute name within the JavaScript code between "". In my case (for example), i have:

Template (ie: filterTemplate.jsp):

<div><a class="btn" id="btn1"><i class="fa fa-filter"></i></a></div>

<div>...</div>

<div id="<tiles:insertAttribute name='myDiv'/>"/>
<script>
    $('#btn1').click(function({
        $('html, body').anímate({
            scrollTop: $("<tiles:insertAttribute name='myDiv'/>").offset().top
        }, 2000);
    });
</script>

In the tiles.xml:

<definition name="resultTable" template="filterTemplate.jsp">
    <put-attribute name="myDiv" value="" />
</definition>

Extending the definition:

<definition name="resultTableA" extends="resultTable">
    <put-attribute name="myDiv" value="#listDogs" />
</definition>
<definition name="resultTableB" extends="resultTable">
    <put-attribute name="myDiv" value="#listCats" />
</definition>

Finally, in other jsp:

<div class="panel-body collapse">
    <tiles:insertAttribute name="resultTableA"/>
</div>

Using apache tiles 2.0.6

Upvotes: 1

Related Questions