In case you've been struggling with this for a while and nothing helps, hear me out! I may help you! I've been there: I searched through the internet and was growing pretty desperate, but then I did this, and the problem vanished 🙌
- You must have this Tomcat dependency
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
Notice that it has a default scope. The default scope is compile
. It is important that you have this scope!
- You should have these JSP/JSTL dependencies, both of them (at least, if you use something like a
forEach
tag)!
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
Notice that they belong to the jakarta
package. In my case, it was important! Maybe, it may be important in your case too. Before I migrated my project to Spring Boot, I used javax
dependencies, specifically these two
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
When trying to open my home page with those dependencies, I received error 500
java.lang.ClassNotFoundException: javax.servlet.jsp.tagext.TagLibraryValidator
I added some more javax
dependencies to appease Spring a little
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
and it still didn't work in Spring Boot! This time, I received ClassCastException
because apparently Spring needed something specifically from the jakarta
package
java.lang.ClassCastException: class org.apache.taglibs.standard.tlv.JstlCoreTLV cannot be cast to class jakarta.servlet.jsp.tagext.TagLibraryValidator (org.apache.taglibs.standard.tlv.JstlCoreTLV and jakarta.servlet.jsp.tagext.TagLibraryValidator are in unnamed module of loader 'app')
- MOST IMPORTANTLY! It appears you do need a
webapp
package when working with JSP views! I thought that since it's Spring Boot, I don't need that directory and may place my views in the resources/templates
folder as HTML/Thymeleaf folks do. BIG MISTAKE! Even when my properties looked like this
spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix= .jsp
or like this
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix= .jsp
in fact, any way you could imagine (I tried to describe the directory in all possible ways), I still got that pesky 404! Not before I created a webapp
directory had I managed to get rid of that problem
Notice that tiny bluish circle on the webapp
folder icon. I have a suspicion that it's important. As a matter of fact, I did try to create such a directory before, but it didn't have that little circle and moving my JSPs there had no effect on my 404. Why it didn't have that circle the first time? I don't know! But at any rate I suspect that the webapp
package should be in the same directory as your source root. In this case, it's the java
package. So since it's in the main
folder, your webapp
directory should be in the main
folder too
Fun fact! My favicon and CSS stylesheet are still in the resources
directory (that is, in the resources/static
directory)! Despite that, Spring has no problem figuring out where they are. So the webapp package is necessary only for your JSPs (perhaps, you can store CSSs and favicons there too, I didn't try it, but at least you don't have to)
Why it works this way with JSP? Search me! I have no idea at all! But it's what helped me and what you should apparently do too if you experience the same issue
You can get in touch with me and request the project's repo if that's something you need 👌
UPD: A quick note. Some folks maintain that you need to have a WAR package for JSP projects. It doesn't appear so! But keep it in mind anyway. If you did every thing I listed and it still doesn't work, tweaking that property may be a good thing to try 🤷♂️
UPD2: After I removed the WAR packaging setting, the little circle on the webapp
icon disappeared. But the page still opens! So since you apparently don't need WAR packaging, you also shouldn't be concerned about the blue circle. I take that back! Or maybe, the circle has nothing to do with WAR at all as I briefly returned that property and clean-installed the project, and it didn't appear again 🤷♂️
UPD3: By the way, that underlining never went away. But it's okay! It works nonetheless