1. Controller implementation of spring MVC
Spring MVC implements Controller mainly in the way of Controller implementation and full annotation implementation, among which full annotation implementation is a common way in current projects.
1.1. Controller implementation mode
1.1.1. Implement Controller interface
Create a class to implement the Controller interface:
/** * Implementation mode 1 of Controller: * Implement a Controller interface and handleRequest method * And configure the bean in the configuration file of spring MVC to specify the access path of Demo1Controller */ public class Demo1Controller implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception { System.out.println("Get into demo1Controller view Model Realization way..."); //Establish ModelAndView object ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/views/demo1Controller.jsp"); return modelAndView; } }
Configure the applicationContext-mvc.xml file, and submit the configuration bean to Spring for management:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--Enable access to static resources--> <mvc:default-servlet-handler /> <!--SpringMVC Configuration file of: give the controller class to Spring To manage--> <!--name: Map path accessed--> <!--Controller Implementation mode 1 configuration--> <bean name="/demo1Controller" class="cn.yif.controllerImpl01.Demo1Controller"></bean> </beans>
1.1.2. Implement HttpRequestHandler interface
Create a class to implement the HttpRequestHandler interface and the handleRequest method:
/** * Implementation mode 2 of Controller: * Implement an HttpRequestHandler interface and the handleRequest method * And configure the bean in the configuration file of spring MVC to specify the access path of Demo2Controller */ public class Demo2Controller implements HttpRequestHandler { @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Get into demo2Controller view Model Realization way..."); //Get parameters //request.getParameter("name"); //Forward request.getRequestDispatcher("/WEB-INF/views/demo2Controller.jsp").forward(request, response); } }
Configure the applicationContext-mvc.xml file, and submit the configuration bean to Spring for management:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--Enable access to static resources--> <mvc:default-servlet-handler /> <!--SpringMVC Configuration file of: give the controller class to Spring To manage--> <!--name: Map path accessed--> <!--Controller Implementation mode 2 configuration--> <bean name="/demo2Controller" class="cn.yif.controllerImpl02.Demo2Controller"></bean> </beans>
1.1.3. POJO annotation implementation
Create a class. Multiple method methods can be provided in the class. Use @ RequestMapping to map the class path + method path. In such a class, multiple access paths can be configured:
/** * Implement Controller mode 3: * POJO and annotation @ RequestMapping * Configuration access path: classpath + method path * Such a class can be configured with multiple methods, multiple method url mappings * You also need to configure bean s in applicationContext-mvc.xml * You need to enable the annotation support of spring MVC: identify and scan the annotation @ RequestMapping on the class */ @RequestMapping("/demo3Controller") public class Demo3Controller { @RequestMapping("/add") public ModelAndView add(){ System.out.println("Get into demo3Controller view Model Of add Method..."); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/views/demo3Controller_add.jsp"); return modelAndView; } @RequestMapping("/del") public ModelAndView del(){ System.out.println("Get into demo3Controller view Model Of del Method..."); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/views/demo3Controller_del.jsp"); return modelAndView; } }
Configure the applicationContext-mvc.xml file. The configuration bean is handed over to Spring for management. There is no need to configure the name access path. Note: you must configure to enable the scanning of Spring MVC annotation configuration:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--Enable access to static resources--> <mvc:default-servlet-handler /> <!--open SpringMVC Support for annotations--> <mvc:annotation-driven /> <!--SpringMVC Configuration file of: give the controller class to Spring To manage--> <!--Controller Implementation mode 3 configuration: in Controller Class is configured url,No configuration required here--> <bean class="cn.yif.controllerImpl03.Demo3Controller"></bean> </beans>
1.2. Full annotation implementation
The full annotation implementation method is simpler than the Controller implementation method, and we only need to add @ Controller and @ RequestMapping annotation on a common Controller class. It is a common annotation configuration method in projects.
The main steps are as follows:
① Create a common class, and configure @ Controller, @ RequestMapping annotation on the class;
/** * Realize the spring MVC full annotation mode: * Write a common Controller class * Instead of configuring beans in applicationContext-mvc.xml, just use @ Controller to tell Spring that this is a bean * By @ RequestMapping, you can map the request path of multiple methods on the class and method */ @Controller @RequestMapping("/annoController") public class AnnotationController { @RequestMapping("/add") public ModelAndView add(){ System.out.println("Get into AnnotationController Medium add Method..."); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/views/annoController_add.jsp"); return modelAndView; } @RequestMapping("/del") public ModelAndView del(){ System.out.println("Get into AnnotationController Medium add Method..."); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/views/annoController_del.jsp"); return modelAndView; } }
② Configure in applicationContext-mvc.xml - enable support for spring MVC annotation, scan configuration package (specific path to scan @ Controller), and be compatible with spring 3.2. The specific configuration is as follows:
applicationContext-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--Enable access to static resources--> <mvc:default-servlet-handler /> <!--open SpringMVC Support for annotations--> <mvc:annotation-driven /> <!-- Scan package,See if there is a corresponding label configuration on the class: include@Component,@Controller,@Service,@Repository --> <context:component-scan base-package="cn.yif.controllerallannoImpl" /> <!-- This is not necessary(spring3.2 Use before version) Good compatibility after matching --> <context:annotation-config /> </beans>
③ Note: you also need to add the spring-aop.jar package. Otherwise, when processing annotation mapping, you cannot find the mapping corresponding to the facet, and an AOP exception will be thrown:
Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
Solution, just import the spring-aop-4.1.2.RELEASE.jar package.
Corresponding method access page / annoController/add and annoController/del: