歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring MVC框架搭建配置教程

Spring MVC框架搭建配置教程

日期:2017/3/1 9:17:47   编辑:Linux編程

為什麼需要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+Spring3+Hibernate4開發環境搭建 http://www.linuxidc.com/Linux/2013-07/87119.htm

Spring MVC整合Freemarker基於注解方式 http://www.linuxidc.com/Linux/2013-02/79660.htm

基於注解的Spring MVC簡單介紹 http://www.linuxidc.com/Linux/2012-02/54896.htm

Spring MVC 框架搭建及詳解 http://www.linuxidc.com/Linux/2012-01/52740.htm

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

本文要用的源碼下載到Linux公社1號FTP服務器下載(有更多您喜歡的)

------------------------------------------分割線------------------------------------------

FTP地址:ftp://ftp1.linuxidc.com

用戶名:ftp1.linuxidc.com

密碼:www.linuxidc.com

在 2016年LinuxIDC.com\3月\Spring MVC框架搭建配置教程\

下載方法見 http://www.linuxidc.com/Linux/2013-10/91140.htm

------------------------------------------分割線------------------------------------------

Copyright © Linux教程網 All Rights Reserved