原创声明:本文为作者原创,未经允许不得转载,经授权转载需注明作者和出处
接下来正式开始我们的框架之旅,由于本人一直都认的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