How to set Context Path in Spring Boot

By default, the web application Context Path is “/”. This example shows you 2 ways to set Context Path in Spring Boot: by Java Code and by properties file.

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. Ways to do

For example, if we wanna set Context Path is /javasampleapp, port 8080, just choose one of 2 simple ways:

– By Java Code, we create a Component that implements EmbeddedServletContainerCustomizer, then override customize() method:

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;

@Component
public class ConfigContainer implements EmbeddedServletContainerCustomizer {

	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		
		container.setPort(8080);
		container.setContextPath("/javasampleapp");
	}

}

– By properties file, we just add these code:

server.port = 8080
server.contextPath = /javasampleapp

If we wanna create referenced links in JSP pages, just use:

${pageContext.request.contextPath}/[linkAddress...]
2. Project Structure


WebController is a Controller which has request mapping methods to display our page.
ConfigContainer is a Component which is used to set Context Path.
webapp folder contains all necessary JSP file and static resource.
application.properties contains settings for prefix and suffix for JSP files and Context Path.
Dependencies for Spring Boot and Tomcat Embed Jasper in pom.xml.

Remember that we should only choose one of 2 ways: using ConfigContainer or using Context Path in application.properties.

III. Practice
1. Dependency

For War Packaging:

		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		

		
			org.apache.tomcat.embed
			tomcat-embed-jasper
			provided
		

Note: (for another case)
If you create a Spring Starter Project with Jar Packaging, you should change

jar

to

war

Then, extends @SpringBootApplication Class with SpringBootServletInitializer and add the code below:

	@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 and about.jsp:

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>








Java Sample Approach Demo



	

My home page

This is content.

About%MINIFYHTML22827fd98ae7805790bc7d500dcf55aa16%

about.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




Java Sample Approach Demo


About.


– Next, create main.css in webapp/css:

h2 {
	background-color: blue;
	color: white
}

p {
	color: red;
}
3. Set prefix and suffix resolution

Under src/main/resources, open application.properties and add:

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:

		
			javax.servlet
			jstl
		

– then create a Bean:

	@Bean
	public ViewResolver getViewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/WEB-INF/jsps/");
		resolver.setSuffix(".jsp");
		resolver.setViewClass(JstlView.class);
		return resolver;
	}
4. Set Context Path

Way 1: Open application.properties and add:

server.port = 8080
server.contextPath = /javasampleapp

Way 2: Under package config, create ConfigContainer Class that implements EmbeddedServletContainerCustomizer:

package com.javasampleapproach.contextpath.config;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;

@Component
public class ConfigContainer implements EmbeddedServletContainerCustomizer {

	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		
		container.setPort(8080);
		container.setContextPath("/javasampleapp");
	}

}
5. Create Web Controller

Under package controller, create WebController class:

package com.javasampleapproach.contextpath.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WebController {

	@RequestMapping("/")
	public String home() {
		
		return "home";
	}
	
	@RequestMapping("/about")
	public String about() {
		
		return "about";
	}
}
6. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

http://localhost:8080/

http://localhost:8080/javasampleapp/

http://localhost:8080/javasampleapp/about

IV. Source Code

SpringContextPath



By grokonez | February 24, 2017.

Last updated on June 4, 2017.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*