In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the SpringMVC to achieve Controller method of what related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this SpringMVC Controller method, let's take a look at it.
Three ways to realize Controller 1. Implement the Controller interface
Implement the Controller interface, override the handleRequest method, the ModelAndView object is a model view object that can not only add data, but also save page information, and handle requests by forwarding. This object needs to be divided into two parts.
Model and view. After forwarding to the next page, the data in model will be rendered to view for display. It can be obtained by using el expressions on the page. The scope of data in Model is request.
Import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @ className: PACKAGE_NAME.MyController1 * @ description: TODO * @ author: ych * @ create: 2021-06-02 20:25 * / public class MyController1 implements Controller {@ Override public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response) throws Exception {/ / instantiate ModelAndView object ModelAndView modelAndView = new ModelAndView () / play data, equivalent to request.setAttribute ("msg", "I am data"); modelAndView.addObject ("msg", "I am data"); / / play view modelAndView.setViewName ("forward:/WEB-INF/jsp/show.jsp"); return modelAndView;}}
Added to the Spring-mvc.xml configuration file, the following information
two。 Implement the HttpRequestHandler interface
Implement the HttpRequestHandler interface and override the handleRequest method. This implementation is basically the same as servlet.
Import org.springframework.web.HttpRequestHandler;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException / * @ className: PACKAGE_NAME.MyController2 * @ description: TODO * @ author: ych * @ create: 2021-06-02 20:28 * / public class MyController2 implements HttpRequestHandler {@ Override public void handleRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / share data to request request.setAttribute ("msg", "I am the data") / / request forwarding request.getRequestDispatcher ("/ WEB-INF/jsp/show.jsp") .forward (request, response);}}
Added to the Spring-mvc.xml configuration file, the following information
3. Full annotation
Full annotations, @ Controller annotations written in development must be scanned to become controllers. The scanning component manages the class with annotations in the header.
@ RequestMapping is an annotation that provides a request access path, such as @ RequestMapping ("/") added to UserController, which is a relative path relative to the entire program, so you can access this controller class directly under the project.
Add @ RequestMapping ("/ test.do") to the header of the method test, indicating that after accessing the controller class, add "/ test.do" to the access path to access the method.
There are many methods that can be defined in an annotation controller class. You only need to add different @ RequestMapping values to the method header to use these methods as different controllers, so annotation patterns are most commonly used in development.
Import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView / * * @ className: PACKAGE_NAME.MyController3 * @ description: TODO * @ author: ych * @ create: 2021-06-02 20:32 * / @ Controller@RequestMapping ("/") public class MyController3 {@ RequestMapping ("/ test1.do") public ModelAndView test () {/ / create model view object ModelAndView modelAndView = new ModelAndView (); / / add model data, which is equivalent to request.setAttribute ("msg", "helloworld") ModelAndView.addObject ("msg", "helloworld"); / / View modelAndView.setViewName ("forward:/WEB-INF/jsp/show.jsp"); return modelAndView;}}
The upscan component must be added to the spring-mvc.xml file:
On the Controller (Controller) Controller Controller of SpringMVC
The controller is responsible for parsing the user's request and converting it into a model
A controller can contain multiple methods in SpringMVC
How to configure Controller in SpringMVC
Implement the Controller interface
Annotations implement Controller
1. Implement Controller interface
1 、 web.xml
Springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc-servlet.xml 1 springmvc /
2 、 springmvc-servlet.xml
3. Create a Controller class
Package com.springmvc.controller;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Test1Controller implements Controller {public ModelAndView handleRequest (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {ModelAndView modelAndView = new ModelAndView (); modelAndView.addObject ("msg", "Controller"); modelAndView.setViewName ("test"); return modelAndView;}}
Register bean for Test1Controller in springmvc-servlet.xml
2. Annotations to implement Controller
1 、 web.xml
Springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc-servlet.xml 1 springmvc /
2 、 springmvc-servlet.xml
3 、 Test2Controller
/ / all methods in the annotated class will be parsed by the view parser if the return value is String and there is a specific page to jump to; @ Controller public class Test2Controller {@ RequestMapping ("/ T2") public String test2 (Model model) {model.addAttribute ("msg", "annotation implementation Controller"); return "test" }}
4 、 test.jsp
This is the end of the article Title$ {msg} on "what are the ways for SpringMVC to implement Controller?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what are the methods of SpringMVC to achieve Controller". If you want to learn more knowledge, you are welcome to follow the industry information channel.
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.