Reputation: 2607
I've started my Spring boot application, and the applicaiton has started when I see the console, but when I try to open the browser to run, it shows No webpage was found for the web address: http://localhost:8080/Demo/login.html. What is the problem here? Here is my code
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication .class, args);
}
}
My login.html is available under
src/main/resources/templates/login.html
Here is the pom.xml
<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.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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Log it shows clearly as Tomcat server started at 8080 port number
2018-12-19 17:02:24.213 INFO 709136 --- [ main]
com.test.demo.DemoApplication : Starting DemoApplication on Test with PID 709136 (C:\Users\Syed\Documents\DEMO\DEMO\target\classes started by Syed in C:\Users\Syed\Documents\DEMO\DEMO)
2018-12-19 17:02:24.254 INFO 709136 --- [ main]
com.test.demo.DemoApplication : The following profiles are active: @spring.profiles.active@
2018-12-19 17:02:25.674 INFO 709136 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-12-19 17:02:25.705 INFO 709136 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-12-19 17:02:25.706 INFO 709136 --- [ main]
org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.13
2018-12-19 17:02:25.715 INFO 709136 --- [ main]
o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_45\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/lib/amd64;C:\Program Files (x86)\RSA SecurID Token Common;C:\Program Files\RSA SecurID Token Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Citrix\System32\;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\McAfee\Solidcore\Tools\GatherInfo;C:\Program Files\McAfee\Solidcore\Tools\Scanalyzer;C:\Program Files\McAfee\Solidcore\;C:\Program Files\McAfee\Solidcore\Tools\ScGetCerts;C:\Program Files\Citrix\System32\;C:\Program Files\Citrix\ICAService\;C:\Program Files\Common Files\Hitachi ID\;C:\Program Files\nodejs\;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.8.0_45\bin;C:\Program Files\Java\jdk1.8.0_45\jre\bin;C:\Users\AF68935\AppData\Roaming\npm;C:\Users\AF68935\AppData\Roaming\npm\node_modules\@angular\cli\bin;;C:\Users\AF68935\Downloads\spring-tool-suite-4-4.0.2.RELEASE-e4.9.0-win32.win32.x86_64\sts-4.0.2.RELEASE;;.]
2018-12-19 17:02:25.837 INFO 709136 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-12-19 17:02:25.837 INFO 709136 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1510 ms
2018-12-19 17:02:26.214 INFO 709136 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2018-12-19 17:02:26.523 INFO 709136 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-19 17:02:26.542 INFO 709136 --- [ main] com.test.demo.DemoApplication : Started DemoApplication in 2.95 seconds (JVM running for 3.852)
2018-12-19 17:04:35.175 INFO 709136 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2018-12-19 17:04:35.175 INFO 709136 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2018-12-19 17:04:35.198 INFO 709136 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 23 ms
What could be the issue here? Any ideas would be greatly appreciated.
Upvotes: 0
Views: 17702
Reputation: 1148
If your static content is located in a directory other than /static
, /public
, /resources
or /META-INF/resources
.
Then add property spring.resources.static-locations
to your application.properties
to point to the static content.
spring.resources.static-locations=classpath:/templates/
You will see the following message in the console log:
Adding welcome page: class path resource [templates/index.html]
Reference documentation
By default, Spring Boot serves static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath
You can also customize the static resource locations by using the spring.resources.static-locations property (replacing the default values with a list of directory locations).
Upvotes: 1
Reputation: 24
With your provided code, launching a GET against your application will return the 404 Spring boot page as you don't handle the relative path '/'
You have to add a Controller class (with @Controller annotation) indicating the request mapping and the request method and render any page you want
Upvotes: 0
Reputation: 743
You should read about controllers, if you are learning spring. Try to create a sample controller and when you will run it then go to localhost:8080/ and you should see home.
@Controller
public class HomeController
{
@GetMapping("/")
public String homeInit() {
return "home";
}
}
Upvotes: 1