I'm trying to run Spring boot app with some HTML5 and JavaScript and I have a problem.
I have project structure like this:
My Spring MVC Controller is calling file offer.html and that works ok.
My offer.html file look like this:
<!DOCTYPE html>
<html lang="en" ng-app="coByTu">
<head>
<title>Page</title>
<script type="text/javascript" src="../js/lib/angular.js" />
<script type="text/javascript" src="../js/src/offer.js" />
</head>
<body>
</body>
</html>
And when I'm typing my app URL http://localhost:8080/offerView
response from server is:
I have no idea why my app doesn't see this script files, could any one have any idea what i did wrong?
Spring Boot - Integrating Static Content - Javascript (JS) and CSS files. This guide will help you create a simple web application with Spring Boot. We will add static content (css and js) and use it from a JSP view.
You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.
Create a folder called static under resources folder and put your static content in that folder. For your example the path to myStatic. js would be resources\static\js\myStatic.
Basically all content that needs to be served staticly (such as javascript files) should be placed under the static folder. https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
I've thrown together a quick working example to show how it is done: https://github.com/ericbv/staticContentWithSpringBoot
File structure:
HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page</title>
<script type="text/javascript" th:src="@{/js/lib/angular.js}" />
<script type="text/javascript" th:src="@{/js/src/offer.js}" />
</head>
<body>
Using th:src will make sure that the links are context aware
Edit: added the th:src to make the references context aware
just for anyone to find this page with the same issue. The way I resolved the 'script is not executed' problem was pretty simple.
Simply replaced :
<script type="text/javascript" src="js/src/Test.js" />
with
<script type="text/javascript" src="js/src/Test.js" ></script>
(Test is located in 'static/js/src') Hopefully this is helpful to anyone but me :)
cheers
You need to put your static js files into static folder. See more here: https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
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