How can a JSP fragment include its dependencies into the parent's head tag? -


I have a JSP file called head.jsp which looks like this.

  & lt; % @ Taglib prefix = "c" yur = "/ c"%> & Lt;% @ taglib prefix = "Fn" yurry = "http://java.sun.com/jsp/jstl/functions"% & gt; & Lt ;! DOCTYPE html & gt; & Lt; Html & gt; & Lt; Top & gt; ... & lt; / Head & gt; & Lt; Body & gt;   

Then I have a content section that looks like this.

  & lt; Jsp: include page = "../ header.jsp" /> My page content here   

If there are some dependencies like CSS and Javascript in that piece of content, in which the head tag is to go inside, how can I include them there? Each piece can have its own set of CSS and Javascript files, which are different, and I do not want to include every possible CSS and Javascript file, so that I can use it in the header for every single page. What is the right way to handle it? I am a dumb intern at startup, so forgive my ignorance. Thank you in advance.

It's easy, you can & lt; Using jsp, you can: & lt; Include / & gt; Tag instead of the tag / & gt; Tag This is new to JSP 2.0 and allows you to move some pieces from one page to another, which makes the task of creating templates much easier. You can check out a tutorial.

And this is the way to solve your problem: Create a tag folder and put it in it.

template.tag

  & lt;% @ tag description = "main template of page" pageConnecting = "UTF-8" trim directive webspace = "true"% & Gt; & Lt;% @ attribute name = "header" piece = "true" required = "wrong"%> & Lt; Html & gt; & Lt; Top & gt; ... Some CSS and JavaScript used on all pages ... & lt ;! - Javascript for custom CSS and one page - & gt; & Lt; Jsp: invitation piece = "header" /> & Lt; / Head & gt; & Lt; Body & gt; & Lt; Jsp: doBody / & gt; & Lt; / Body & gt; & Lt; / Html & gt; Use the previous template on your page by including the   

tag folder:

useOfTemplate.jsp

  & lt;% @ Page content type = "text / html" page encoding = "UTF-8"%> & Lt;% @ taglib prefix = "custom" tagdir = "/ web-ANF / tag"% " & Lt; Custom: Templates & gt; & Lt; Jsp: attribute name = "header" & gt; ... Here you can add your custom CSS and JavaScript for this page ... & lt; / Jsp: attribute & gt; & Lt; Jsp: body & gt; ... here comes the information of the page ... & lt; / Jsp: body & gt; & Lt; / Custom: Templates & gt;    

Comments