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
- Reuse the schema named
junia_lab03
- We assume that your DB credentials are
root:root
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
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 withAppConfig
andDBConfig
, theses classes are provided by the core module.getServletConfigClasses()
which returns an array filled withWebConfig
, 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
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 thejunia.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 thesetResourceLoaderPath
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)
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 keycompanies
then returns"companiesList"
(it will load the provided template insrc/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
- path :
- it takes a parameter of type
Check the provided files in the resources
directory of that practice. Copy the files where they should be ;)
Deploy your app in a Tomcat then test your page!
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 thePOST
HTTP method.
Deploy your app in a Tomcat then test your form.
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.
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.