javascript
SpringMVC学习笔记-新建工程及一些注意事项
1、學習網站
:B站狂神說
狂神說的文檔鏈接:https://mp.weixin.qq.com/s/8ddT6FD0Y4f3XdbEz0aqpQ
2、新建SpringMVC工程
(我的是IDEA2020.3)
(1)新建工程
(2)命名
(3)對新建的工程右鍵,選擇Add Frameworks Support,然后勾上
Web Application
(4)配置pom.xml
然后等待maven倉庫下載相關依賴
(5)到這里就差不多了,新建model也是一樣的步驟,不過工程里面pom.xml已經配置了,module里面的pom.xml可不單獨進行配置
(6)如果相關的依賴沒有下載下來,確認一下你是否配置了maven的路徑,一定要自己安裝mavencan倉庫哦,不要用默認的
兩圖中的Repositories地址要一致。
3、跑程序之前確定三件事
:
(1)確定maven倉庫已經導入了springmvc的依賴
(2)確定在發布的項目中,即是否在Project Structure->Artifacts中導入了相關的依賴
【注】:lib文件夾如果沒有就自己新建,然后點擊圖中的加號就可以導入相關依賴了
(3)是否部署了對應的Tomcat服務器
【注】注意是添加Server Tomcat,并且選擇的是local
4、由于Maven可能存在資源過濾的問題,我們將配置完善,在pom.xml中引入
<build><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xm1</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build>5.springmvc-servlet.xml
<?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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--自動掃描包,讓指定包下的注解生效,由IOC容器統一管理--><context:component-scan base-package="com.kuang.controller"/><!--讓SpringMVC不處理靜態資源(過濾),.css .js .htmL . mp3. mp4--><mvc:default-servlet-handler/><!--支持mvc注解驅動在spring中,一般采用@RequestMapping注解來完成映射關系要想使@RequestMapping注解生效必須向上下文中注冊@DefaultAnnotationHandlerMapping和一個AnnotationMethodHandlerAdapter實例這兩個實例分別在類級別和方法級別處理。而annotation-driven配置幫助我們自動完成上述兩個實例的注入。--><mvc:annotation-driven /><!-- 視圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver" ><!--前綴--><property name="prefix" value="/WEB-INF/jsp/" /><!--后綴--><property name="suffix" value=".jsp" /></bean> </beans>一般在實際項目開發中,直接寫下述內容即可,不會更改這部分的代碼
<?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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--自動掃描包,讓指定包下的注解生效,由IOC容器統一管理--><context:component-scan base-package="com.kuang.controller"/><mvc:default-servlet-handler/><mvc:annotation-driven /><!-- 視圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver" ><!--前綴--><property name="prefix" value="/WEB-INF/jsp/" /><!--后綴--><property name="suffix" value=".jsp" /></bean> </beans>6、使用springMVC必須配置的三大件
處理器映射器、處理器適配器、視圖解析器
通常,我們只需要手動配置視圖解析器,而處理器映射器和處理器適配器只需要開啟注解驅動即可,而省去了大段的xml配置
總結
以上是生活随笔為你收集整理的SpringMVC学习笔记-新建工程及一些注意事项的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自适应阈值算法(大津阈值法)
- 下一篇: JS判断数字类型