Spring MVC Controller is used to process user requests. The Controller is equivalent to the Action in Struts 1. Their implementation mechanism and running principle are similar.
Controller is an interface. It generally inherits the AbstrcatController directly and implements the handleRequestInternal method. The handleRequestInternal method is equivalent to the execute method of Struts 1.
Import org. springframework. web. servlet. modelAndView; import org. springframework. web. servlet. mvc. abstractController; public class CatController extends AbstractController {private ICatService catService; // setter, getter protected ModelAndView handleRequestInternal (HttpServletRequestrequest, HttpServletResponse response) throws Exception {String action = request. getParameter ("action"); if ("list ". equals (action) {returnthis. list (request, response) ;}} protected ModelAndView list (HttpServletRequestrequest, HttpServletResponse response) throws Exception {List
CatList = catService. listCat (); request. setAttribute ("catList", catList); returnnew ModelAndView ("cat/listCat ");}}
Spring MVC does not have built-in data encapsulation. developers can encapsulate data conversion code themselves.
The unique feature of Spring MVC lies in the processing of the view layer. HandleRequestInternal returns the ModelAndView object, which can be seen as an encapsulation of JSP objects. ModelAndIView directly accepts the path of the JSP page. For example, the parameter "cat/listCat" is only part of the JSP path, the actual complete path is "WEB-INF/jsp/cat/catList. jsp ", before and after the path is configured in the configuration file
In addition to the JSP path, ModelAndView can directly pass the Model object to the View layer, instead of placing it in the request, such as new ModelAndView ("cat/listCat", "cat", cat ), if multiple parameters are passed, you can use Map, as shown in figure
Map map = newHashMap();map.put("cat",cat);map.put("catList",catList);return new ModelAndView("cat/listCat",map);
Typically, a separate xml file such as a spring-action.xml is used to specifically configure web-related components
/WEB-INF/jsp/
. Jsp
CatController
Web. xml configuration
ContextConfigLocation
/WEB-INF/classes/applicationContext. xml,/WEB-INF/classes/spring-action.xml
Org. springframework. web. context. ContextLoaderListener
Spring
Org. springframework. web. servlet. DispatcherServlet
ContextConfigLocation
/WEB-INF/classes/spring-action.xml
1
Spring
*. Mvc
If a Controller needs to process multiple business logic, you can use MultiActionController, which is equivalent to DispatchAction in Struts 1 and can distribute different requests to different methods based on a specific parameter.
Import org. springframework. web. servlet. mvc. multiaction. multiActionController; public class CatController extends AbstractController {private ICatService catService; // setter, getter protected ModelAndView add (HttpServletRequestrequest, HttpServletResponse response) throws Exception {...... Returnnew ModelAndView ("cat/addCat");} protected ModelAndView list (HttpServletRequestrequest, HttpServletResponse response) throws Exception {List
CatList = catService. listCat (); request. setAttribute ("catList", catList); return new ModelAndView ("cat/listCat ");}}
Configuration to spring-action.xml
Action
List
CatController
CatMultiController