Include css and js file in every jsp page

前端 未结 5 1042
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 14:00

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 <%@

相关标签:
5条回答
  • 2021-01-03 14:32

    The best solutions is to use JSP Tag File. It allows to encapsulate reusable content using Tag Files : Java doc ref

    Here is a good answer with example : https://stackoverflow.com/a/3257426/1140748

    0 讨论(0)
  • 2021-01-03 14:32

    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>
    
    0 讨论(0)
  • 2021-01-03 14:40

    Yes! It can be a good option to have all the css and js defination in one jsp and then include that jsp in all pages/jsp.

    As you are using spring i would suggest you use tiles https://tiles.apache.org/ that is the much better way to make a standard layout and then use that layout in your all jsp.

    0 讨论(0)
  • 2021-01-03 14:42

    Make JavaScript and CSS External.

    Using external files in the real world generally produces faster pages because the JavaScript and CSS files are cached by the browser. JavaScript and CSS that are inlined in HTML documents get downloaded every time the HTML document is requested. This reduces the number of HTTP requests that are needed, but increases the size of the HTML document. On the other hand, if the JavaScript and CSS are in external files cached by the browser, the size of the HTML document is reduced without increasing the number of HTTP requests.

    Follow these links to get more info on this:

    "https://developer.yahoo.com/performance/rules.html"

    "include external java script file in jsp page "

    0 讨论(0)
  • 2021-01-03 14:44

    You can try this without any framework, in your web.xml:

    <jsp-config>
       <jsp-property-group>
         <display-name>Display</display-name>
         <url-pattern>*.jsp</url-pattern>
         <el-ignored>false</el-ignored>
         <scripting-invalid>false</scripting-invalid>
         <is-xml>false</is-xml>
         <include-prelude>/template/header.jsp</include-prelude><!-- header -->
         <include-coda>/template/footer.jsp</include-coda><!-- footer -->
       </jsp-property-group>
     </jsp-config>
    

    See more here

    0 讨论(0)
提交回复
热议问题