Spring MVC application with Cucumber and Selenium part 1

Submitted by Barnaby Golden on Tue, 18/06/2013 - 17:23

Part 1 - Build a simple Spring MVC application

This is a multi-part posting on building a reference web application.

The focus here is on tools and process rather than the actual functionality.

Bootstrap a simple Maven project (easiest in Eclipse with the M2Eclipse plugin).

Add the following directories (if not there already):

src/main/java src/main/resources src/test/java src/test/resources

Add the following dependencies to the pom:

spring-core spring-context spring-webmvc

and make sure you have the following directories:

src/main/webapp src/main/webapp/WEB-INF

Add a simple web.xml to the WEB-INF directory, something like:

  <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="WebApp_ID" version="2.5">
      <display-name>Spring MVC tutorial</display-name> 
      <servlet>
        <servlet-name>SpringMvcServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>SpringMvcServlet</servlet-name>
        <url-pattern>*.go</url-pattern>
      </servlet-mapping>
    </web-app>

Worth running mvn eclipse:eclipse at this stage if you are working in Eclipse. Also add a servlet bean file <servlet_name>-servlet.xml (e.g. SpringMvcServlet-servlet.xml):

  <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
  
      <!-- Enabling Spring beans auto-discovery -->
      <context:component-scan base-package="com.blizzardtec.controller" />
 
      <!-- Enabling Spring MVC configuration through annotations -->
      <mvc:annotation-driven />
 
      <!-- Defining which view resolver to use -->
      <bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
      </bean>
    </beans>

Add a controller that matches the package you defined in your servlet bean file (it doesn't matter what it is called as we will use annotations:

/**
 * Base Spring MVC controller.
 */
package com.blizzardtec.controller;

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

/**
 * @author Barnaby Golden
 *
 */
@Controller
public final class MyController {

    /**
     * Request handler.
     *
     * @return str
     */
    @RequestMapping(method = RequestMethod.GET, value = "/home")
    public String handleRequest() {
     return "welcome";
    }
}

Add a new folder:

src/main/webapp/WEB-INF/views

and put inside it a jsp file matching the return string (i.e. welcome.jsp):

<%@ 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">
<title>Spring MVC Maven Webapp</title>
</head>
<body>
A simple Spring MVC Maven web application.
</body>
</html>

 

Deploy to Tomcat and it should be accessible on the path:

http://localhost:8080/spring-mvc-webapp-0.0.1-SNAPSHOT/home.go

Source code for this Spring MVC Maven application can be found in this Github project.

In the second part of this post we add Cucumber.


 

Target Audience

© Copyright 2018 bhgagile Limited

Paperblog

Software Development Blogs - BlogCatalog Blog Directory