user1088014 user1088014 - 3 months ago 12
CSS Question

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?

Answer

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>