In Java Web Application Development, JSP page and static resource (css file, javascript file) are two of the most important thing to start. This tutorial shows you how to embed them into a Spring Boot application.
Contents
I. Technology
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE
II. Overview
1. Project Structure
– WebController is a Controller which has request mapping methods to display our page.
– webapp folder contains all necessary JSP file and static resource.
– application.properties contains settings for prefix and suffix for JSP files.
– Dependencies for Spring Boot and Tomcat Embed Jasper in pom.xml.
2. Step to do
– Create Spring Boot project & add Dependencies
– Create jsp file and static resource
– Set prefix and suffix resolution
– Create Web Controller
– Run Spring Boot Application & Enjoy Result
III. Practice
1. Create Spring Boot project & add Dependencies
Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields. Remember that we will the Packaging is War.
Click Next, then click Finish.
Open pom.xml and add Dependencies:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<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.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> |
Note: (for another case)
If you create a Spring Starter Project with Jar Packaging, you should change
1 |
<packaging>jar</packaging> |
to
1 |
<packaging>war</packaging> |
Then, extends @SpringBootApplication Class with SpringBootServletInitializer and add the code below:
1 2 3 4 |
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringJspResourceApplication.class); } |
2. Create jsp file and static resource
– Under src/main/webapp folder, create new folder named WEB-INF and jsps inside, then create home.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <link href="/css/main.css" rel="stylesheet"></link> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script type="text/javascript" src="js/main.js"></script> <title>Java Sample Approach Demo</title> </head> <body> <h2>My home page</h2> <p>Hello ${name}, this is Content:</p> <span id="content"></span> </body> </html> |
– Next, create main.css in webapp/css:
1 2 3 4 5 6 7 8 |
h2 { background-color: blue; color: white } p { color: red; } |
– And main.js in webapp/js:
1 2 3 |
$(document).ready(function(){ $("#content").text("Welcome to my Homepage!"); }); |
3. Set prefix and suffix resolution
Under src/main/resources, open application.properties and add:
1 2 |
spring.mvc.view.prefix = /WEB-INF/jsps/ spring.mvc.view.suffix = .jsp |
We have another way to set prefix and suffix, just follow the steps below:
– add dependency to pom.xml:
1 2 3 4 |
<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> |
– then create a Bean:
1 2 3 4 5 6 7 8 |
@Bean public ViewResolver getViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/jsps/"); resolver.setSuffix(".jsp"); resolver.setViewClass(JstlView.class); return resolver; } |
4. Create Web Controller
Under package controller, create WebController class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.javasampleapproach.jspresource.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class WebController { @RequestMapping("/") ModelAndView home(ModelAndView modelAndView) { modelAndView.setViewName("home"); modelAndView.getModel().put("name", "John"); return modelAndView; } } |
5. Run Spring Boot Application & Enjoy Result
– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:
IV. Source Code
Last updated on June 4, 2017.
ViewResolver is not found on the supplied code