Giter Site home page Giter Site logo

lab_004's Introduction

Lab 04

Intro

Several goals for this homework:

  • It is based on the previous practice (code + database)
  • You will create a MVC architecture with Spring WebMVC
  • You will write some logs

DB

  • Reuse the schema named junia_lab03
  • We assume that your DB credentials are root:root

lab04-web

Dependencies

Create a new Maven module called lab04-web. Its packaging is war

For this module, you have to declare, in the pom.xml file, the following dependencies.

groupId artifactId version
${project.groupId} lab04-core ${project.version}
org.springframework spring-webmvc 5.1.9.RELEASE
org.springframework spring-context-support 5.1.9.RELEASE
org.apache.velocity velocity 1.7
com.shield-solutions spring-velocity-adapter 1.0.0.RELEASE
org.webjars bootstrap 3.3.7-1
ch.qos.logback logback-classic 1.2.3

For this module, we won't write any web.xml file, because everything will be configured with Java

Initializer

In the junia.lab04.web package, create a class named Initializer which extends AbstractAnnotationConfigDispatcherServletInitializer (hey, that's what I call an accurate name!)

3 methods to implement:

  • getRootConfigClasses() which returns an array filled with AppConfig and DBConfig, theses classes are provided by the core module.
  • getServletConfigClasses() which returns an array filled with WebConfig, a class you will write later.
  • getServletMappings() which returns a String array with the following entry : "/"

If you wonder how to create arrays in Java, check this out : http://mathbits.com/MathBits/Java/arrays/Initialize.htm

WebConfig

In the junia.lab04.web.config package, create the WebConfig class which implements WebMvcConfigurer.

  • Annotate this class with @EnableWebMVC in order to start Spring WebMVC.
  • Annotate this class with @Configuration
  • Annotate this class with @ComponentScan and configure this annotation to tell Spring to check the junia.lab04.web.controller package.
  • Override the addResourceHandlers with the following code : registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/");
  • Declare a bean of type VelocityConfigurer
    • "/WEB-INF/velocity" is the path you should pass to the setResourceLoaderPath method of that bean.
  • Declare a bean of type VelocityViewResolver
    • ".vm" is the suffix configured in that bean (check the available methods of that bean)

CompanyController

In the junia.lab04.web.controller package, create the CompanyController class.

  • Annotate this class with @Controller
  • Inject the CompanyService
  • Create a method getListOfCompanies
    • it takes a parameter of type ModelMap
    • it returns a String
    • its implementation is quite simple, it loads all the companies from the DB with findAllWithProjects method from the service, puts it in the modelMap with the key companies then returns "companiesList" (it will load the provided template in src/main/webapp/WEB-INF/velocity/companiesList.vm)
    • annotate this method in order to map this method with the current HTTP Request info:
      • path : /list
      • verb : GET

Check the provided files in the resources directory of that practice. Copy the files where they should be ;)

Deployment

Deploy your app in a Tomcat then test your page!

Let's continue

CompanyController

Create a getForm method which returns a String and takes a ModelMap parameter

  • create a new instance of Company then stores it in the map.
  • return "companyForm"
  • add the correct annotation in order to map this method to the /form URL.

Create a submitForm method

  • it returns a String
  • it takes a Company parameter, annotated with @ModelAttribute("company")
  • it saves the company in the DB
  • it returns "redirect:list (remember the "redirect after submit" pattern?)
  • annotate this method with @RequestMapping with the /form path and the POST HTTP method.

Deployment

Deploy your app in a Tomcat then test your form.

Let's continue

CompanyController

Because you are a smart student, read the HTML template of the "trash" button then implement the deletion of a company, followed by a refresh of the list.

Logs

Inside the src/main/resources directory of the web module, paste the logback.xml file then check how the logs are formatted. Do not hesitate to play with this config to understand the concept of logging. You can also add some logging in your code.

lab_004's People

Contributors

charlesp-0 avatar awillemant avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.