国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

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

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

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

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

為什么需要Spring MVC

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

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

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

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

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

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

下面步入正題,說說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的相關內容,并配置攔截的url,如上面所示,所有/開頭的請求,都會通過SpringMVC這個servlet進行處理。

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

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!";
  }
 
}

總結一下:

1 兩個maven依賴,spring-context;spring-mvc。maven就會自動下載所有關聯的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相關的文件,applicationContext.xml和servletName-servlet.xml

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

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

就能得到

hello, world! This com from spring!

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人欧美一区二区三区视频xxx | 蜜桃成人 | 中文字幕在线三区 | 中文字幕亚洲欧美日韩在线不卡 | 嫩草视频免费在线观看 | av影院在线观看 | 欧美极品一区二区 | 亚洲国产一区二区在线观看 | 国模爱爱 | www中文字幕 | 综州合另类 | 国产乱码精品一区二区三区中文 | 激情视频网站 | 亚洲欧美视频二区 | 久久精品国产一区二区三区不卡 | 99热在线精品免费 | 91国内外精品自在线播放 | 精品视频网站 | 亚洲国产一区二区在线观看 | 亚洲天堂高清 | 精品成人一区二区 | 午夜www| 欧美日韩电影一区二区三区 | 中文字幕av在线 | 精品九九 | 在线亚洲电影 | 97久久久| 久久国产免费 | 韩国一区二区视频 | 日韩视频在线观看 | 俺来也俺也啪www色 性色视频在线 | 精品国产乱码久久久久久1区2区 | 国产欧美日韩一区二区三区 | 91视频一88av | 国产精品久久久久久中文字 | 午夜视频在线观看网站 | 婷婷久| 综合自拍 | 日日夜夜精品免费视频 | 精久久久 | 五月婷婷丁香在线 |