In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "brief introduction of annotations in Spring". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the brief introduction of annotations in Spring".
Core components of SpringMVC
DispatcherServlet-- > controller, request entry
HanderMapping--- > controller, request dispatch
Controller---- > Controller, request processing flow
ModelAndView---- > model, encapsulating business processing results and views
ViewResolver---- > view, view display processor
Processing flow
The browser sends a request to the Spting and the request is handed over to the front-end controller DispatcherServlet for processing.
The controller uses HanderMapping to find the appropriate Controller component to process the request.
The method of executing the Controller component convention handles the request, and the contract method invokes the model component to complete the business processing. The convention method can return a ModelAndView object that encapsulates the business processing result data and view name information.
After receiving the ModelAndView, the controller invokes the ViewResolver component, locates the View (JSP) and transmits the information to generate the response interface result.
Annotation configuration has many advantages over XML configuration:
It can make full use of the reflection mechanism of Java to obtain class structure information, which can effectively reduce the work of configuration. For example, when configuring ORM mapping with JPA annotations, we do not need to specify the attribute name, type, and other information of PO. If the relational table fields and PO attribute names and types 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 in the same file, while XML configuration uses a separate configuration file, most of the configuration information will not be adjusted after the program development is completed, if the configuration information and Java code are put together, it will help to enhance the cohesion of the program. With independent XML configuration files, programmers often need to switch between program files and configuration files when writing a function. This kind of thinking incoherence will reduce the development efficiency.
Controller component
The Controller component is responsible for executing the specific business processing, and needs to implement the Controller interface and the agreed methods when writing.
ModelAndView component
The Controller convention component then returns a ModelAndView object that 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 model is stored in the attribute of request
HanderMapping component
Through the HanderMapping component, the DispatcherServlet controller can map the http request to the Controller component.
-SimpleUserHandlerMapping maintains a mapping relationship between http request and Controller (map), which calls Controller according to the list correspondence.
SimpleUserHandlerMapping usage
LoginController 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 method to specify the corresponding customer http request.
ViewResolver component
All Controller components return an ModelAndView instance that encapsulates the view name, the view in Spring is identified by name, and the view parser ViewResolver parses the view by name.
InternalResourceViewResolver wrapper for Servlet and JSP, using examples:
For example, the view name hello can be mapped to / WEB-INF/hello.jsp through the above configuration
Controller comments
We typically declare Controller components using the @ Controller annotation, which makes it more flexible and doesn't have to implement the Controller interface as before.
@ Controllerpublic class HelloController {public String execute () {return "Hello";}}
A prerequisite for using the above annotations is to enable annotation scanning:
What is written in base-package is the package where the Controller component is located.
ResquestMapping comments
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, RequestMappingHandlerMapping (before class definition) and RequestMappingAdapter (before method definition), in spring's XML configuration file, such as (sping3.1 version needs to be defined):
The spring3.2 version does not need to be defined
Thank you for your reading, the above is the content of "brief introduction of annotations in Spring". After the study of this article, I believe you have a deeper understanding of the brief introduction of annotations in Spring, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.