CD part XI: Creating the Webapplication

Johan Tuitel

  • 20/01/2017
  • 5 minuten leestijd
Johan Tuitel

CD part XI: Creating the Webapplication

in this blog we will create a simple web application to show account status in a list

create the easy webapplication

source: https://www.mkyong.com/spring-mvc/spring-mvc-and-list-example/

generate default project

  1. navigate to the folder C:\projects
  2. execute this maven command to generate a default structure
    mvn archetype:generate -DgroupId=nl.com.devnl -DartifactId=easyclient -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

adjust the pom.xml for neccessary dependencies

     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>nl.com.devnl</groupId>
    <artifactId>easyclient</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Easy Project</name>

    <properties>
        <spring.version>3.2.2.RELEASE</spring.version>
        <jstl.version>1.2</jstl.version>
    </properties>

    <dependencies>

        <!-- included this dependency, which we use to get account status list from the webservice-->
        <dependency>
            <groupId>nl.com.devnl</groupId>
            <artifactId>AccountStatusWsClient</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <!-- Spring Core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
            <type>jar</type>
        </dependency>
    </dependencies>

    <build>
        <finalName>easy-webapp</finalName>
    </build>
    </project>

adjust configuration files

  1. adjust web.xml in the WEB-INF folder
        <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    </web-app>
  1. add mvc-dispatcher-servlet.xml in this folder
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan
        base-package="nl.com.devnl" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <mvc:annotation-driven />

    </beans>

create controller class

  1. create a class called EasyController
  2. package is nl.com.devnl
    package nl.com.devnl;

    import java.util.ArrayList;
    import java.util.List;
    import nl.com.devnl.client.api.AccountStatusClient;
    import nl.com.devnl.client.impl.AccountStatusClientImpl;
    import nl.com.devnl.entity.AccountStatus;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;

    @Controller
    public class EasyController {

        @RequestMapping(value = "/", method = RequestMethod.GET)
        public ModelAndView getdata() {
            List<AccountStatus> accountStatusList = getAccountStatusList();

            //return back to index.jsp
            ModelAndView model = new ModelAndView("index");
            model.addObject("accountStatusList", accountStatusList);

            return model;
        }

        private List<AccountStatus> getAccountStatusList() {
            List<AccountStatus> list = new ArrayList<AccountStatus>();
            AccountStatusClient asc = new AccountStatusClientImpl();
            list.addAll(asc.getAllAccountStatus());
            return list;
        }
    }

create webpage

  1. create a folder under the WEB-INF
  2. in this folder we create a index.jsp
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    <body>
        <h2>Spring MVC and Account Status List Example</h2>

        <c:if test="${not empty accountStatusList}">

            <ul>
                <c:forEach var="accountStatus" items="${accountStatusList}">
                    <li>${accountStatus.id} - ${accountStatus.name}</li>
                </c:forEach>
            </ul>

        </c:if>
    </body>
    </html>

test project

  1. navigate to c:\projects\easyclient
  2. execute maven command: mvn clean install
  3. start webservice application-server by executing tomcat ws develop - start.cmd batch-script in C:\tools\tomcat-scripts
  4. start webapplication application-server by executing tomcat client develop - start.cmd batch-script in C:\tools\tomcat-scripts
  5. now copy the easy-webapp.war file to C:\tools\tomcatwebappd\webapps folder. The application will be installed.
  6. open a browser and enter http://localhost:8073/easy-webapp/

easy webapp