dutchlab
dutchlab

Reputation: 590

Loading html head tag common files in jsp

I am new to Java and I have set up a Maven/Spring 4 environment in NetBeans 8.

In PHP and ColdFusion I would create one file that loads the css and javascript files and include that file in all views in order to make one point of change.

I would like to do that in Java but I am not sure what the best practice would be. I have the files in the resources directory under WEB-INF and in the css and js folders respectively.

Should I create a jsp file that has the link and script paths and include the jsp file in others, use an xml file, combination of both or something else?

Upvotes: 1

Views: 2826

Answers (2)

Bibek Shakya
Bibek Shakya

Reputation: 1273

For Easy to learn and run Templating the component in Jsp, like header body and footer when you are developing page only body of page change then use apache tile. It is xml configuration template composition framework.

Here is snippet of tile.xml

<definition name="base.definitions" template="/WEB-INF/page/layout.jsp">
    <put-attribute name="title" value=""/>
    <put-attribute name="header" value="/WEB-INF/page/include/header.jsp"/>
    <put-attribute name="menu" value="/WEB-INF/page/include/menu.jsp"/>
    <put-attribute name="body" value=""/>
    <put-attribute name="footer" value="/WEB-INF/page/include/footer.jsp"/>
</definition>

<definition name="login" template="/WEB-INF/page/loginlayout.jsp">
    <put-attribute name="title" value=""/>
    <put-attribute name="body" value=""/>
    <put-attribute name="footer" value="/WEB-INF/page/include/footer.jsp"/>
</definition>

Should I create a jsp file that has the link and script paths and include the jsp file in others

for this you create header.jsp and include all the js and css

Apache Tiles will render it all for you.

if you dont have that many of jsp pages then use <jsp:include page="header.jsp" /> in your content page.

Upvotes: 0

Der
Der

Reputation: 757

You have two options.

Static includes

Static includes are the equivalent to copy-pasting code into the page you're calling it from. This means that a static include will be executed in the current page context.

<%@include file="page.html"%>

Dynamic includes

Dynamic includes are requests evaluated in their own context, and then output to the page you're calling it from

<jsp:include page="page.html" />

Since the dynamic include is a request, you can send parameters like this

<jsp:include page="page.jsp">
  <jsp:param name="myVar" value="${someValue}"/>
</jsp:include>

Upvotes: 2

Related Questions