激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

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

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

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

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

為什么需要Spring MVC

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

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

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

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

到現(xiàn)在,好像web開發(fā)又發(fā)展到了一個階段——前端由于Nodejs的作用,可以支撐一部分業(yè)務邏輯,通過轉(zhuǎn)發(fā)代理,統(tǒng)一發(fā)往后臺。后臺通過url實現(xiàn)mvc,對性持久化、更深入的邏輯操作等等。Spring MVC在這里就起了很關(guān)鍵的作用....它通過Url攔截請求,自定義業(yè)務邏輯,可以返回自定義的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">
  <!-- 設置使用注解的類所在的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)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲情视频| 成人宗合网 | 欧洲色阁中文字幕 | 精品一区二区三区免费爱 | www.成人免费 | 成人爱爱电影 | 激情综合在线观看 | 欧美成a人片在线观看久 | 黄色免费电影网址 | 亚洲电影在线播放 | 日日爱影院 | 欧美性黄 | 久久久精品视频免费看 | 免费一级欧美大片视频在线 | 国产流白浆高潮在线观看 | 欧美巨乳在线观看 | 午夜视频你懂的 | 成av在线 | 91成人久久 | 成人在线97| 国产麻豆久久 | 成人综合免费视频 | 精品1| 国产免费一区二区三区网站免费 | 国产a级网站 | 国产精品久久久久久模特 | 亚洲欧美一区二区三区在线观看 | 精品一区二区三区在线观看视频 | 欧美视频首页 | 91短视频版高清在线观看免费 | 亚洲精品久久久久久下一站 | 天天天干夜夜夜操 | 欧美精品一区二区三区在线 | 亚洲欧美国产高清va在线播放 | 亚洲国产精品久久久久婷婷老年 | 成人在线视频免费观看 | 毛片观看网址 | 视频一区二区精品 | 国产精品久久久免费观看 | 一级毛片播放 | 午夜视频在线观 |