1. 程式人生 > >SpringMVC框架(1)之(1.2 入門程式—處理器對映器和處理器介面卡(註解方式))

SpringMVC框架(1)之(1.2 入門程式—處理器對映器和處理器介面卡(註解方式))

1.DispatcherServlet載入時會預設載入 DispatcherServlet.properties 檔案,目的是為了載入裡面的處理器對映器、處理器介面卡、檢視解析器等各個元件;(所以 springmvc.xml兩種處理器介面卡、兩種處理器介面卡、檢視解析器都可以省略;)

2.如果在 springmvc.xml中配置了以上元件,則以 springmvc.xml優先(即覆蓋掉預設的);

DispatcherServlet.properties
在這裡插入圖片描述

1. 註解對映器:

3.1 之前使用 DefaultAnnotationHandlerMapping 註解對映器
3.1 之後使用 RequestMappingInfoHandlerMapping 註解對映器:
    1. 需要在 Handler使用 @Controller標識這是一個控制器;
    2. 使用 @RequestMapping註解指定 Handler方法對應的 URL(URL可以與方法名不一致,但建議一致,方便開發維護);

2. 註解介面卡:

3.1 之前使用 AnnotationMethodHandlerAdapter 註解介面卡
3.1 之後使用 RequestMappingHandlerAdapter 註解介面卡(不要求Handler(即 Controller)實現介面了,但要求註解對映器和註解介面卡配對使用;)

要求註解對映器和註解介面卡要一起配對使用;

3. 註解開發的 Handler:

springmvc.xml 中配置註解對映器、註解介面卡後,開發註解 Handler:
1. 建立 3. ItemListController3.java 類,使用 @Controller、@RequestMapping註解;
2.

再在 springmvc.xml 檔案中配置註解開發的 Handler(即新增對應Handler(即 Controller)類的 bean);
3. 如果有多個 Controller類,則在 springmvc.xml 檔案中直接開啟註解掃描方式:

<context:component-scan base-package="com.asd"></context:component-scan>

4. 檢視解析器 ViewResolver:

根據邏輯檢視名解析成真正的檢視:
1. ItemListController3.java 類中
modelAndView.setViewName("/WEB-INF/jsp/itemsList.jsp")不寫全名,改為 modelAndView.setViewName(“itemsList”)


2. springmvc.xml 檔案中檢視解析器中的 bean新增< prperty>屬性標籤配置前後綴 (字首+邏輯檢視名+字尾)

 
 
2. springmvc.xml(使用註解後)

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 
	
	<!-- 3.註解開發的Handler -->
	//單個類時:<bean  class="com.asd.ItemListController3"></bean>
	<context:component-scan base-package="com.asd"></context:component-scan>

	<!-- 1.註解對映器 -->  
	<bean name="" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingInfoHandlerMapping">
	</bean>

	<!-- 2.註解介面卡 --> 
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

	<!-- 4.檢視解析器 --> 
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

2. springmvc.xml(使用註解前)

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 
	
	<!-- 3.配置Handler(因為使用BeanNameUrlHandlerMapping處理器對映器,name配置是url) -->
	<bean  id="itemsListController1" name="/itemsList.action" class="com.asd.ItemListController"></bean>
	<bean  id="itemsListController2" class="com.asd.ItemListController2"></bean>

	<!-- 1.處理器對映器 -->  //可以省略
	<!-- 法一:根據bean的name(自定義)查詢handler,將action的url配置在bean的name中 -->
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
	</bean>
	<!-- 法二:根據bean的name(自定義)查詢handler,將action的url配置在bean的name中 -->
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mapping">
			<props> <!-- < prop>標籤中 key:url;值:handler的id -->
				<prop key="/items1.action">itemsListController1</prop>
			  //<prop key="/items2.action">itemsListController1</prop>
			    <prop key="/items2.action">itemsListController2</prop>
			</props>
		</property>
	</bean>

	<!-- 2.處理器介面卡 --> //可以省略
	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
	<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>

	<!-- 4.檢視解析器 --> //可以省略
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

</beans>

3. ItemListController3.java
(不需要實現介面了,需要在 Handler即 Controller使用 @Controller 標識這是一個控制器;使用 @RequestMapping指定 Handler方法對應的 URL(URL可以與方法名不一致,但建議一致,方便開發維護);

@Controller
public class ItemListController3 {
       @RequestMapping("/queryItems")
	public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)throws Exception{
		//靜態資料(商品列表)
		List<Items> itemsList=new ArrayList<Items>();
		Items item1=new Items();
		item1.setName("筆記本");
		item1.setPrice(5000);
		item1.setDetail("膝上型電腦");

		Items item2=new Items();
		item2.setName("手機");
		item2.setPrice(5000);
		item2.setDetail("華為手機");

		itemsList.add(item1);
		itemsList.add(item2);

		ModelAndView modelAndView=new ModelAndView();
		//填充資料到 request域中
		modelAndView.addObject("items",itemsList);
		//檢視
		//指定轉發的jsp頁面
		//真正檢視名:modelAndView.setViewName("/WEB-INF/jsp/itemsList.jsp"); 
		modelAndView.setViewName("itemsList"); //邏輯檢視名
		return modelAndView;
	}
}

執行結果:
(URL:http: // localhost8080/專案名/queryItems.action)

(queryItems即為 Controller中 @ResultMapping註解指定的 url;)