原創聲明:本文為作者原創,未經允許不得轉載,經授權轉載需注明作者和出處
接下來正式開始我們的框架之旅,由于本人一直都認的Spring和Mybatis這兩個爹,所以接下來的重心會以這兩個框架為主,Struts和Hibernate等相同功能的框架就不做重點了。
之前講過,所謂的框架就是為了處理某一個或者某些復雜的問題而去編寫的一個可復用的設計結構。那么我們來看看我們的servlet,這個框架(姑且算它也是個框架吧)有很多比較麻煩的地方,比如說我們的url映射,我們需要寫一個servlet,一個servlet里面只有一個doget一個dopost來處理請求,每寫一個servlet我們就要去web.xml配置一個servlet,是不是很煩(反正我寫這么多字來描述它就已經很煩了==.),所以呢,為了簡化這個url映射的工作,我們的SpringMVC給我們提供了@requestMapping注解,好了先看代碼,注解是個啥后面高級的地方我們再講。
首先我們需要按照上一章的內容創建個maven工程:
然后,我們需要:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
我們導入依賴保存pom之后,稍等一會兒,等maven下載好相應的依賴之后,你就會發現這個包給我們帶來了SpringMVC依賴的全家桶:開始寫配置文件:
<?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>
<?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>
這樣我們就做好了準備工作。
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("進來了");
}
}
```
另外附上源碼下載鏈接:https://share.weiyun.com/e6b7142f77cd760288a6401a0dce5d1a