原创地址:https://blog.csdn.net/a901122/article/details/79882140
先简单搭建一下环境在讲解执行流程
搭建过程步骤如下:
1、新建一个JavaWeb项目
2、导入SpringMVC支持jar包文件加上log4j日志和数据库驱动包总共16个 Spring源码文件中可以找到相应的jar资源
3、配置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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!-- 配置controller扫描包 -->
<context:component-scan base-package="org.bdqn.demo" />
</beans>
4、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springmvc-first</display-name>
<servlet>
<servlet-name>springmvc-first</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc-first</servlet-name>
<!--匹配拦截的请求 -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
5、编写控制层,使用注解申明
1、在类上添加@Controller注解,把Controller交由Spring管理
2、在方法上面添加@RequestMapping注解,里面指定请求的url。其中“.action”可以加也可以不加。
@Controller
public class StudentController {
@RequestMapping("all.action")
public ModelAndView ItemList() {
System.out.println("---------------");
ModelAndView modelAndView=new ModelAndView();
// 添加数据到模型中
modelAndView.addObject("login", "admin");
// 设置视图的物理地址
modelAndView.setViewName("welcome.jsp");
return modelAndView;
}
}
6、启动web服务,进入测试页测试url地址 all.action
进入控制器后 跳回到指定的url地址
欢迎页面如下:
效果图如下:
二:框架结构
架构流程如下: