一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 手把手教你搭建SpringMVC框架——最小化配置

手把手教你搭建SpringMVC框架——最小化配置

2020-08-19 11:38xingoo Java教程

這篇文章主要介紹了手把手教你搭建SpringMVC框架——最小化配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

為什么需要Spring MVC

最開始接觸網(wǎng)頁的時候,是純的html/css頁面,那個時候還是用Dreamweaver來繪制頁面。

隨著網(wǎng)站開發(fā)的深入,開始學(xué)習(xí)servlet開發(fā),記得最痛苦的就是servlet返回網(wǎng)頁的內(nèi)容是字符串拼接的html頁面,整不好就無法顯示....

再到后來開學(xué)學(xué)習(xí)SSH,龐大的架構(gòu)眼花繚亂。Struts繁雜的標簽、hibernate搞不清楚的數(shù)據(jù)表,Spring不知道哪里搞錯的bean。

最后隨著發(fā)展,前端開始占有一席之地,nodejs風(fēng)生水起,很多業(yè)務(wù)邏輯開始前置。再也看不到當初的bo、dao了,取而代之的是各種框架的mvvm,后臺減輕壓力只負責一些必要的邏輯。

到現(xiàn)在,好像web開發(fā)又發(fā)展到了一個階段——前端由于Nodejs的作用,可以支撐一部分業(yè)務(wù)邏輯,通過轉(zhuǎn)發(fā)代理,統(tǒng)一發(fā)往后臺。后臺通過url實現(xiàn)mvc,對性持久化、更深入的邏輯操作等等。Spring MVC在這里就起了很關(guān)鍵的作用....它通過Url攔截請求,自定義業(yè)務(wù)邏輯,可以返回自定義的view或者模型數(shù)據(jù)。

當然,上面的鬼扯都是片面的,不代表行業(yè)的發(fā)展,只是博主管中窺豹而已。

下面步入正題,說說Spring MVC的最小化配置,給入門的朋友引個路。

Spring MVC的最小化配置

需要的jar包

  • Spring framework spring-context
  • Spring framework spring-mvc

具體可以參考maven中的引用:

?
1
2
3
4
5
6
7
8
9
10
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>

web.xml配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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>
    <!-- 默認是/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>
      <!-- 默認是/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,如上面所示,所有/開頭的請求,都會通過SpringMVC這個servlet進行處理。

他們都需要一個xml文件,默認位置上面已經(jīng)說過了。

applicationContext.xml

空的,反正咱也沒用什么bean。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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

里面放一個掃描controller的配置即可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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包 -->
  <context:component-scan base-package="hello" />
</beans>

controller文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package hello;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
@Controller
public class HelloController {
  
  @RequestMapping("/hello")
  public @ResponseBody String test() {
    return "hello, world! This com from spring!";
  }
 
}

總結(jié)一下:

1 兩個maven依賴,spring-context;spring-mvc。maven就會自動下載所有關(guān)聯(lián)的jar包,包括

  • spring-webmvc
  • spring-beans
  • spring-core
  • spring-expression
  • spring-web
  • spring-context
  • spring-aop
  • aopalliance
  • commons-logging

2 一個web.xml文件,配置了listener和servlet

3 兩個spring相關(guān)的文件,applicationContext.xml和servletName-servlet.xml

4 一個controller文件,配置了攔截的url處理代碼

有了這些準備工作,運行后輸入:http://localhost:8080/SpringTest/hello

就能得到

hello, world! This com from spring!

這樣的信息,恭喜你的SpringMVC搭起來了!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/xing901022/p/5240044.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜在线播放免费人成无 | 成人福利在线播放 | 亚洲午夜天堂 | www.一区二区三区.com | 国产午夜精品久久理论片小说 | 欧美成人中文字幕 | 国产外围 | 短篇同学新婚h系列小说 | 无人区大片免费播放器 | 国产欧美另类久久精品91 | 人妖女天堂视频在线96 | 欧美视频在线播放观看免费福利资源 | 羞羞视频免费观看网站 | 亚洲天堂.com| 国产极品精频在线观看 | 国产精品理论片在线观看 | 99视频在线观看免费视频 | 国产在线拍 | 王淑兰李思雨李铁柱乡村小说免费 | 国产美女久久精品香蕉69 | 国产色视频一区二区三区 | 2023最新伦理片 | 日本欧美不卡一区二区三区在线 | 五月天婷婷网亚洲综合在线 | 国产一区二区免费视频 | 成人影院在线观看 | 国产中文字幕 | 欧美成人免费一区在线播放 | 欧美一级特黄特色大片免费 | chinese一tk视频丨vk | 黑人同学征服教师麻麻 | 视频亚洲一区 | 欧美最猛性xxxxx男男 | 无套大战白嫩乌克兰美女 | 亚洲第一区se | 情人梁家辉在线 | 免费视频大全 | 日韩手机在线视频 | 日韩一区二区在线视频 | 给我免费观看的视频在线播放 | 国产女乱淫真高清免费视频 |