| Features | Languagues & Technologies | Enviroment setup | Main process | Additional process | JSP | Notes |
|---|
A simple web application to manage customer relationships, using Spring MVC and Hibernate, configured by XML
- View customers
- Add new customer
- Update customer
- Delete customer
- AOP Logging support
- Java
- XML
- MySQL
- Spring MVC
- Spring AOP
- Hibernate
- JSTL
- JSP
- CSS/JavaScript
-
Create new Dynamic Web Project in Eclipse
-
Create MySQL database in Workbench [create-database.sql]
-
Import jars to library: WEB-INF/lib
-
For Java 9+, include following jars in WEB-INF/lib
- javax.activation-1.2.0.jar
- jaxb-api-2.3.0.jar
- jaxb-core-2.3.0.jar
- jaxb-impl-2.3.0.jar
-
Test database connection (Project facets/runtimes/Tomcat): create a servlet [TestDatabaseServlet]
-
Include configuaration xml files (spring mvc and web) in WEB-INF.
-
Configure Spring + Hibernate in xml files [spring-mvc-crud-demo-servlet.xml]
- Define XML namespaces and schema locations
- Define database dataSource / connection pool
- Setup Hibernate session factory
- Setup Hibernate transaction manager
- Enable configuration of transctional annotation
- Add support for reading web resources: css, images, js, etc.
- Enable AspectJ Auto Proxy
-
Test Spring controller: create a Controller class with a method return to a new JSP file in 'view' folder.
- Create Customer class and map to database table using Hibernate.
- Create CustomerDAO interface.
- Create CustomerDAOImpl class with @Repository
- Inject the session factory with @Autowired
- Implement methods
- Get the current Hibernate session
- Create a query [org.hibernate.query.Query]
- Execute query and get result list
- Return result
- Create Service interface
- Create Service implementation with @Service
- Inject DAO with @Autowired.
- Implement methods with @Transactional.
- Delegate calls to DAO.
- Create/Update CustomerController
- Inject CustomerService using @Autowired
- Create a request method with @RequestMapping
- Get result from Service from DAO or create model attribute to bind form data
[CustomerController]
- Add result/attribute to the model
- Return JSP page
- Create/Update JSP pages
- Add support for JSTL Core tags:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- Add support for JSTL Core tags:
- Create Aspect class with @Aspect and @Component
[LoggingAspect]
[Reference]
- Add logger
- Setup pointcut declarations
- Add advices
- Prettify JSP page with CSS
- Place CSS in a 'resources' folder in WebContent.
- Configure Spring to serve up 'resources' folder.
- Reference CSS in the JSP.
- Configure welcome files in Spring XML to redirect from home page
- Create a JSP inside WebContent, match with name of welcome file in XML [index.jsp]
- Redirect to a desired address:
<% response.sendRedirect("address"); %>
- Refactor request method in the Controller using @GetMapping, @PostMapping
- Support for Spring MVC Form Tags:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> ${pageContext.request.contextPath}returns proper app name in JSP.
- Package for scanning must match in Spring configuration xml file.
- 'view' folder containning JSP files must match in Spring configuration xml file
- Run project: choose project root -> run as -> run on server.
- Use SessionFactory.saveOrUpdate(): combine save() and update() [CustomerDAOImpl]
- @RequestMapping (hanlde all methods), @GetMapping, @PostMapping
@RequestMapping(path="...", method=RequestMethod.GET)<=>@GetMapping(path="...")- Use "redirect:mapped_method" to redirect to a given method in the Controller instead of JSP page [CustomerController]
- Use @Transactional [org.springframework] to start and close transaction for a method [CustomerDAO]
- Use @Repository with DAO implementation to handle exception translation.
- Use @Service for Service implementation.
- Service Layer design pattern
- Define @Transactional at Service layer instead of DAO.
- Inject Service instead of DAO in the Controller.
- Controller <-> Service layer <-> DAO.
