1. 程式人生 > >搭建SpringMVC——最小化配置

搭建SpringMVC——最小化配置

很詳細的文章,轉載以留用。原文連結:https://www.cnblogs.com/xing901022/p/5240044.html

為什麼需要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中的引用:

<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配置

<?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。

<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的配置即可。

<?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檔案

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搭起來了!




相關推薦

搭建SpringMVC——配置

很詳細的文章,轉載以留用。原文連結:https://www.cnblogs.com/xing901022/p/5240044.html為什麼需要Spring MVC最開始接觸網頁的時候,是純的html/css頁面,那個時候還是用Dreamweaver來繪製頁面。隨著網站開發的

(轉)手把手教你搭建SpringMVC——配置

為什麼需要Spring MVC 最開始接觸網頁的時候,是純的html/css頁面,那個時候還是用Dreamweaver來繪製頁面。 隨著網站開發的深入,開始學習servlet開發,記得最痛苦的就是servlet返回網頁的內容是字串拼接的html頁面,整不好就無法顯示.

使用fdopen對python程序產生的檔案進行許可權配置

# 需求背景 用python進行檔案的建立和讀寫操作時,我們很少關注所建立的檔案的許可權配置。對於一些安全性較高的系統,如果我們建立的檔案許可權其他使用者或者同一使用者組裡的其他使用者有可讀許可權的話,有可能導致不必要的資訊洩漏的風險。因此,除了建立一個更加安全和隱私的個人環境之外(如容器環境等),我們還可以

CentOS7安裝配置IP

centos配置ipCentOS7配置IP:因為是我們安裝的CentOS操作系統是最小化安裝的,所以沒有圖形界面和一些軟件或插件的。IP也是沒有配置的我們需要手動配置IP,然後使用遠程終端通過這個IP去連接操作系統,在終端上操作要比在虛擬機裏操作方便。因為在虛擬機裏不能夠用鼠標復制粘貼或者用滾軸上下滑動界面,

安裝centos7後的簡單配置

修改主機名 -i sys ipad head enabled led 主機名 onf 1、修改網絡配置# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736 BOOTPROTO=static ONBOOT=yes IP

CentOS 7 安裝後的配置

rmi block ets 查看系統 1.8 sql 命令行 環境 www. 最小化安裝完成 CentOS 7 後,需要做些許配置,以便更好地使用。 配置網絡 首先配置本地網絡,vi /etc/sysconfig/network-scripts/ifcfg-eno*。 TY

搭建LNMP環境(基於安裝CentOS 6.5)

解壓 hive ima zlib RM opp 客戶 killall 宋體 本文檔主要說明在單臺服務器上手動安裝LNMP環境的操作步驟,本文檔使用的系統版本可能與您的實際使用版本不同,您可以根據實際情況選擇相應版本。一、本文檔LNMP環境版本說明:OS:最小化安裝CentO

Oracle_plsql_開發工具搭建客戶端

AD 9.png 客戶 href ack nec 使用 list back 一:資源下載獲取路徑:二:配置方法1:前提是安裝好plsql開發工具具體安裝步驟略2:配置 簡化版的客戶端工具。具體格式:可以參照下文來修改編寫使用。orcl_1521 = (DESCRIPTIO

Linux 安裝後IP的配置

ica 否則 修改 網卡 9.png yum too 打開 www linux最小化安裝後沒有ifconfig這個命令: yum install net-tools.x86_64 這樣就安裝了ifconfig命令。 使用ifconfig看看IP: 可以看到並沒有自動分

在VM中安裝CentOS 7後的網絡配置

avr img vmware ESS process protocol 相關 res 屬性 1、點擊虛擬網絡編輯器:2、選擇VMnet0為橋接模式,選擇自動或者網卡3、右鍵VMware Network Adapter 屬性4、選中VMware Bridge Protocol

linux CentOS7安裝環境靜默安裝Oracle11GR2數據庫(修改配置文件)

shmall file-max 數據庫 安裝 tex rap png img entos linux CentOS7最小化安裝環境靜默安裝Oracle11GR2數據庫(修改配置文件) 一、修改內核文件: vim /etc/sysctl.conf 按一次 "i&qu

centos 7安裝文字格式配置網絡卡資訊

最近使用文字模式最小化安裝系統發現一個問題手動配置網路,網路啟動正常,但是就是上不了網,ping閘道器都不行。搜尋了網上資源發現都是配置資訊,沒有披露如何啟動。下面給大家展示如何解決這個問題。 系統環境:centos 7.2在系統安裝好後先別急於配置網絡卡,我們先使用nmtui命令啟用網絡卡,再去配

centos 7安裝文本格式配置網卡信息

一個 sta 如何解決 work 連通性 測試 png 文本 但是 最近使用文本模式最小化安裝系統發現一個問題手動配置網絡,網絡啟動正常,但是就是上不了網,ping網關都不行。搜索了網上資源發現都是配置信息,沒有披露如何啟動。下面給大家展示如何解決這個問題。 系統環

CentOS安裝後IP的配置(圖形手動及DHCP獲取IP地址)

下面主講CentOS最小化安裝後IP的配置,即DHCP獲取IP地址、手動獲取靜態IP地址的方法。 一、CentOS最小化安裝後IP的配置(DHCP獲取IP地址) 圖形化Linux的DHCP好配置,我就不講了。主要講一下Linux最小化安裝後IP的配置。 linux最小化安裝後沒有ifco

CentOS7.5安裝之後的配置

我是最小化安裝的,安裝了之後很多基本使用配置沒有,接下來要做一些配置,如網路之類的,使系統可用。 1、使命令分頁顯示(1頁顯示不不下,又不能上翻頁)            xxx | more 2、檢視系統安裝了哪些軟體包   rpm -qa 3、配置網路  最小化安裝

Spring學習(4) Spring XML配置

Spring提供了幾種技巧,可以幫助我們減少XML的配置數量。 自動裝配(autowiring)有助於減少甚至消除配置元素和元素,讓Spring自動識別如何裝配Bean的依賴關係。 自動檢測(autodiscovery)比自動裝配更進了一步,讓Spring能夠

virtualbox 安裝centos7 配置雙網絡卡(nat和橋接)記錄----已經過期

[[email protected] ~]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1     link/loopback 00:00:00:00:00

hadoop環境搭建之 linux CENTOS6.5 安裝步驟

安裝VmwareWorkStation下載CentOS6.5 iso 映象檔案點選建立新的虛擬機器,選擇典型,點下一步選擇稍後安裝作業系統選擇Linux CentOS64為選擇安裝路徑和虛擬機器名稱分配50G磁碟空間, 選擇將虛擬磁碟拆分多個檔案選擇自定義硬體安裝過程先分配4

OpenStack 安裝配置(九):計算節點的服務安裝

      之前的篇章提到了對OpenStack控制節點的簡單安裝。並且已經可以進入視覺化介面。這樣的花控制節點的虛擬機器已經可以協同工作了。接下來我們需要給與他一個資源池,我們以後雲平臺的所有資源都將在資源池獲取。控制節點只是作為一個對雲平臺的管理節點。計算節點我們需要用

VMware下CentOS7安裝及配置

1、VMware建立虛擬機器 2、安裝CentOS7 選擇系統語言 磁碟分割槽規劃,選擇預設自動分割槽,也可手動分割槽: 設定網路和主機名: 這裡設定主機名為:cen