The core components of SpringMVC
DispatcherServlet―>Controller, request entry
HanderMapping-->Controller, request distribution
Controller―――>Controller, request processing process
ModelAndView--->Model, encapsulate business processing results and views
ViewResolver--->View, view display processor
Processing process
The browser issues a request to Spting and handes the request to the front-end controller DispatcherServlet for processing.
The controller finds the corresponding Controller component to handle the request through HanderMapping.
Execute the method agreed by the Controller component to process the request, and call the model component to complete the business processing in the convention method. The convention method can return a ModelAndView object, which encapsulates the business processing result data and view name information.
After the controller receives the ModelAndView, it calls the ViewResolver component, locates the View (JSP) and passes information to generate the response interface result.
Comment configuration has many advantages over XML configuration:
It can make full use of Java's reflection mechanism to obtain class structure information, which can effectively reduce configuration work. If you use JPA annotation to configure ORM mapping, we do not need to specify the attribute name, type and other information of the PO. If the relationship table field and the PO attribute name and type are the same, you do not even need to write task attribute mapping information - because this information can be obtained through the Java reflection mechanism.
Comments and Java code are located in the same file, while XML configurations use independent configuration files. Most configuration information will not be adjusted after the program is developed. If the configuration information and Java code are put together, it will help enhance the cohesion of the program. Using independent XML configuration files, programmers often need to switch between program files and configuration files when writing a function. This inconsistency in thinking will reduce development efficiency.
Controller Component
The Controller component is responsible for performing specific business processing, and it is necessary to implement the Controller interface and agreed methods during writing.
ModelAndView Component
The Controller convention component will return a ModelAndView object, which encapsulates the business processing result model data and view information.
The ModelAndView constructor is as follows:
-ModelAndView(StringviewName)
-ModelAndView(StringviewName,Mapmodel)
where viewName is the name of the jsp page, and the data of the model is stored in the request attribute
HanderMapping Component
Through the HanderMapping component, the DispatcherServlet controller can map http requests to the Controller component.
-SimpleUserHandlerMapping maintains an http request and Controller mapping relationship (map) to call Controller according to the list correspondence relationship.
SimpleUserHandlerMapping
<bean> <property name="mappings"> <props> <prop key="/login.form">loginController</prop> <props > </prop key="/hello.form">helloController</prop> </property></bean><bean id="helloController"/>
The helloController in the SimpleUserHandlerMapping tag above corresponds to the component whose id is helloController in the bean tag below.
RequestMappingHandlerMapping
ResquestMappingHandlerAdapter
Use the @ResquestMapping annotation on the Controller class and methods to specify the corresponding client http request.
ViewResolver component
All Controller components return a ModelAndView instance, encapsulating the view name, the view in Spring is identified by the name, and the view resolver ViewResolver resolves the view through the name.
InternalResourceViewResolver wraps Servlets and JSPs, using examples:
<bean id="jspViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/></bean>
For example: the view name hello can be mapped to /WEB-INF/hello.jsp through the above configuration
Controller annotation
We generally use the @Controller annotation to declare the Controller component, which makes it more flexible and you don't need to implement the Controller interface like before.
@Controllerpublic class HelloController{ public String execute(){ return "Hello"; }}There is a prerequisite for using the above annotation, which is to enable annotation scanning:
<context:component-scan base-package="org.test.controller">
The base-package writes the package where the Controller component is located.
ResquestMapping annotation
ResquestMapping indicates which request this class or method corresponds to.
@Controller@ResquestMapping("/test1")public class HelloController{ @ResquestMapping("/hello.form") public String helloExecute(){ return "Hello"; }}If you need to use RequestMapping, you need to define two bean components in the spring XML configuration file RequestMappingHandlerMapping (before class definition) and RequestMappingAdapter (before method definition), for example (required to be defined in the spring version 3.1):
<bean/><bean/>
Spring 3.2 version does not need to be defined
<mvc:annotation-driven/>
Summarize
The above is the brief introduction of the notes on Spring in this article, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!