springMVC--4種映射處理器handlerMapping

來源:互聯網
上載者:User

標籤:dap   def   dha   oca   div   model   simple   使用   beans   

  •  根據controller的name名稱來映射尋找controller:BeanNameUrlHandlerMapping  (預設)
1.1開啟該映射:預設是開啟的<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>1.2定義bean的name屬性:<bean id="helloController" name="/hello1.do" class="com.controller.HelloController"></bean>1.3介面訪問URL:與bean的name屬性值一致http://localhost:5080/springmvc/hello1.do
  • 根據URL來映射尋找controller:推薦使用:SimpleUrlHandlerMapping
2.1開啟該映射:<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><prop key="/hello2.do">HelloController</prop></props></property></bean>2.2定義bean:<bean id="helloController" class="com.controller.HelloController"></bean>2.3介面URL訪問:與prop的key屬性值一致http://localhost:5080/springmvc/hello2.do優點:將controller定義與映射分離開,耦合性低。
  • 根據controller的類名來映射尋找controller: ControllerClassNameHandlerMapping 
3.1開啟類名映射處理器:<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>3.2定義bean<bean id="hello3Controller" class="com.controller.Hello2Controller"></bean>3.3介面URL訪問:與bean的id屬性值無關。http://localhost:5080/springmvc/hello2.do   正確http://localhost:5080/springmvc/hello2Controller.do  正確http://localhost:5080/springmvc/hello21111.do  正確http://localhost:5080/springmvc/hello3.do 錯誤注意點:使用類名來訪問一個Controller,
  • 使用註解來映射尋找controller: DefaultAnnotationHandlerMapping

DefaultAnnotationHandlerMapping和RequestMappingHandlerMapping都是處理requestMapping的註解,
前者以廢棄,不建議使用,後者用來代替前者

1.開啟註解掃描組件:作用:當我載入設定檔時,到我們指定的包下掃描(讀取)Java檔案,尋找標註註解的地方(介面,類,方法或者其他),進行完成初始化。<beans><!--配置掃描使用註解的包路徑  --><context:component-scan base-package="com"></context:component-scan><!--開啟springMVC註解  --><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>  </beans>2.在類上標記:@Controller 聲明該類是一個springMVC Controller3.在方法上使用:@RequestMapping("/login") 聲明該方法處理哪一個請求eg:@Controllerpublic class Login {@RequestMapping("/login")public ModelAndView login(){ModelAndView mv = new ModelAndView(); mv.setViewName("login"); return mv;}}

 

springMVC--4種映射處理器handlerMapping

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.