配置SpringMVC
dispatcherServlet-servlet.xml
在ssm-crud項目中
SpringMVC的配置主要是在dispatcherServlet-servlet.xml
文件
在這之前,先修改beans
的頭信息,否則按alt+/
快捷鍵沒有提示,並且添加context:component-scan
還報錯:context:component-scan is not bound,后來找到的解決方法:context:component-scan is not bound。
我試圖了解一下這些配置的信息,但是並沒有找到比較讓人清晰明了的文章,未來如果找到我會放在這里:地址(挖個坑)。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>
然后,我們在src/main/java
文件夾添加未來我們需要的文件夾:
- com.liantao.crud.bean
- com.liantao.crud.controller
- com.liantao.crud.dao
- com.liantao.crud.service
- com.liantao.crud.test
- com.liantao.crud.utils
點擊xml文件左下角的Namespaces
勾選上context
,beans
,如圖:
接着,dispatcherServlet-servlet.xml
文件添加:
<!-- SpringMVC的配置文件,包含網站跳轉邏輯的控制配置 -->
<context:component-scan base-package="com.liantao" use-default-filters="false">
<!-- 只掃描控制器 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
作用:讓SpringMVC只掃描com.liantao.crud.controller
包
先知道作用就夠了,這里有一篇<context:component-scan/>
的詳細介紹:context:component-scan
ViewResolver 視圖解析器
<!-- 視圖解析器,方便頁面返回 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
作用:簡單描述就是對Controller類中每個函數返回值那里的String類型前后加路徑
關於它的分析:ViewResolver
兩個標准配置
在Namespaces
勾選上mvc
<!-- 兩個標准配置 -->
<!-- 將SpringMVC不能處理的請求交給tomcat -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven>
<!-- 能支持SpringMVC更高級的一些功能,JSR303的校驗,快捷的ajax...映射動態請求 -->
</mvc:annotation-driven>
作用看注釋。
詳細分析看:SpringMVC處理靜態文件源碼分析
END