SpringMVC學習(二)–SpringMVC的開發過程
標籤(空格分隔): SpringMVC
1、環境搭建
配置web開發環境,導入對應的SpringMVC依賴的jar包或者在pom中添加Spring MVC的依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
2、配置前端控制器
在web.xml文件中配置DispatcherServlet。
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
說明:
load-on-startup:表示servlet隨服務啓動;
url-pattern:*.action的請交給DispatcherServlet處理。
contextConfigLocation:指定springmvc配置的加載位置,如果不指定則默認加
載WEB-INF/[DispatcherServlet 的Servlet 名字]-servlet.xml。
url-pattern:Servlet的攔截模式:
① 攔截固定後綴的url,比如設置爲 .do、.action, 例如:/user/add.action
此方法最簡單,不會導致靜態資源(jpg,js,css)被攔截。
②、攔截所有,設置爲/,例如:/user/add /user/add.action
此方法可以實現REST風格的url,很多互聯網類型的應用使用這種方式。
但是此方法會導致靜態文件(jpg,js,css)被攔截後不能正常顯示。需要特殊處理。
③、攔截所有,設置爲/*,此設置方法錯誤,因爲請求到Action,當action轉到jsp時再次被攔截,提示不能根據jsp路徑mapping成功。
3、SpringMVC的配置文件
1、映射器
HandlerMapping 負責根據request請求找到對應的Handler處理器及Interceptor攔截器,將它們封裝在HandlerExecutionChain 對象中給前端控制器返回
1.1、BeanNameUrlHandlerMapping
BeanNameUrl處理器映射器,根據請求的url與spring容器中定義的bean的name進行匹配,從而從spring容器中找到bean實例
<!—beanName Url映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- controller配置 -->
<bean name="/items1.action" id="itemList1"
class="cn.itcast.springmvc.controller.first.ItemList1"/>
根據/item1.action進行映射到指定的處理器中
1.2、SimpleUrlHandlerMapping
simpleUrlHandlerMapping是BeanNameUrlHandlerMapping的增強版本,它可以將url和處理器bean的id進行統一映射配置
<!—簡單url映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/items1.action">controller的bean id</prop>
<prop key="/items2.action">controller的bean id</prop>
</props>
</property>
</bean>
2、適配器
HandlerAdapter會根據適配器接口對後端控制器進行包裝(適配),包裝後即可對處理器進行執行,通過擴展處理器適配器可以執行多種類型的處理器,這裏使用了適配器設計模式
2.1、SimpleControllerHandlerAdapter
簡單控制器處理器適配器,要求所有的處理器Handler(Controller)所有實現了org.springframework.web.servlet.mvc.Controller 接口的Bean通過此適配器進行適配、執行。
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
控制層示例代碼:
public class ItemList1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//do same thing
}
2.2、HttpRequestHandlerAdapter
HttpRequestHandlerAdapter,http請求處理器適配器,所有實現了org.springframework.web.HttpRequestHandler 接口的Bean通過此適配器進行適配、執行
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
控制層代碼實現示例:
public class ItemList2 implements HttpRequestHandler {
@Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
...
}
從這個實現方法可以看出,這個方法和上面的不同在於,返回值並不是返回void。通過response進行設置返回的數據類型,例如:json
視圖解析器
<!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
InternalResourceViewResolver:支持JSP視圖解析
viewClass:JstlView表示JSP模板頁面需要使用JSTL標籤庫,所以classpath中必須包含jstl的相關jar 包;
prefix 和suffix:查找視圖頁面的前綴和後綴,最終視圖的址爲:
前綴+邏輯視圖名+後綴,邏輯視圖名需要在controller中返回ModelAndView指定,比如邏輯視圖名爲hello,則最終返回的jsp視圖地址 “WEB-INF/jsp/hello.jsp”
註解實現適配器(@Controller)和映射器(@RequestMapping)
SpringMVC提供註解實現適配器和映射器,大大簡化了開發工作量。默認都支持按這種模式開發。
1、組件掃描器
使用組件掃描器省去在spring容器聲明每個controller bean類的繁瑣。使用
<!-- 掃描controller註解,多個包中間使用半角逗號分隔 -->
<context:component-scan base-package="cn.itcast.springmvc.controller.first"/>
2、註解驅動配置
<mvc:annotation-driven>
springmvc使用上述配置自動加載RequestMappingHandlerMapping和RequestMappingHandlerAdapter,可用在springmvc.xml配置文件中使用該配置替代註解處理器和適配器的配置
3、適配器配置
註解式處理器映射器,對類中標記@ResquestMapping的方法進行映射,根據ResquestMapping定義的url匹配ResquestMapping標記的方法,匹配成功返回HandlerMethod對象給前端控制器,HandlerMethod對象中封裝url對應的方法Method
配置如下:
<!--註解映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
註解描述:
@RequestMapping:定義請求url到處理器功能方法的映射
4、映射器配置
註解式處理器適配器,對標記@ResquestMapping的方法進行適配
<!--註解適配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>