1. SpringMVC的Controller實現方式 SpringMVC實現Controller的方式主要有控制器實現方式與全註解實現方式,其中全註解實現方式是當前項目中比較常用的一種方式。 1.1.控制器實現方式 1.1.1. 實現Controller介面 創建一個類實現Controller介面 ...
1. SpringMVC的Controller實現方式
SpringMVC實現Controller的方式主要有控制器實現方式與全註解實現方式,其中全註解實現方式是當前項目中比較常用的一種方式。
1.1.控制器實現方式
1.1.1. 實現Controller介面
創建一個類實現Controller介面:
/** * 實現Controller方式一: * 實現一個Controller介面,實現handleRequest方法 * 並且在Springmvc的配置文件中配置這個bean,指定Demo1Controller的訪問路徑 */ public class Demo1Controller implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception { System.out.println("進入demo1Controller視圖Model實現方式..."); //創建ModelAndView對象 ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/views/demo1Controller.jsp"); return modelAndView; } }
配置applicationContext-mvc.xml文件,配置bean交給Spring來管理:
<?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"> <!--開啟靜態資源的訪問--> <mvc:default-servlet-handler /> <!--SpringMVC的配置文件:把控制器類交給Spring來管理--> <!--name:訪問的映射路徑--> <!--Controller實現方式一配置--> <bean name="/demo1Controller" class="cn.yif.controllerImpl01.Demo1Controller"></bean> </beans>
1.1.2. 實現HttpRequestHandler介面
創建一個類來實現HttpRequestHandler介面,實現handleRequest方法:
/** * 實現Controller方式二: * 實現一個HttpRequestHandler介面,實現handleRequest方法 * 並且在Springmvc的配置文件中配置這個bean,指定Demo2Controller的訪問路徑 */ public class Demo2Controller implements HttpRequestHandler { @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("進入demo2Controller視圖Model實現方式..."); //獲取參數 //request.getParameter("name"); //轉發 request.getRequestDispatcher("/WEB-INF/views/demo2Controller.jsp").forward(request, response); } }
配置applicationContext-mvc.xml文件,配置bean交給Spring來管理:
<?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"> <!--開啟靜態資源的訪問--> <mvc:default-servlet-handler /> <!--SpringMVC的配置文件:把控制器類交給Spring來管理--> <!--name:訪問的映射路徑--> <!--Controller實現方式二配置--> <bean name="/demo2Controller" class="cn.yif.controllerImpl02.Demo2Controller"></bean> </beans>
1.1.3. 普通類(POJO)註解實現
創建一個類,在類中可以提供多個method方法,使用@RequestMapping映射類路徑+方法路徑,,這樣一個類中就可以配置多個訪問路徑:
/** * 實現Controller方式三: * 普通類(POJO)和註解@RequestMapping * 配置訪問路徑:類路徑+方法路徑 * 這樣一個類中可以配置多個方法、多個方法url映射 * 同樣需要在applicationContext-mvc.xml中配置bean * 而且需要開啟SpringMVC的註解支持:識別並掃描類上面的註解@RequestMapping */ @RequestMapping("/demo3Controller") public class Demo3Controller { @RequestMapping("/add") public ModelAndView add(){ System.out.println("進入demo3Controller視圖Model的add方法..."); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/views/demo3Controller_add.jsp"); return modelAndView; } @RequestMapping("/del") public ModelAndView del(){ System.out.println("進入demo3Controller視圖Model的del方法..."); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/views/demo3Controller_del.jsp"); return modelAndView; } }
配置applicationContext-mvc.xml文件,配置bean交給Spring來管理,無需配置name訪問路徑,註意:必須配置開啟SpringMVC註解配置的掃描:
<?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"> <!--開啟靜態資源的訪問--> <mvc:default-servlet-handler /> <!--開啟SpringMVC對註解的支持--> <mvc:annotation-driven /> <!--SpringMVC的配置文件:把控制器類交給Spring來管理--> <!--Controller實現方式三配置:在Controller類中配置了url,這裡無需配置--> <bean class="cn.yif.controllerImpl03.Demo3Controller"></bean> </beans>
1.2.全註解實現方式
全註解實現方式較控制器實現方式簡單,而且只需要我們普通的一個Controller類上面添加@Controller與@RequestMapping註解即可,是一種項目中常見的註解配置方式。
主要的步驟如下:
① 創建一個普通的類,在類上面配置@Controller、@RequestMapping註解;
/** * 實現Springmvc全註解方式: * 寫一個普通的Controller類 * 無需在applicationContext-mvc.xml中配置bean,只需要使用@Controller告訴Spring這是一個bean * 通過@RequestMapping在類與方法上映射請求路徑,可以映射多個方法的請求路徑 */ @Controller @RequestMapping("/annoController") public class AnnotationController { @RequestMapping("/add") public ModelAndView add(){ System.out.println("進入AnnotationController中的add方法..."); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/views/annoController_add.jsp"); return modelAndView; } @RequestMapping("/del") public ModelAndView del(){ System.out.println("進入AnnotationController中的add方法..."); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/WEB-INF/views/annoController_del.jsp"); return modelAndView; } }
② 在applicationContext-mvc.xml中配置—開啟對SpringMVC註解的支持、配置包的掃描(具體到哪個路徑下去掃描@Controller)、相容Spring3.2版本的配置,具體配置如下:
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"> <!--開啟靜態資源的訪問--> <mvc:default-servlet-handler /> <!--開啟SpringMVC對註解的支持--> <mvc:annotation-driven /> <!-- 進行包的掃描,去看類上面是否有相應的標簽配置:包含@Component、@Controller、@Service、@Repository --> <context:component-scan base-package="cn.yif.controllerallannoImpl" /> <!-- 這個不是必須的(spring3.2版本前使用) 配上後相容性好 --> <context:annotation-config /> </beans>
③ 註意事項:還需要加上spring-aop.jar包,否則在處理註解映射時無法找到對應切麵的映射會拋出aop異常:
Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
解決方案,導入spring-aop-4.1.2.RELEASE.jar包即可。
對應方法訪問頁面/annoController/add與annoController/del: