Spring MVC Module

Source: Internet
Author: User

Spring MVC Module

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
     
                         
                  
     
    
         
         
               
         
         
               
         
  


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.