An overview of the
In the previous article, From SpringBoot to SpringMVC (Non-annotated), we moved away from the out-of-the-box and auto-configuration convenience of SpringBoot and went back to the days of naive SpringMVC development, but presented in a non-annotated way. This article is explained again in the form of annotations.
Welcome to My Personal Blog CodeSheep
Spring MVC architecture pattern
A typical Spring MVC request flow is shown in 12 steps:
-
- The user initiates a request, which is processed by the front-end controller DispatcherServlet
-
- The front-end controller looks up the Hander through the processor mapper, either based on XML or annotations
-
- The processor mapper returns the chain of execution
-
- The front-end controller requests the processor adapter to execute the Hander
-
- Processor adapter to execute handler
-
- When the processing is complete, the processor adapter is returned with a ModeAndView object containing the view name and model data
-
- The processor adapter returns the view name and model data to the front controller
-
- The front controller parses the view through the view parser
-
- The view resolver returns the real view to the front controller
-
- The front-end controller renders through the returned view and data
-
- Returns the rendered view
-
- The final view is returned to the user, generating a response
The whole process is clear and clear, and we will understand the whole process based on actual experiments.
Spring MVC project construction
The experimental environment is as follows:
- IntelliJ IDEA 2018.1 (Ultimate Edition)
- For SpringMVC v4.3.9. RELEASE
- Maven 3.3.9
Here I used IDEA to build a Maven-based SpringMVC project. The construction process will not be described again. Various clicks and next steps will finally create the project architecture as follows:
Add front-end controller configurations
With SpringMVC, all requests should be managed by SpingMVC, which intercepts all eligible requests on SpringMVC’s proprietary Servlet.
To do this we need to add SpringMVC’s front-end controller DispatcherServlet to web.xml:
<! -- SpringMvc front-end controller --> <servlet> <servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc-dispatcher.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>Copy the code
This configuration indicates that all urls that match. Action are handled by the MVC-Dispatcher Servlet
Write the Spring MVC core XML configuration file
As you can see from the previous configuration, the MVC-Dispatcher Servlet we define depends on the configuration file MVC-Dispatcher.xml, to which we need to add the following configuration in this step
- Add annotated processor adapters and processor mapper
A:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
Copy the code
Method 2:
<mvc:annotation-driven></mvc:annotation-driven>
Copy the code
Writing controller
Because of the annotated processor mapper and processor adapter, there is no need to configure any information in the XML or implement any interfaces, just add annotations.
@Controller
public class TestController {
private StudentService studentService = new StudentService();
@RequestMapping("/queryStudentsList")
public ModelAndView handleRequest( ) throws Exception {
List<Student> studentList = studentService.queryStudents();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("studentList",studentList);
modelAndView.setViewName("/WEB-INF/views/studentList.jsp");
return modelAndView;
}
}
class StudentService {
public List<Student> queryStudents() {
List<Student> studentList = new ArrayList<Student>();
Student hansonwang = new Student();
hansonwang.setName("hansonwang99");
hansonwang.setID("123456");
Student codesheep = new Student();
codesheep.setName("codesheep");
codesheep.setID("654321");
studentList.add(hansonwang);
studentList.add(codesheep);
returnstudentList; }}Copy the code
To enable the annotation’s processor mapper and processor adapter to find the annotation’s Controllor, there are two configurations:
Method 1: Declare Controllor’s corresponding bean in XML
<bean class="cn.codesheep.controller.TestController" />
Copy the code
Method 2: Use the scan configuration to scan all classes in a package and find all Handler controller classes that use the @controllor annotation
<context:component-scan base-package="cn.codesheep.controller"></context:component-scan>
Copy the code
Writing a view file
The view file here is a JSP file, the path as: / WEB – INF/views/studentList. JSP
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> < HTML > <head> <title> </head> <body> <h3> <table width="300px;"Border =1> <tr> < TD > Name </ TD > < TD > Student number </ TD > </tr> <c:forEach items="${studentList}" var="student" >
<tr>
<td>${student.name}</td>
<td>${student.ID}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Copy the code
The experimental test
Start the Tomcat server and type in your browser:
http://localhost:8080/queryStudentsList.action
Copy the code
Data rendering OK.
Remember after
Due to the limited ability, if there is a mistake or improper place, please also criticize and correct, study together!
- My Personal Blog: CodeSheep program sheep
- My six months of tech blogging