Go to: start.spring.io and Generate Project
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // ~ @Configuaretion + @EnableAutoConfiguration + @ComponentScan ()
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
//System.out.println("Spring Boot Web MVC Started...");
}
}
Go to: src/main/resources > application.properties
Add:
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
- Create controller package
- Create controller
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class TestController {
@GetMapping("/")
public String dsiplayTestPage(Model model) {
model.addAttribute("tm", "Test Message");
return "test";
}
}
Create Run Configuration:
Name: Mvn Spring Boot Run
Goals: spring-boot:run
Alternative:
Right click on project > Run As > Spring Boot App
Cleaning
Name: Mvn Clean
Goals: clean
Edit Pom:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.thanga.[REPLACE WITH YOUR MAIN CLASS]</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
Create Run Configuration
Name: Pack As Single Assembly
Goals: package assembly:single
Run (Maven will create two jars - app.jar & depedency.jar)
NB: requires internet connection to download dependencies
Offline Build Method 1
mvn dependency:go-offline
command will download everything that your project depends on and will make sure that on a later build, nothing will be downloaded. Then, you can simply make a ZIP of {user.home}/.m2/repository.- unzip inside offline machine {user.home}/.m2/repository (will be able to build the project)
Offline Build Method 2
- Get the dependency.jar from "Single Assembly Build" described above
- Maven command to deploy to offline machine:
mvn install:install-file -Dfile=<path-to-file>
Remove following from application.properties
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
Jasper dependency is also not needed
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
To use thymeleaf template engine instead of JSP & JSLT, add dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
By default thymeleaf will look in src/main/resources/templates
with .html
files (jar deploy)
To deploy as war change default configuration in application.properties
spring.thymeleaf.presfix= /WEB-INF/views/
pring.thymeleaf.suffix= .html
Add spring-boot-devtools
dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Open application.properties
set thymeleaf cache to false
spring.thymeleaf.cache= false