Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot add HTML and JavaScript

I'm trying to run Spring boot app with some HTML5 and JavaScript and I have a problem.

I have project structure like this:

enter image description here

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:

enter image description here

I have no idea why my app doesn't see this script files, could any one have any idea what i did wrong?

like image 842
Piotr Żak Avatar asked Nov 25 '15 20:11

Piotr Żak


People also ask

Can we use JavaScript in spring boot?

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.

How do you add JavaScript to HTML?

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.

What are the steps to add a custom js code with spring boot?

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.


3 Answers

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: enter image description here

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

like image 191
Eric Avatar answered Sep 19 '22 13:09

Eric


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

like image 26
Chris Avatar answered Sep 22 '22 13:09

Chris


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

like image 21
jny Avatar answered Sep 20 '22 13:09

jny