Newsletter |
Spring Boot + Spring MVC + JSP Hello World Example
Spring-Boot-Tutorials » on May 17, 2018 { 5 Comments } By Sivateja
This article describes how to create a Spring MVC application using Spring Boot. As this is an MVC application unlike previous examples, we have to create a webapp folder under /src/main (src > main > webapp) where we will place all our .jsp files.
Dependencies
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
Final 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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java4s</groupId> <artifactId>SpringBootSpringMVC</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <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> <scope>provided</scope> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> </project>
application.properties
# Applicationn context name server.contextPath=/springbootmvc # Spring MVC related spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
In the current example, I am going to put all my .jsp file(s) under /WEB-INF/jsp/ folder, no need to mention ‘webapp’ in the properties file as spring boot by default consider that folder and search under webapp.
SpringBootApp.java
package com.java4s.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootApp { public static void main(String[] args) { SpringApplication.run(SpringBootApp.class, args); } }
SpringJava4sController.java
package com.java4s.app.controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; @RestController public class SpringJava4sController { @GetMapping("/") public ModelAndView showLoginPage(Model model) { model.addAttribute("message", "Welcome to Java4s Spring Boot Tutorials"); return new ModelAndView("welcomePage"); } }
welcomePage.jsp
${message}!
Output
Run the application and hit http://localhost:8080/springbootmvc/
You Might Also Like
::. About the Author .:: | ||
Comments
5 Responses to “Spring Boot + Spring MVC + JSP Hello World Example”
I think we should use @Controller in place of @RestController in SpringJava4sController.java
yeah correct. but if you want to return JSON data not possible with @controller, need to use @Restontroller
Really appriciate on your effort on SpringBoot and really you made it so easy and handy. It will be helpfull if you will add some medium scale project related to SpringBoot.
Thanks
which dependency we have to add for springMVC in POM.xml file
in above program you added dependencies for spring boot, those dependencies work for spring also ?
Can you please give an example to connect Spring with Angular JS?