SpringMVC的最小化配置說(shuō)明
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,如上面所示,所有/開(kāi)頭的請(qǐng)求,都會(huì)通過(guò)SpringMVC這個(gè)servlet進(jìn)行處理。
他們都需要一個(gè)xml文件,默認(rèn)位置上面已經(jīng)說(shuō)過(guò)了。
所配置的Servlet是SpringMVC的入口,可以contextConfigLocation參數(shù)來(lái)指定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>先空著反正啥也沒(méi)有
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è)置使用注解的類(lèi)所在的jar包 -->
<mvc:annotation-driven/>
<context:component-scan base-package="hello" />
</beans><mvc:annotation-driven/>是SpringMVC提供的一鍵式配置方法自動(dòng)做注冊(cè)組件之類(lèi)的事情
到此這篇關(guān)于SpringMVC的最小化配置說(shuō)明的文章就介紹到這了,更多相關(guān)SpringMVC最小化配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Hystrix的監(jiān)控及可視化面板
這篇文章主要介紹了關(guān)于Hystrix的監(jiān)控及可視化面板,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
SpringBoot中連接多個(gè)RabbitMQ的方法詳解
這篇文章主要介紹了SpringBoot中連接多個(gè)RabbitMQ的方法詳解,要實(shí)現(xiàn) SpringBoot 連接多個(gè) RabbitMQ,只能自定義重寫(xiě)一些東西,分別配置才可以,下面一起來(lái)走一下試試,需要的朋友可以參考下2023-10-10
java 實(shí)現(xiàn)截取字符串并按字節(jié)分別輸出實(shí)例代碼
這篇文章主要介紹了java 實(shí)現(xiàn)截取字符串并按字節(jié)分別輸出實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
VSCode搭建Java開(kāi)發(fā)環(huán)境的超詳細(xì)步驟
VSCode是一款多平臺(tái)的源代碼編輯器,支持多種編程語(yǔ)言,它輕量級(jí)、功能強(qiáng)大,通過(guò)豐富的插件生態(tài)系統(tǒng)可以支持更多語(yǔ)言和運(yùn)行時(shí),如C++、C#、Java、Python等,這篇文章主要介紹了VSCode搭建Java開(kāi)發(fā)環(huán)境的超詳細(xì)步驟,需要的朋友可以參考下2024-10-10
JVM教程之Java代碼編譯和執(zhí)行的整個(gè)過(guò)程(二)
這篇文章主要介紹了JVM學(xué)習(xí)筆記第二篇,關(guān)于Java代碼編譯和執(zhí)行的整個(gè)過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

