简体   繁体   中英

How to include a javascript in a jsp page

I am new to J2EE and Web development. Here is my issue: I want to include angular.js in the webpage.

Here is the version that works:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <script src="http://code.angularjs.org/1.2.1/angular.min.js"></script>
    <title>Lets Meet Up!</title>
</head>

But I also want to have some local javascript files, and hopefully, I want to import the angularjs in the local directory

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <script src="WEB-INF/lib/angular/angular.js"></script>
    <title>Lets Meet Up!</title>
</head>

The the page is unable to run the javascript.

My stack is Tomcat, Spring MVC, Spring, J2EE I put the angularjs source in AppRoot/WebContents/WEB-INF/lib/angular

Anyone can help? THanks!!

Files in WEB-INF are inaccessible.

Put angular.min.js and all other static assets at the top level of your war file. Or better yet in some nice folder like js/angular/angular.min.js at the top level.

Then do this

<script src="js/angular/angular.min.js"></script>

Depending on your needs you might want to consider serving your assets from a CDN, but that's another story altogether.

Don't put them into WEB-INF, that folder is for the stuff that you do not want to have directly served.

Put it next to WEB-INF

+- WEB-INF

+- static


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM