首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Spring MVC 入門總結:非註解與註解完整配置對比速查表

     編輯:Java 綜合教學

Spring MVC 入門總結:非註解與註解完整配置對比速查表

通過入門程序梳理 Spring MVC 四大核心組件(前端控制器、處理器映射器、處理器適配器、視圖解析器)的配置要點,提供非註解與註解兩套完整可用的配置模板,便於項目快速啟動與遷移。

核心組件配置要點速查

組件非註解模式註解模式(推薦)
前端控制器*.action 或 / (RESTful)同左
處理器映射器BeanNameUrlHandlerMappingRequestMappingHandlerMapping 或 <mvc:annotation-driven/>
處理器適配器SimpleControllerHandlerAdapterRequestMappingHandlerAdapter 或 <mvc:annotation-driven/>
視圖解析器InternalResourceViewResolver (JSP)同左

關鍵規則:註解映射器與適配器必須成對使用;<mvc:annotation-driven/> 自動註冊兩者並啟用眾多預設功能(JSON 轉換、參數綁定、JSR-303 等),實戰首選。

非註解完整配置:springmvc.xml

<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:context="https://www.springframework.org/schema/context"
       xmlns:mvc="https://www.springframework.org/schema/mvc"
       xsi:schemaLocation="https://www.springframework.org/schema/beans 
       https://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       https://www.springframework.org/schema/mvc 
       https://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       https://www.springframework.org/schema/context 
       https://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- Handler: Bean 名稱即 URL -->
    <bean name="/queryItems.action" class="com.iot.ssm.controller.ItemsController"/>

    <!-- 非註解映射器 -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!-- 非註解適配器 -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <!-- 視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>

註解完整配置:springmvc.xml(推薦模板)

<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:context="https://www.springframework.org/schema/context"
       xmlns:mvc="https://www.springframework.org/schema/mvc"
       xsi:schemaLocation="https://www.springframework.org/schema/beans 
       https://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       https://www.springframework.org/schema/mvc 
       https://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       https://www.springframework.org/schema/context 
       https://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 掃描 Controller -->
    <context:component-scan base-package="com.iot.ssm.controller"/>

    <!-- 一鍵啟用註解映射器、適配器、參數綁定、JSON轉換、JSR-303等 -->
    <mvc:annotation-driven/>

    <!-- 視圖解析器:配置 JSP 路徑前綴/後綴 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

遷移建議

  • 新項目直接使用註解模式 + <mvc:annotation-driven/>
  • 舊項目遷移時,可共存:保留非註解 Handler,新增註解 Controller
  • 視圖解析器建議統一配置 prefix/suffix,Controller 僅返回邏輯視圖名

收藏此文,新建 Spring MVC 項目即刻可用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved