In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to configure springmvc processor mapper and adapter". In daily operation, I believe many people have doubts about how to configure springmvc processor mapper and adapter. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to configure springmvc processor mapper and adapter". Next, please follow the editor to study!
(1) unannotated processor mappers and adapters
Processor mapper
The first non-annotated mapper
Another non-annotated mapper
Multiple mappers can also coexist, and the DispatcherServlet (front-end controller) specifies which mapper to handle the URL, as follows:
Items1
Items2
Processor adapter
The first non-annotated adapter
(required to write Handler to implement the Controller interface)
Public class ItemsController implements Controller {
Public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response)
Throws Exception {
ModelAndView modelAndView = new ModelAndView ()
ModelAndView.setViewName ("view/list")
ModelAndView.addObject ("name", "Zhang San")
Return modelAndView
}
}
Another non-annotated adapter
(required to write Handler to implement the HttpRequestHandler interface)
Public class ItemsController implements HttpRequestHandler {
Public void handleRequest (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {List itemsList = new ArrayList (); Items items = new Items (); items.setName ("Muxia"); itemsList.add (items); httpServletRequest.setAttribute ("list", itemsList) HttpServletRequest.getRequestDispatcher ("view/list") .forward (httpServletRequest,httpServletResponse);}}
Here you can use response to set the response format, character encoding, etc.
Response.setCharacterEncoding ("utf-8")
Response.setContentType ("application/json;charset=utf-8")
(2) Annotation-based processor mappers and adapters
Annotation mapper
Annotation adapter
The annotated mapper and the annotated adapter must be paired. Spring has a more simplified configuration for it, and you can use the following code instead of the above two configurations
The use of annotated mappers and annotated adapters varies greatly in the implementation of specific Java code, eliminating the need to implement specific interfaces and simplifying the code style.
@ Controllerpublic class ItemsController {
@ RequestMapping ("/ items")
Public ModelAndView items () throws Exception {List itemsList = new ArrayList (); Items items = new Items (); items.setName ("Muxia"); itemsList.add (items); ModelAndView modelAndView = new ModelAndView (); modelAndView.addObject ("list", itemsList); modelAndView.setViewName ("view/list"); return modelAndView;}}
Two annotations are used here, which are the most basic ones.
The @ Controller modifier class is used to identify that it is a controller.
@ RequestMapping ("") modifies a method or class, which means that the implementation maps items methods to url, with a method corresponding to a url. Note that the name in @ RequestMapping (") is generally the same as the method, but not required.
Finally, you need to load Handler in the spring container and specify the scan controller.
Here, you need to load Handler in the spring container for all the controller classes used, but in actual development, all Controller is usually placed under a package, so you can use the scanning component to scan the Controller package file.
Expansion
Annotated processor mappers and adapters are slightly different before and after spring3.1, and are now generally used after spring3.1, but have a general understanding of previous needs to avoid looking confused after seeing them in old projects.
Use (Annotation Mapper) before spring3.1
Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
Use (Annotation Mapper) after spring3.1
Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
Use before spring3.1 (Annotation Adapter)
Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
Use after spring3.1 (Annotation Adapter)
Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
At this point, the study on "how to configure springmvc processor mappers and adapters" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.