欧美日韩国产一区,亚洲一区视频,色综合久久久久,私密按摩师舌头伸进去了,99re6这里只有精品,夜夜性日日交xxx性hd

java框架(2.0)SpringMVC的搭建

  • • 發表于 8年前
  • • 作者 大妖怪
  • • 4352 人瀏覽
  • • 11 條評論
  • • 最后編輯時間 8年前
  • • 來自 [技 術]

原創聲明:本文為作者原創,未經允許不得轉載,經授權轉載需注明作者和出處

接下來正式開始我們的框架之旅,由于本人一直都認的Spring和Mybatis這兩個爹,所以接下來的重心會以這兩個框架為主,Struts和Hibernate等相同功能的框架就不做重點了。
之前講過,所謂的框架就是為了處理某一個或者某些復雜的問題而去編寫的一個可復用的設計結構。那么我們來看看我們的servlet,這個框架(姑且算它也是個框架吧)有很多比較麻煩的地方,比如說我們的url映射,我們需要寫一個servlet,一個servlet里面只有一個doget一個dopost來處理請求,每寫一個servlet我們就要去web.xml配置一個servlet,是不是很煩(反正我寫這么多字來描述它就已經很煩了==.),所以呢,為了簡化這個url映射的工作,我們的SpringMVC給我們提供了@requestMapping注解,好了先看代碼,注解是個啥后面高級的地方我們再講。
首先我們需要按照上一章的內容創建個maven工程:

然后,我們需要:

  • 導入SpringMVC的依賴:
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.1.2.RELEASE</version>
    </dependency>
    
    我們導入依賴保存pom之后,稍等一會兒,等maven下載好相應的依賴之后,你就會發現這個包給我們帶來了SpringMVC依賴的全家桶:

    接下來:
  • 開始寫配置文件:

    • 首先在resources下加上applicationContext.xml文件:
      此文件是Spring的配置文件,目前寫個空白的就行了,以后在加入別的功能,比如配置jdbc,配置redis時可能會用到:
      <?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:context="http://www.springframework.org/schema/context"
      xmlns:task="http://www.springframework.org/schema/task" 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/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 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
      </beans>
      
    • 然后在WEB-INF下加入spring-servlet.xml文件:
      這個文件是用來管理我們的請求的,當servlet被訪問之后,會將所有的請求都交給我們的SpringMVC處理,這里我們目前只進行了包掃描(就是告訴服務器你定義的url在哪個包下),以后也會加上別的東西,比如過濾器。
      <?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:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
      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/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 
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
      http://www.springframework.org/schema/task
      http://www.springframework.org/schema/task/spring-task.xsd">
      <!-- 進行包掃描 -->
      <context:component-scan base-package="com.demo"></context:component-scan>
      </beans>
      
    • 然后編輯我們的web.xml:

      <!-- 加載applicationContext配置文件,默認是/WEB-INF/applicationContext.xml 可以改成別的,此處配置為resources下的applicationContext.xml-->
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
       </context-param>
      
       <!-- 指定aop初始化方法 -->
       <listener>
          <listener-class>
              org.springframework.web.context.ContextLoaderListener
          </listener-class>
       </listener>
      
      <!-- 定義mvc的相關內容,配置攔截的url,加載配置文件spring-servlet.xml,將所有/開頭的請求都交給SpringMVC這個servlet進行處理。 -->
      <servlet>
          <servlet-name>Spring</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/spring-servlet.xml</param-value>
              <!-- 默認是/WEB-INF/[servlet名字]-servlet.xml可以隨意修改 -->
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      
      <servlet-mapping>
          <servlet-name>Spring</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
      

      這樣我們就做好了準備工作。

  • 接下來開始寫業務邏輯:
    • 首先我們在src下建個包 com.test.controller
    • 然后在包下創建一個類:TestController,并且在類下寫上一個普通方法test并且打印一句話。
    • 接著我們給這個類加上一個@controller注解,再加上個@RequestMapping注解,并且加上value值test。
    • 再接著給方法也加上一個@RequestMapping注解,并且加上value值test。
      ```java
      package com.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = “/test”)
public class TestController {

@RequestMapping(value = "/test")
public void test(){
    System.out.println("進來了");
}

}
```

  • 最后我們運行項目,訪問http://localhost:8080/SpringMVC_Demo/test/test, 也就是地址+端口+項目名+/test/test.最后雖然頁面報了404,但是你會發現我們也控制臺打印了一句話

    這說明我們就訪問成功了。
    其實我們訪問的原理是這樣的:
    當我們訪問地址之后,servlet把所有的請求都通過配置交給了SpringMVC的DispatcherServlet來管理。當加載DispatcherServlet了spring-servlet.xml就會讀取里面的包掃描,在被掃描的包下讀取被注解@Controller標記的類,然后通過@RequestMapping注解里面的value值確定訪問的路徑,一級一級往下走,先去找類的@RequestMapping,再去找方法的@RequestMapping。
    恩,流程大概是醬紫的

另外附上源碼下載鏈接:https://share.weiyun.com/e6b7142f77cd760288a6401a0dce5d1a

分享到:
11條評論
Ctrl+Enter
作者

大妖怪

大妖怪

APP:1 帖子:76 回復:200 積分:7517

已加入社區[3076]天

夢里巷口,可有你倚門回首

作者詳情》
Top