I have common css and js files that i include in every jsp file.
So what's the best practice to include them in every page ?
I used to use <%@ include file="header.jsp" %>
but I'm wondering if this is the best and clean way to proceed.
I like using fragments for this, they are standard supported by JSP so no other dependencies are needed. And as you will see it will comes with lot's of other benefits.
Create a tag (parentpage.tag):
<%@tag description="Base page" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@attribute name="extra_css" fragment="true" %>
<%@attribute name="footer" fragment="true" %>
<%@attribute name="header" fragment="true" %>
<html>
<head> ....
// insert css that is needed for every page
<jsp:invoke fragment="extra_css"/>
<jsp:invoke fragment="header"/>
<jsp:doBody/>
<jsp:invoke fragment="footer"/>
Then you create individual pages that inherit from this tag
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:parentpage>
<jsp:attribute name="extra_css">
// custom css for this page
</jsp:attribute>
<jsp:attribute name="footer">
// footer content
</jsp:attribute>
<jsp:body>
// body content
</jsp:body>
</t:parentpage>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With