My Works

Scala Based SpringMVC Application Without XML

Here we are going to look at creating a simple example in Scala language for SpringMvC with WebApplicationIntializer. It means that we would not be requiring web.xml as means of initialising the servlet.Therefore we would not have web.xml in our example. We would be programmatically configuring without using the xml files and use annotation based loading of context and beans.

  1. Project Dependencies.
  2. Create WebInitializer.
  3. Create MVC Context Configuration programmatically.
  4. Create View Configuration programmatically.
  5. Create helloworld example JSP page.
  6. Add a unit test for the controller.
  7. Based on the Unit test create the controller.
  8. Deploy and Run.

1.Project Dependencies.

JDK 1.6 or above,In this case have chosen Tomcat 7.0.67 , Spring MVC jars, Scala 2.11.7 is used in this Example, junit jar and logging jars are the ones we would need. The build.gradle file dependencies are listed below.

apply plugin: "scala"
apply plugin: "eclipse"
apply plugin: "war"
apply plugin: "eclipse-wtp"
apply plugin: "com.bmuschko.tomcat"
group = projGroup
version = projVersion
buildscript {
repositories {
maven { url '' }

dependencies {
classpath "com.bmuschko:gradle-tomcat-plugin:2.2.4"

repositories {
maven { url '' }
maven { url '' }
maven { url '' }
maven { url '' }

war {
archiveName 'SpringMVCScalaWebIntializer.war'
dependencies {
compile "org.scala-lang:scala-library:2.11.7"
providedCompile ("javax.servlet:javax.servlet-api:3.0.1")
compile ("org.springframework:spring-webmvc:3.2.3.RELEASE")
compile ("org.springframework:spring-expression:3.2.3.RELEASE")
compile ("org.springframework:spring-test:3.2.3.RELEASE")
compile ("commons-logging:commons-logging:1.1.1")
compile ("org.slf4j:jcl-over-slf4j:1.7.0")
compile ("org.slf4j:slf4j-api:1.7.0")
compile ("org.slf4j:slf4j-log4j12:1.7.0")
compile ("junit:junit:4.11")
sourceSets {
main {
scala {
include '**/*.scala'
sourceSets {
test {
scala {
include '**/*.scala'
dependencies {
def tomcatVersion = '7.0.67'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
task wrapper(type: Wrapper) { gradleVersion = '2.10' }


settings.gradle = projName

2.Create WebInitializer.

SpringMVC with Servlet 3.0 detects the WebApplicationInitializer implementations by using SpringServletContainerInitializer. Thus help to initialize our web application by configuring the ServletContext programmatically.So We create a class SrinivasExampleWebApplicationInitializer which implements the WebApplicationInitializer. This class methods starts up the web application with a Servlet registered with required contexts and beans. In This class a DispatcherServlet is created containing programmatically configured AnnotationConfigWebApplicationContext.The dispatcher is also mapped to .html. For more info on WebApplicationInitializer, Please refer here..


class SrinivasExampleWebApplicationInitializer extends WebApplicationInitializer {
val logger = LoggerFactory.getLogger(classOf[SrinivasExampleWebApplicationInitializer])
/* (non-Javadoc)
* @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.ServletContext)

override def onStartup(container:ServletContext):Unit = {"Started to pickup the annotated classes")
* Start servlet.
* @param container   the ServletContext
private def startServlet(container:ServletContext):Unit = {
val dispatcherContext:WebApplicationContext = registerContext(classOf[MvcContextConfiguration])
val  dispatcherServlet:DispatcherServlet = new DispatcherServlet(dispatcherContext)
val dispatcher:ServletRegistration.Dynamic = container.addServlet("dispatcher",dispatcherServlet)
val mapping: String = "/"
* Register context.
* @param annotatedClasses the Class type variable argument of classes that needs to be registered to the context.
* @return the web application context
def registerContext(@varargs annotatedClasses:Class[_]):WebApplicationContext = {"Using AnnotationConfigWebApplicationContext createContext");
val context:AnnotationConfigWebApplicationContext  = new AnnotationConfigWebApplicationContext()
return context;


3.Create MVC Context Configuration programmatically.

Now, We are going to configure the MVC context programmatically. This is achieved by creating a class MvcContextConfiguration which extends the Abstract WebMvcConfigurerAdapter.Usually MvcContextConfiguration class can contain method for registering interceptors for security purposes, can contain method to register view controllers and so on.We in this case override the method configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) by enabling DefaultServletHandlerConfigurer.This means unhandled requests are delegated by forwarding to the Servlet container's "default" servlet. This method is optional and is not required in our example.However it is included to explain the purpose achieved by overriding the methods of WebMvcConfigurerAdapter. MvcContextConfiguration class enables WebMVC based on annotation and scans the base packages for the annotated resources. These are further registered by the web application context of the default servlet which was initialized by WebApplicationInitializer. For more info on WebMvcConfigurerAdapter, Please refer here..


@ComponentScan(basePackages = Array("org.srinivas.siteworks","org.srinivas.siteworks.controllers"))
class MvcContextConfiguration extends WebMvcConfigurerAdapter {
val logger = LoggerFactory.getLogger(classOf[MvcContextConfiguration])

override	def configureDefaultServletHandling(configurer:DefaultServletHandlerConfigurer ):Unit={"WebMvcContextConfiguration: configureDefaultServletHandling Method")

4.Create View Configuration programmatically.

We now create a class ViewConfiguration.This class beans are loaded during the component scan of class MvcContextConfiguration. Thereby it is registered to the servlet. We programmatically configure a ViewResolver bean of Type InternalResourceViewResolver in this class. This helps the view class to be preconfigured with the set prefix and set suffix.

* View resolver.
* @return the Resolved view
def viewResolver():ViewResolver = {"ViewConfiguration viewResolver()")
val viewResolver:InternalResourceViewResolver = new InternalResourceViewResolver()
return viewResolver


5.Create helloworld example JSP page.

Now, we need to create a simple jsp page which takes the model attribute message which was set in the Controller.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Works:WebApplicationInitializer</title>

6.Add a unit test for your controller.

Now, we need to add unit test for the controller of helloworld.jsp that we created. In this case we are executing the unit test using SpringJUnit4ClassRunner. We are actually checking the HelloWorldController for successful status, return of view string and for the message attribute. For more info on MockMvC, Please refer here..

@ContextConfiguration(classes = Array(classOf[HelloWorldControllerTestConfiguration]))
class HelloWorldControllerTest {
val logger = LoggerFactory.getLogger(classOf[HelloWorldControllerTest])

@Autowired val helloWorldController:HelloWorldController = null
* Test helloPage method of HelloWorldController
def  testHelloPage():Unit = {
val  mockMvc:MockMvc = MockMvcBuilders.standaloneSetup(this.helloWorldController).build()
try {
} catch {
case e: Exception => {"Error",e);
fail("Failed Due to: "+e.getMessage());		}

7.Based on the Unit test create the controller.

Based on the expected results in the Unit test for helloworld.jsp controller that we created above.We now create the HelloWorldController.


class HelloWorldController {
val STRING_MESSAGE_WELCOME_TO_WEB_APPLICATION_INITIALISER:String = "Welcome to WebApplication Initialiser"
val logger = LoggerFactory.getLogger(classOf[HelloWorldController])

* Hello page.
* @param model the model
* @return the string
@RequestMapping(value = Array("/helloworld.mvc"), method =Array(RequestMethod.GET))
def helloPage(model:Model):String = {"HelloWorldController")
return "helloworld";

8.Deploy and Run.

Build the project and deploy SpringMVCScalaWebIntializer.war from build/libs folder on to the chosen Tomcat 7.0.67. For Gradle building of the project you can refer to ReadMe.txt instructions in the worked out example.You can download the Worked example and Follow the commands provided in the zip to run embedded Tomcat. Following deployment to Tomcat(emmbedded or standalone) and starting the Tomcat(Standalone),the localhost result is as below.

Calling Hello World on Web ApplicationIntializer