HTTP Status 404 - not found - Java Spring MVC - java

I'm trying to run my first Spring MVC Program on eclipse using XML configuration, I have no error markings on my directory but I get this response when I run the program. I also tried with Spring java configuration and got similar response.
bug
I use eclipse Version: 2021-06 (4.20.0) and apache-tomcat-10.0.13 installed on my device, sending a request at http://localhost:8080/ returns apache-tomcat successfully installed. So far I have tried all suggestions I found online including updating maven project, maven build-clean install, cleared file cache, deleted temp folder in my root director changed workspace directory, uninstalled and reinstall apache-tomcat multiple times, cleared missing files on source directory in java build path but none of this approaches have worked. Below is a screenshot of my program (XML configuration)
Directory
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.idevelope</groupId>
<artifactId>spring-mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Spring MVC</name>
<description>Spring MVC example</description>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>16</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
</project>
web.xml
dispatcher-servlet.xml
controller
user.java
(View Pages)
home.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC tutorial - Home JSP</title>
</head>
<body>
<h1>Hello World!</h1>
<h4>Message- </h4><span>${message}</span>
<form:form action="showUser" modelAttribute="user" method="post">
<tr>
<td>
<form:label path="firstName">First Name</form:label>
</td>
<td>
<form:input path="firstName" id="firstname" />
</td>
</tr>
<tr>
<td>
<form:label path="lastName">Last Name</form:label>
</td>
<td>
<form:input path="lastName" id="lastname" />
</td>
</tr>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
user.jsp
Please I need help to fix this bug.

In my case and as i searched internet - tomcat 10 does not work. I downloaded tomcat 9 and 404 disappeared. Spring MVC 5 does not work on Tomcat 10. This is because Tomcat 10 is based on Jakarta EE 9 where package names for APIs have changed from javax.* to jakarta.*.

maybe lack
*<mvc:annotation-driven/>*Annotation-driven injection (e.g. Controller, #requestMapping, etc.) results in a page always indicating that the relevant resource information cannot be accessed

You write, you try to run your first spring program with Spring MVC. I did not dive into your details, but I would suggest two different approaches to learn and play around with Spring MVC:
Spring initializer web site
When I try out new spring parts, I always start at
https://start.spring.io/
and just add my wanted dependencies (search for mvc when pressing add button).
After defined your setting there, you can just download a working maven or a gradle example directly from the web page as a zip file. After extraction the files can be directly imported by IDE (e.g. eclipse, intelliJ, etc.)
Official Spring MVC tutorial
When you are searching a good and out-of-the-box working MVC example you should look at https://spring.io/guides/gs/serving-web-content/
This works well and you can start directly without dependency problems etc.

Try adding tomcat jasper dependency to pom.xml
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>10.1.0-M5</version>
</dependency>
check tomcat version before adding the dependency

Related

Spring boot: 404 error when calling JSP using controller

I'm getting the following error when running my project using Spring Tool Suite,
But in case my problem is I have already added the appropriate dependencies to pom.XML file. So what could be the problem?
My pom.XML file dependencies as follows,
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
My controller ApplicationController.java as follows,
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class ApplicationController {
#RequestMapping("/")
public String Welcome() {
return "welcomepage";
}
}
My vives are in src/main/webapp/WEB-INF/view/welcomepage.jsp you can look the tree view below,
And I have already changed the application.properties file as well. But still, I can't understand what is wrong.
My application.properties file as follows,
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
I just print hello in My welcomepage.jsp,
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello
</body>
</html>
Looks like you was very close to the working application. The main issue in your code is in <scope>provided</scope> for your Jasper dependency.
And also looks like you are running your code from eclipse IDE through the main method.
Long story short:
If you would like to run your application through the main method in MyApplication.java then just remove scope provided for the Jasper.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
Or you can run your application exactly in that state like you have right now from the console:
mvn clean spring-boot:run
But I suggest to remove this scope so you could be able to run your code from IDE and from console. In addition to that looks like spring-boot-starter-tomcat dependency is redundant (it must be available within spring-boot-starter-web). In a nutshell please try to use following pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
</project>
Hope my answer will help you.
You may also need to add this in pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
UPDATE 1:
JSP Limitation
When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.
With Jetty and Tomcat, it should work if you use war packaging. An
executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
Undertow does not support JSPs.
Creating a custom error.jsp page does not override the default view
for error handling. Custom error pages should be used instead.
Scope
compile This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
provided This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
runtime This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
Also, Try to change the Following in tomcat-embed-jasper
Remove <scope>provided</scope> OR change the scope to compile <scope>compile</scope>
JSP Limitations
Spring Boot JSP 404
I was able to generate a jar from my application and then run it with java -jar myapp.jar But I only managed to run this jar with the version below spring-boot-starter-parent:
MyApp/pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
I researched in:
Spring Boot JSP 404

why isn't Spring MVC finding the path to my views?

I'm new to Spring and I'm trying to make a simple web app, however I cannot get started with the basics and cant make even a HelloWorld application.
Here is what I am Using:
Spring 5
Spring boot 2.1
Eclipse Photon, I used the add-on Spring Tools Version 3.9.6 to create the proyect
Here is what I did:
Created via File -> New -> Spring Starter Proyect, selected type package type as WAR, also in the dependencies section I selected Web
Added jstl and jasper dependencies to the pom.xml, here is the whole file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.logback.app</groupId>
<artifactId>spring-boot-logback</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-boot-logback</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
**<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>**
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
My view template is as follows:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1" />
<title>Logback</title>
</head>
<body>
<h1>Hello world</h1>
<h2><c:out value="${titulo}"/></h2>
</body>
</html>
This is my controller:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
#Controller
public class IndexController {
#GetMapping("/index")
public String index(Model model) {
model.addAttribute("titulo", "Pruebas Logback");
return "index";
}
}
And I added these to the application.properties file
spring.mvc.view.prefix: /WEB-INF-/views/
spring.mvc.view.suffix: jsp
With all these configuration it should work, but when I enter the address localhost:8080/index it throws what it looks like a 404 error
And the strangest thing is that neither in the web browser console or in the Eclipse console are errors that point out to the right direction.
What do you think it can be?
Thanks ind advance
Thanks to Piotr Podraza I realized my mistake
Changed
spring.mvc.view.prefix: /WEB-INF-/views/
spring.mvc.view.suffix: jsp
to
spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
The path to my views was incorrect, and I was missing the period of the jsp suffix
Thank you!

JSP Parameters not accessible using ${param.xxx} using HTML form

I'm following a tutorial on Udemy that explains JSPs and Servlets:
https://www.udemy.com/jsp-tutorial
The tutorial uses Eclipse + Tomcat server.
Since I'm an IntelliJ and Maven user I wanted to set up my environment using these two. So I created a Maven project from the following archetype: "org.apache.maven.archetypes:maven-archetype-webapp" and configured my POM as folllows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SomeGroupId</groupId>
<artifactId>HelloWorldJavaServerPages</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>HelloWorldJavaServerPages Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>HelloWorldJavaServerPages</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>Tomcat85Localhost</server>
<username>admin</username>
<password>admin</password>
</configuration>
</plugin>
</plugins>
</build>
</project>
Set up my other properties as described here:
Tomcat 8 Maven Plugin for Java 8
I am able to build my project and use the tomcat plugin to deploy to the tomcat server (or manually drag the war file in the webapps folder).
Problem is that when I have the following two files:
student-form.html
<html>
<head><title>Student Registration Form</title></head>
<body>
<form action="student-response.jsp">
First name: <input type="text" name="firstName" />
<br/>
Last name: <input type="text" name="lastName" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
student-response.jsp
<html>
<head><title>Student Confirmation Title</title></head>
<body>
The student is confirmed: <%= request.getParameter("firstName")%> ${param.lastName}
</body>
</html>
The request.getParameter method works, but the ${param.lastName} does not and it simply shows up as plain text in the browser: ${param.lastName}.
Using Eclipse (without Maven) it does work for both, so I'm wondering what I'm doing actually different/wrong here and why it is not working.
Thanks in advance for your help.
Once I found out that the references between the braces are called the "Expression Language", I decided to do another search on stackoverflow and found this:
Expression Language in JSP not working
Changing the web.xml as described in the link above made it work.
Thank you all for reading my question, the answer was quite simple actually.

JDBC driver not found for PostgreSQL database while building with Maven

I am following a course "Learning Path: Enterprise Web Programming with Java" by O'Reilly. I have re-written code for a simple webapp using PostgreSQL. I build it with Maven and run it on a Tomcat server. The database is up and filled. I receive the following error:
javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for ${ourDS}"
I have found that it is usually a result of no postgres dependency. I have also tried adding other dependencies like tomcat plugin, etc.
I understand that I miss a small piece of dependency or some file somewhere, but I can not find out what is it.
The main .jsp file has a following code:
<%# page errorPage = "error.jsp" %>
<%# taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri = "http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<!DOCTYPE html>
<html>
<head>
<title>List of products in our database</title>
<link rel = "stylesheet" href = "style.css" type = "text/css"></link>
</head>
<body>
<sql:setDataSource
var = "ourDS"
driver = "org.postgresql.Driver"
url = "jdbc:postgresql://localhost:5432/skistuff"
user = "******"
password = "*******"
/>
<sql:query var = "productList" dataSource = "${ourDS}">
SELECT * FROM skisEtc ORDER BY id;
</sql:query>
<!-- Some kind of display -->
</body>
In contrary to the lecturer, I use Maven instead of Ant, so I have to figure it out other way. From what was mentioned in the course, I prepared the following pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.training.skis</groupId>
<artifactId>skispagebasic</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>skispagebasic Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.4.jre7</version>
</dependency>
</dependencies>
<build>
<finalName>skispagebasic</finalName>
</build>
</project>

spring-boot 404 error in serving .js, .css files to html templates

I am using spring boot and was trying to host HTML pages that use angular js.
My folder structure is :
I referred to a lot of other question on SO and even spring.io guide on spring boot and what I understood was that we are supposed to keep our html templates in templates folder and all the .css, images, .js files in static folder. I have done the same, but whenever the page loads , only the html is rendered and browser get 404 for all .css and .js files.
I have include .css and .js in my HTML in the following manner :
<script src="/js/socket.io/socket.io.js"></script>
<script src="/js/moment.min.js"></script>
<script src="/js/demoApp.js"></script>
I tried all the combinations like :
src="../static/js/socket.io/socket.io.js" or src="js/socket.io/socket.io.js"
but always I am getting 404 for these files, in the browser.
I checked the spring boot logs and this is what it says :
2016-07-24 21:08:05 WARN PageNotFound:1139 - No mapping found for HTTP request with URI [/js/demoApp.js] in DispatcherServlet with name 'dispatcherServlet'
2016-07-24 21:08:05 DEBUG DispatcherServlet:997 - Successfully completed request
I am unable to understand how to solve the issue, please help !!!
Note : I have not written view resolver code because according to my understanding spring boot automatically picks up static content from static folder. Please correct me if I am wrong.
Edit : Adding my pom file :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
<!--Alexa Dependencies -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
<version>2.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.amazon.alexa</groupId>
<artifactId>alexa-skills-kit</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.9.40</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build>
I see you are using thymeleaf so try to access your resources like this :
<script th:src="#{/js/socket.io/socket.io.js}"></script>
<script th:src="#{/js/moment.min.js}"></script>
<script th:src="#{/js/demoApp.js}"></script>
Also and if this does not work can you add your index.html.
I have found the problem , everything was correct but :
1) I was not extending my Application class from WebMvcAutoConfiguration.
#SpringBootApplication
#EnableWebMvc
public class HLACUIApplication extends WebMvcAutoConfiguration {
public static void main(String[] args) {
SpringApplication.run(HLACUIApplication.class, args);
}
}
This is how the application class should be.
2) I don't need to use templates folder because I am using angularjs and not thymeleaf. I have put everything in static folder.
In my case with thymeleaf, helped when I skipped '/' before 'js'.
Example, instead of
<script src="/js/demoApp.js"></script>
I've put
<script src="js/demoApp.js"></script>
I was facing the same issue. I think you are having html code like below:
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
<!-- Main Styles -->
<link rel="stylesheet" href="/css/bar.css" />
<script src="/js/foo.js"></script>
</head>
<body>
<div>Welcome to Foo!</div>
</body>
</html>
I solved the problem by adding type="text/javascript" to the <script> element. In Spring Boot applications it's required to define type="text/javascript", otherwise it's not going to load.
The solution for your example would look like this:
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
<!-- Main Styles -->
<link rel="stylesheet" href="/css/bar.css" />
<script type="text/javascript" src="/js/foo.js"></script>
</head>
<body>
<div>Welcome to Foo!</div>
</body>
</html>
If you are using Eclipse IDE right click on Project Properties/Project Facets

Categories