The Front End

Once you have a model and business logic in place, you can develop a suitable front-end web application. When building your front end, use the Spring MVC framework to separate the model, the view, and the controller parts.

SAP Commerce provides a range of Accelerator storefronts. Accelerator gives you basic building blocks that you can use for developing sophisticated and responsive storefronts within specific commerce domains. But to illustrate the basics of how stores are constructed, you focus here on two aspects.

The Controller Class
The interface between the user action and the underlying model.
JSP Pages
Dynamic web pages for presenting data to the end user.

You have previously created the model aspect of your MVC architecture, now you create the view, the JSP pages, and the controller, the controller class.

Build a Web App Component Using Spring MVC

Develop a simple front-end store and get the basics for understanding how stores are constructed.

Prerequisites

The following acceptance test is included in your Hybris123Tests.java file.
public void testWebAppComponent() throws Exception {	
canLoginToHybrisCommerce();
navigateTo("https://localhost:9002/concerttours/bands");
waitFor("a","The Quiet");

navigateTo("https://localhost:9002/concerttours/bands/A007");
assertTrue( waitFor("p","English choral society specialising in beautifully arranged, soothing melodies and songs"));	
}
Before you begin this step, run the test to confirm that it fails by executing this command:
cd $HYBRIS_HOME_DIR/hybris123; mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testWebAppComponent test
cd %HYBRIS_HOME_DIR%\hybris123 & mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testWebAppComponent test

Procedure

  1. Stop SAP Commerce.
    cd $HYBRIS_HOME_DIR/hybris/bin/platform; ./hybrisserver.sh stop
    cd %HYBRIS_HOME_DIR%\hybris\bin\platform & hybrisserver.bat stop
  2. Create two new controller classes called concerttours.controller.BandController and concerttours.controller.TourController under the web/src folder in the concerttours extension.
    package concerttours.controller;
    import de.hybris.platform.catalog.CatalogVersionService;
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import concerttours.data.BandData;
    import concerttours.facades.BandFacade;
     
    @Controller
    public class BandController
    {
        private static final String CATALOG_ID = "concertToursProductCatalog";
        private static final String CATALOG_VERSION_NAME = "Online";
        private CatalogVersionService catalogVersionService;
        private BandFacade bandFacade;
        @RequestMapping(value = "/bands")
        public String showBands(final Model model)
        {
            final List<BandData> bands = bandFacade.getBands();
            model.addAttribute("bands", bands);
            return "BandList";
        }
        @RequestMapping(value = "/bands/{bandId}")
        public String showBandDetails(@PathVariable final String bandId, final Model model) throws UnsupportedEncodingException
        {
            catalogVersionService.setSessionCatalogVersion(CATALOG_ID, CATALOG_VERSION_NAME);
            final String decodedBandId = URLDecoder.decode(bandId, "UTF-8");
            final BandData band = bandFacade.getBand(decodedBandId);
            model.addAttribute("band", band);
            return "BandDetails";
        }
        @Autowired
        public void setCatalogVersionService(final CatalogVersionService catalogVersionServiceService)
        {
            this.catalogVersionService = catalogVersionServiceService;
        }
        @Autowired
        public void setFacade(final BandFacade facade)
        {
            this.bandFacade = facade;
        }
    }
    package concerttours.controller;
    import de.hybris.platform.catalog.CatalogVersionService;
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import concerttours.data.TourData;
    import concerttours.facades.TourFacade;
     
    @Controller
    public class TourController
    {
        private static final String CATALOG_ID = "concertToursProductCatalog";
        private static final String CATALOG_VERSION_NAME = "Online";
        private CatalogVersionService catalogVersionService;
        private TourFacade tourFacade;
        @RequestMapping(value = "/tours/{tourId}")
        public String showTourDetails(@PathVariable final String tourId, final Model model) throws UnsupportedEncodingException
        {
            catalogVersionService.setSessionCatalogVersion(CATALOG_ID, CATALOG_VERSION_NAME);
            final String decodedTourId = URLDecoder.decode(tourId, "UTF-8");
            final TourData tour = tourFacade.getTourDetails(decodedTourId);
            model.addAttribute("tour", tour);
            return "TourDetails";
        }
        @Autowired
        public void setCatalogVersionService(final CatalogVersionService catalogVersionServiceService)
        {
            this.catalogVersionService = catalogVersionServiceService;
        }
        @Autowired
        public void setFacade(final TourFacade facade)
        {
            this.tourFacade = facade;
        }
    }

    You use several Annotations in these classes. The @Controller annotation tells Spring to handle the created Class as a Spring Controller component.

    In BandController, you can see two methods that are annotated using the @RequestMapping annotation. This maps the paths /bands and /band/{bandcode} to the Controller methods. While the first method, the showBands method, is straightforward, the showBandDetails method also declares @PathVariable. The bandId parameter is automatically resolved by Spring. Both methods return simple Strings which will be picked up by the ViewResolver you declared. A returned String hello would therefore be mapped to WEB-INF/views/hello.jsp according to the setup.

    To clearly separate the View from the Model part, populate the passed Model object with the data you would like to retrieve in the view. Either the List of BandData or a single BandData instance will be part of the model passed to the View.

    You need to set the session catalog because flexible search constrains any search for catalog-aware items to catalogs listed in the session object.

    The TourController class is coded similarly.

    SAP Commerce 123 Interactive Shortcut: If you are unable to create the classes or you want to skip this step, replace <HYBRIS_HOME_DIR>/hybris/bin/custom/concerttours/src/concerttours/controller with the contents of <HYBRIS_HOME_DIR>/hybris123/src/main/webapp/resources/concerttours/src/concerttours/controller.

    ditto $HYBRIS_HOME_DIR/hybris123/src/main/webapp/resources/concerttours/src/concerttours/controller/BandController.java $HYBRIS_HOME_DIR/hybris/bin/custom/concerttours/src/concerttours/controller/BandController.java; ditto $HYBRIS_HOME_DIR/hybris123/src/main/webapp/resources/concerttours/src/concerttours/controller/TourController.java $HYBRIS_HOME_DIR/hybris/bin/custom/concerttours/src/concerttours/controller/TourController.java
    
    echo d | xcopy %HYBRIS_HOME_DIR%\hybris123\src\main\webapp\resources\concerttours\src\concerttours\controller %HYBRIS_HOME_DIR%\hybris\bin\custom\concerttours\src\concerttours\controller
  3. Create the BandList.jsp, BandDetails.jsp, and TourDetails.jsp files in web/webroot/WEB-INF/views
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!doctype html>
    <html>
        <title>Band List</title>
        <body>
            <h1>Band List</h1>
         <ul>
         <c:forEach var="band" items="${bands}">
            <li><a href="./${band.id}">${band.name}</a></li>
          </c:forEach>
          </ul>
        </body>
    </html>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <!doctype html>
    <html>
    <title>Band Details</title>
    <body>
        <h1>Band Details</h1>
        Band Details for ${band.name}
        <p>${band.description}</p>
        <p>Music type:</p>
        <ul>
            <c:forEach var="genre" items="${band.genres}">
                <li>${genre}</li>
            </c:forEach>
        </ul>
        <p>Tour History:</p>
        <ul>
            <c:forEach var="tour" items="${band.tours}">
                <li><a href="../tours/${tour.id}">${tour.tourName}</a>(number of concerts: ${tour.numberOfConcerts})</li>
            </c:forEach>
        </ul>
        <a href="../bands">Back to Band List</a>
    </body>
    </html>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
    <!doctype html>
    <html>
    <title>Tour Details</title>
    <body>
        <h1>Tour Details</h1>
        Tour Details for ${tour.tourName}
        <p>${tour.description}</p>
        <p>Schedule:</p>
        <table>
            <tr><th>Venue</th><th></th><th>Date</th></tr>
            <c:forEach var="concert" items="${tour.concerts}">
                <tr><td>${concert.venue}</td><td>${concert.type}</td><td><fmt:formatDate pattern="dd MMM yyyy" value="${concert.date}" /></td></tr>
            </c:forEach>
        </table>
        <a href="../bands">Back to Band List</a>
    </body>
    </html>

    SAP Commerce 123 Interactive Shortcut: If you are unable to create the List and JSP pages or you want to skip this step, replace <HYBRIS_HOME_DIR>/hybris/bin/custom/concerttours/web/webroot/WEB-INF/views with the contents of <HYBRIS_HOME_DIR>/hybris123/src/main/webapp/resources/concerttours/web/webroot/WEB-INF/views.

    mkdir -p $HYBRIS_HOME_DIR/hybris/bin/custom/concerttours/web/webroot/WEB-INF/views; cp $HYBRIS_HOME_DIR/hybris123/src/main/webapp/resources/concerttours/web/webroot/WEB-INF/views/* $HYBRIS_HOME_DIR/hybris/bin/custom/concerttours/web/webroot/WEB-INF/views
    echo d | xcopy %HYBRIS_HOME_DIR%\hybris123\src\main\webapp\resources\concerttours\web\webroot\WEB-INF\views %HYBRIS_HOME_DIR%\hybris\bin\custom\concerttours\web\webroot\WEB-INF\views
  4. Rebuild SAP Commerce with Ant.
    cd $HYBRIS_HOME_DIR/hybris/bin/platform; ant clean all
    cd %HYBRIS_HOME_DIR%\hybris\bin\platform & ant clean all
  5. Start SAP Commerce.
    cd $HYBRIS_HOME_DIR/hybris/bin/platform; ./hybrisserver.sh start
    cd %HYBRIS_HOME_DIR%\hybris\bin\platform & hybrisserver.bat start
  6. Navigate to https://localhost:9002/concerttours/bands and view the list of bands. You are now able to view the list of Bands as well as details on individual Bands, and details of their tours. Note that you don't have to restart the server if you create new or modify existing jsp pages. This can be very helpful in speeding up the development of your front end.
  7. Run the testWebAppComponent acceptance test again and confirm that it now passes.
    cd $HYBRIS_HOME_DIR/hybris123; mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testWebAppComponent test
    cd %HYBRIS_HOME_DIR%\hybris123 & mvn -Dtest=com.hybris.hybris123.runtime.tests.Hybris123Tests#testWebAppComponent test
  8. Commit the changes to your local Git repository.
    cd $HYBRIS_HOME_DIR/hybris; git add . ; git commit -m "Build a Web App Component Using Spring MVC" 
    cd %HYBRIS_HOME_DIR%\hybris & git add . & git commit -m "Build a Web App Component Using Spring MVC"