第一步:
创建一个web工程
第二步:
引入jar包(在spring的基础上多了一个mvc的jar包)
第三步:
添加配置文件springmvc.xml并且引入配置(这里包含了开发所需的基本配置)
<?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:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
第四步:
在配置文件springmvc.xml中配置包扫描(因为基本是面向注解开发,所以这个是bi'xu'xiang
<!--配置注解扫描哪些包-->
<context:component-scan base-package="com.ctbu"/>
第五步:(重点)
在web.xml中配置前端控制器(前端所有界面的请求都会通过这里进行拦截分发)
<!-- 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>mySpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定SpringMVC配置文件 -->
<!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mySpringMVC</servlet-name>
<!-- 设置所有以action结尾的请求进入SpringMVC -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
第六步:
编写一个jsp页面,并使用a标签发送一个请求
<a href="${pageContext.request.contextPath}/test1.action">点击我</a>
第七步:
编写一个接收请求的控制器类,并写好注解以接收参数
@Controller
public class Mycontroller {
@RequestMapping("/test1.action")
public ModelAndView test1() {
System.out.println("来了");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name","hello");
modelAndView.setViewName("resoult.jsp");
return modelAndView;
}
}
第八步:
启动服务器运行并查看结果(我配置了一个结果页并把数据取了出来,说明一个入手的项目已经配置完成并且运行成功)