日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

(转)SpringMVC学习(三)——SpringMVC的配置文件

發布時間:2025/4/16 javascript 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转)SpringMVC学习(三)——SpringMVC的配置文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://blog.csdn.net/yerenyuan_pku/article/details/72231527

讀者閱讀過SpringMVC學習(一)——SpringMVC介紹與入門這篇文章后,想必都會寫寫SpringMVC的入門小程序,在這個小程序中,SpringMVC的核心配置文件——springmvc.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.itheima.springmvc.controller"/> </beans>

讀者可能懷疑這寫的不對啊!怎么可能只配這點東西呢?SpringMVC的三大組件哪去了,它們不是要配置嗎?且聽我慢慢講解。我們發現這幾個組件并沒配置,但卻是好使的,就是因為它有一個默認配置,DispatcherServlet.properties這個默認配置文件里面默認加載了,看圖:?

可以看出我們使用了注解方式的處理器映射器和處理器適配器。

  • 默認加載的注解方式的處理器映射器

    org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
  • 默認加載的注解方式的處理器適配器

    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
  • 默認加載的視圖適配器(默認解析JSP視圖的視圖解析器)

    org.springframework.web.servlet.view.InternalResourceViewResolver

我們如果使用默認加載的注解方式的映射器和適配器,那么對它們的可控制性是比較小的,所以一般來說,我們都是自己配置的,因為有的時候我們需要擴展一些其他的組件。

注解映射器和適配器

配置組件掃描器

使用組件掃描器可省去在Spring容器中配置每個Controller類的繁瑣。使用<context:component-scan>自動掃描標記@controller注解的控制器類,配置如下:

<context:component-scan base-package="com.itheima.springmvc.controller"/>

注意:如果要掃描多個包,多個包中間使用半角逗號分隔。很明顯在入門小程序中我已經配置了。

配置RequestMappingHandlerMapping

注解式處理器映射器,對類中標記@ResquestMapping注解的方法進行映射,根據@ResquestMapping注解定義的url匹配@ResquestMapping注解標記的方法,匹配成功返回HandlerMethod對象給前端控制器,HandlerMethod對象中封裝了url對應的方法Method。?
從Spring3.1版本開始,廢除了DefaultAnnotationHandlerMapping的使用,推薦使用RequestMappingHandlerMapping完成注解式處理器映射。配置如下:

<!-- 配置注解式處理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

推薦使用最新版本的注解式處理器映射器,如果你想對其擴展,可以在這個bean里面配置其他的屬性。?
@RequestMapping注解的描述:定義請求url到處理器功能方法的映射。

配置RequestMappingHandlerAdapter

注解式處理器適配器,對標記@ResquestMapping注解的方法進行適配。?
從Spring3.1版本開始,廢除了AnnotationMethodHandlerAdapter的使用,推薦使用RequestMappingHandlerAdapter完成注解式處理器適配。配置如下:

<!-- 配置注解式處理器適配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

推薦使用最新版本的注解式處理器適配器,如果你想對其擴展,可以在這個bean里面配置其他的屬性。?
當我們配置完注解式處理器映射器和注解式處理器適配器之后,SpringMVC的核心配置文件——springmvc.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.itheima.springmvc.controller"/> <!-- 配置注解式處理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> <!-- 配置注解式處理器適配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> </beans>

然后在瀏覽器地址欄中輸入url地址——http://localhost:8080/springmvc-first/itemList.action,回車,也能同樣看到如下效果:?

繼續優化注解,配置<mvc:annotation-driven>

使用注解要注意一個問題,就是注解適配器和映射器必須配對使用,也就是說,不能一個用注解,一個用非注解。要用一起用,要么都不用。其實在SpringMVC中還有更加簡便的注解,SpringMVC使用<mvc:annotation-driven>自動加載RequestMappingHandlerMapping和RequestMappingHandlerAdapter,可在springmvc.xml配置文件中使用<mvc:annotation-driven>替代注解處理器和適配器的配置,如下圖所示:?

注意:如果配置一個注解驅動之后,那么就可以不用配置處理器映射器和處理器適配器了。?
此時在瀏覽器地址欄中輸入url地址——http://localhost:8080/springmvc-first/itemList.action,回車,同樣也能看到如上效果。

配置視圖解析器

我們也可在springmvc.xml配置文件中自己手動配置視圖解析器,如下:

<!-- 配置視圖解析器(對jsp默認解析的視圖解析器) --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- prefix:前綴 --> <property name="prefix" value="/WEB-INF/jsp/"></property> <!-- suffix:后綴 --> <property name="suffix" value=".jsp"></property> </bean>
  • InternalResourceViewResolver:支持JSP視圖解析。
  • viewClass:JstlView表示JSP模板頁面需要使用JSTL標簽庫,所以classpath中必須包含jstl的相關jar 包。此屬性可以不設置,默認為JstlView。
  • prefix和suffix:查找視圖頁面的前綴和后綴,最終視圖的址為:前綴+邏輯視圖名+后綴,邏輯視圖名需要在Controller返回的ModelAndView中指定,比如邏輯視圖名為hello,則最終返回的jsp物理視圖地址就為 “WEB-INF/jsp/hello.jsp”。

這樣一來,ItemController類的代碼就要修改為:

@Controller public class ItemController { // .action可以省略 (請求的url地址) @RequestMapping("/itemList.action") public ModelAndView itemList() { // 查詢商品列表,使用靜態數據生成一個商品列表 List<Items> itemList = new ArrayList<Items>(); itemList.add(new Items(1, "imac", 20000, new Date(), "蘋果本很貴")); itemList.add(new Items(2, "imac1", 20000, new Date(), "蘋果本很貴")); itemList.add(new Items(3, "imac2", 20000, new Date(), "蘋果本很貴")); itemList.add(new Items(4, "imac3", 20000, new Date(), "蘋果本很貴")); itemList.add(new Items(5, "imac4", 20000, new Date(), "臥槽,蘋果本很貴啦!")); // 把商品列表傳遞給jsp ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("itemList", itemList); // 配置完視圖解析器之后只需要返回返回jsp的名稱即可 modelAndView.setViewName("itemList"); // 返回結果 return modelAndView; } }

如果這時返回全路徑,即/WEB-INF/jsp/itemList.jsp,那就不好使了。?
到這就基本總結完了SpringMVC中使用注解方式的適配器和映射器了,很明顯,開發中我們就使用注解配置,那樣非常方便。

總結

以上是生活随笔為你收集整理的(转)SpringMVC学习(三)——SpringMVC的配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。