DaveCat
DaveCat

Reputation: 813

How to link Thymeleaf to external js file?

I have a page that's being served in a Spring Boot Application, but it's rendering the view through Thymeleaf. I initially wrote it in HTML and connected it to an external javascript file in order to allow the functionality of submitting data to the database. I wasn't able to get it to work, and I'm doing it in Thymeleaf. However, I keep getting a 404.

This is the documentation I'm working with:

http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html

This is the code I have in the actual page:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <script type="text/javascript" th:src="@{/js/main.js}"></script>
  <title>Stereoscopics</title>
</head>

This is the error I get:

Request URL:http://localhost:8080/js/main.js Request Method:GET Status Code:404 Not Found Remote Address:[::1]:8080 Referrer Policy:no-referrer-when-downgrade

Here's what I've tried

  1. Initially I built this page out in HTML. Just for the record it used to work, but I moved to using Thymeleaf and now it does. It used to look like this:
<script type="text/javascript" src="js/main.js"></script>
  1. I checked my directory structure. The javascript file is saved in:

    C:...\src\main\resources\static\js

  2. I consulted the documentation.

What am I doing wrong? The page needs to connect with javascript so that the user can submit data to the database.

Upvotes: 2

Views: 7946

Answers (1)

DaveCat
DaveCat

Reputation: 813

Fixed it.

For some reason, I have to manually go into Eclipse and refresh the package containing the static content. If you don't do this, it won't pick up on the changes.

Upvotes: 2

Related Questions