Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to re-specify taglib annotation in my Tiles child page fragments? (The are already specified in my masterlayout.jsp)

Sorry I dont know how to phrase this question any better.

Currently, I am specifying the spring mvc "form" taglib annotation (and a few others) in my main layout.

I would expect specifying these annotations in this location would eliminate the need to duplicate the same annotations in the other page fragments that comprise this tile definition.

But, it "appears", that my "body" fragment appears to work properly only if I re-specify the annotations there, as well.

    e.g., 
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    -
    -
    -

the tiles definition for "mypage" (based on "masterpage") looks like this

                 -
                 -
                 -
    <definition name="masterpage" template="/WEB-INF/views/masterlayout.jsp">
        <put-attribute name="title" value="" type="string"/>
        <put-attribute name="header" value="" />
        <put-attribute name="leftside" value="" />
        <put-attribute name="rightside" value="" />
        <put-attribute name="footer" value="" />
    </definition>   

    <definition name="mypage" extends="masterpage">
        <put-attribute name="title" value="My Page Title" type="string"/>
        <put-attribute name="header" value="/WEB-INF/views/header.jsp" />  
        <put-attribute name="leftside" value="/WEB-INF/views/leftside.jsp" />        
        <put-attribute name="rightside" value="/WEB-INF/views/rightside.jsp"/>
        <put-attribute name="footer" value="/WEB-INF/views/footer.jsp" />                        
    </definition>
                 -
                 -
                 -

here is what the masterlayout.jsp looks like

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>blah blah blah</title>
            <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/resources/mypage.css" /> 
        </head>
        <body>
            <div>
                <div>
                    <div id="headerdiv">
                        <tiles:insertAttribute name="header" />
                    </div>
                    <div id="middle">
                        <div>
                            <tiles:insertAttribute name="leftside" />
                            <tiles:insertAttribute name="rightside" />
                        </div>
                    </div>
                    <div id="footerdiv">
                        <tiles:insertAttribute name="footer" />
                    </div>
                </div>
            </div>
        </body>
    </html>

Here is the rightside.jsp page fragment - where I currently have to re-specifying taglibs (without these, the page does not work properly)

    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

    <div style="min-height: 550px;">
        <div>
            <form:form id="form1" modelAttribute="myViewBean" method="post" action="saveForm.html">
                <form:errors path="errorMsg" cssClass="error" element="div" />                   
                <div class="clear"></div>

                <div>
                    <div class="label">
                        <form:label path="email">Email:<em>*</em></form:label>
                    </div>
                    <div>
                        <form:input path="email" size="40" maxlength="256" />
                        <form:errors path="email" cssClass="error" />   
                    </div>
                    <div class="clear"></div>
                </div>
            </form:form>
        </div>
    </div>
like image 396
sairn Avatar asked Dec 10 '25 10:12

sairn


1 Answers

Because each JSP is independant from the others. Tiles uses dynamic includes behind the scenes.

And the whole point of a templating engine like Tiles is to be able to use the same components inside multiple layouts, and multiple components inside the same layout. Having a component's code depend on the layout it's included in is not a good idea.

like image 87
JB Nizet Avatar answered Dec 11 '25 23:12

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!