How to connect css and javascript in a .jsp file

47 Views Asked by At

I am having trouble connecting an external css and javascript file to its jsp.

I tried doing the WebConfig as I read on google but I realised that my jsp files' path are done via application.properties so when it ran it override the application.properties and the .jsp could not be found. I deleted the WebConfig and stuck with the application.properties and tried to add a filepath for css

spring.mvc.view.prefix=/WEB-INF/views/ 
spring.mvc.view.suffix=.jsp  
spring.web.resources.statstaticic-locations=classpath:/static/,classpath:/public/,classpath:/resources/,classpath:/META-INF/resources/ 

These made the JSP run but it could not read the css.

File Path for JSP: src-> main -> webapp-> home.jsp

home.jsp:

<head> 
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<link rel="stylesheet" type="text/css" href="c:url value='/css/home.css' />">     
<title>Project One</title> </head> <body>

File Path for CSS: src-> main-> resources-> static-> css-> home.css

I am not sure if this matters but these are the dependencies I am using (gradle):

dependencies {

   implementation 'org.springframework.boot:spring-boot-starter-web'
   developmentOnly 'org.springframework.boot:spring-boot-devtools'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'
  implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  runtimeOnly 'com.mysql:mysql-connector-j'



   runtimeOnly 'org.apache.tomcat.embed:tomcat-embed-jasper:10.1.9' //jsp
   runtimeOnly 'org.glassfish.web:jakarta.servlet.jsp.jstl:3.0.0'  //helpful jsp tags
 
1

There are 1 best solutions below

0
Roman C On

You have not configured static resources or tried to configure it but have a wrong property name.

spring.web.resources.statstaticic-locations=classpath:/static/,classpath:/public/,classpath:/resources/,classpath:/META-INF/resources/ 

See chapter 27: Developing web applications

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources ) in the classpath or from the root of the ServletContext.