Reputation: 12061
First: the HTML shows all the css and it cannot be recognized:
Second: The project runs just localhost but just give me the following output and does not start port 8080:
2017-03-11 12:07:48.579 INFO 2397 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.12.Final}
2017-03-11 12:07:48.581 INFO 2397 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-03-11 12:07:48.582 INFO 2397 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-03-11 12:07:48.622 INFO 2397 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-03-11 12:07:48.748 INFO 2397 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2017-03-11 12:07:49.046 INFO 2397 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2017-03-11 12:07:49.047 INFO 2397 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
2017-03-11 12:07:49.076 INFO 2397 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-03-11 12:07:49.544 INFO 2397 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-03-11 12:07:49.561 INFO 2397 --- [ main] com.brock.BrockApplication : Started BrockApplication in 12.875 seconds (JVM running for 13.425)
2017-03-11 12:07:49.562 INFO 2397 --- [ Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@21a947fe: startup date [Sat Mar 11 12:07:47 EST 2017]; root of context hierarchy
2017-03-11 12:07:49.564 INFO 2397 --- [ Thread-3] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2017-03-11 12:07:49.564 INFO 2397 --- [ Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-03-11 12:07:49.565 INFO 2397 --- [ Thread-3] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2017-03-11 12:07:49.565 INFO 2397 --- [ Thread-3] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
Process finished with exit code 0
File structure:
In HomeController I have:
@Controller()
public class HomeController {
@RequestMapping("/")
public String home(){
return "index";
}
}
The JS and CSS file paths look like this:
<link rel="stylesheet" href="css/bootstrap.min.css">
According to the docs I put them in the correct place and it should return index.html.
-----------------Update 1----------------
applications.properities
spring.datasource.initialize=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=- 1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=brock
spring.datasource.password=password
spring.h2.console.enabled=true
Upvotes: 1
Views: 1488
Reputation: 44745
The issue causing the application to shut down immediately is caused by using WAR packaging, while running the application as a Spring boot application in IntelliJ.
Due to that, IntelliJ won't provide the necessary classes (because spring-boot-starter-tomcat has scope provided) and if it's missing, Spring boot won't know that it should run as a web application causing it to exit immediately.
To solve it either:
Comment the provided scope:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- <scope>provided</scope> -->
</dependency>
When you fix the first problem, you still have another issue. You've placed your HTML files within the src/main/resources/templates folder, while this folder should only be used for dynamic templates (Thymeleaf, Freemarker, Groovy, Mustache, ...).
However, currently your HTML files are just plain, static HTML files, so you should put them in src/main/resources/static. If they aren't static HTML files, you should add a proper template engine, for example spring-boot-starter-thymeleaf.
If you use static templates, Spring boot will automatically resolve /
to your index.html
, so you can delete HomeController
.
Finally, you said you're using the following CSS:
<link rel="stylesheet" href="css/main.css">
This is the right way to do it. However, on GitHub I noticed that you were using:
<link rel="stylesheet" href="../../resources/static/css/main.css">
This is not the right way. Even though the folder structures are correct, at runtime, Spring boot will run both public/
as templates/
on your context path root (/
). This means that, even if you would have index.html
in one folder and css/main.css
in your other folder, it would still work.
Upvotes: 3
Reputation: 1944
Try returning a modelandview instead of raw string?
@RestController()
public class HomeController {
@RequestMapping("/")
public ModelAndView home(){
return new ModelAndView("index");
}
}
Upvotes: 1
Reputation: 3738
Teena is right use @RestController
or if you use @Controller
add the @ResponseBody
annotation
@Controller
@ResponseBody
@RequestMapping("/")
public class HomeController {
@RequestMapping(method = RequestMethod.GET)
public String get(){
return "index";
}
}
Upvotes: 0
Reputation: 743
Can you annotate your HomeController class with @RestController annotation and deploy your app again
Upvotes: 2
Reputation: 135
Adding spring-boot-starter-web
to your dependencies will enable embedded tomcat as default servlet container and your application will be exposed on port 8080
by default.
Gradle:
compile("org.springframework.boot:spring-boot-starter-web")
Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Upvotes: 0