SpringMVC的最小化配置說明
SpringMVC的配置步驟
1.在web.xml中配置Servlet
2.創(chuàng)建SpringMVC的xml配置文件
3.創(chuàng)建Controller和view
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
<!-- 默認(rèn)是/WEB-INF/applicationContext.xml -->
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>
<!-- 默認(rèn)是/WEB-INF/[servlet名字]-servlet.xml -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>其中,必要的配置就是指定servlet和listener.
- ContextLoaderListener指定了IOC容器初始化的方法
- DispatcherServlet則定義了mvc的相關(guān)內(nèi)容,并配置攔截的url,如上面所示,所有/開頭的請(qǐng)求,都會(huì)通過SpringMVC這個(gè)servlet進(jìn)行處理。
他們都需要一個(gè)xml文件,默認(rèn)位置上面已經(jīng)說過了。
所配置的Servlet是SpringMVC的入口,可以contextConfigLocation參數(shù)來指定SpringMVC的配置文件
如不指定默認(rèn)使用--servlet.xml文件
application.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
</beans>先空著反正啥也沒有
SpringMVC-servlet.xml
里面放一個(gè)掃描controller的配置即可。
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 設(shè)置使用注解的類所在的jar包 -->
<mvc:annotation-driven/>
<context:component-scan base-package="hello" />
</beans><mvc:annotation-driven/>是SpringMVC提供的一鍵式配置方法自動(dòng)做注冊(cè)組件之類的事情
到此這篇關(guān)于SpringMVC的最小化配置說明的文章就介紹到這了,更多相關(guān)SpringMVC最小化配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中Object類常用的12個(gè)方法(小結(jié))
Java 中的 Object 方法在面試中是一個(gè)非常高頻的點(diǎn),本文主要介紹了Java中Object類常用的12個(gè)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
分析java并發(fā)中的wait notify notifyAll
一個(gè)線程修改一個(gè)對(duì)象的值,而另一個(gè)線程則感知到了變化,然后進(jìn)行相應(yīng)的操作,這就是wait()、notify()和notifyAll()方法的本質(zhì)。本文將詳細(xì)來介紹它們概念實(shí)現(xiàn)以及區(qū)別2021-06-06
StreamAPI多次消費(fèi)一個(gè)stream代碼實(shí)例
這篇文章主要介紹了StreamAPI多次消費(fèi)一個(gè)stream代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Java Agent入門學(xué)習(xí)之動(dòng)態(tài)修改代碼
這篇文章主要給大家分享了Java Agent入門學(xué)習(xí)之動(dòng)態(tài)修改代碼的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07
java通過jni調(diào)用opencv處理圖像的方法
今天小編就為大家分享一篇java通過jni調(diào)用opencv處理圖像的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
springboot 使用logback啟動(dòng)報(bào)警報(bào)錯(cuò)的解決
這篇文章主要介紹了springboot 使用logback啟動(dòng)報(bào)警報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
基于SpringBoot接口+Redis解決用戶重復(fù)提交問題
當(dāng)網(wǎng)絡(luò)延遲的情況下用戶多次點(diǎn)擊submit按鈕導(dǎo)致表單重復(fù)提交,用戶提交表單后,點(diǎn)擊瀏覽器的【后退】按鈕回退到表單頁(yè)面后進(jìn)行再次提交也會(huì)出現(xiàn)用戶重復(fù)提交,辦法有很多,我這里只說一種,利用Redis的set方法搞定,需要的朋友可以參考下2023-10-10

