1.SpringMVC执行流程图
2.相关代码
- web.xml 前端控制器配置
<!-- 前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--启动服务器时,加载springMVC.XML配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!-- 配置:load-on-startup 表示启动服务就创建DispatcherServlet对象
不配置则第一次被访问时才创建DispatcherSerlet对象-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--拦截所有请求-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2.SpringMVC配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<!-- 注解,包扫描 -->
<context:component-scan base-package="com.joyTop"></context:component-scan>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<!-- 文件的前缀-->
<property name="prefix" value="/WEB-INF/pages/" />
<!-- 文件的后缀名-->
<property name="suffix" value=".jsp" />
</bean>
<!-- 开启SpringMVC框架注解的支持-->
<mvc:annotation-driven />
</beans>
3.contrller 控制层
@Controller
public class HelloController {
@RequestMapping(path = "/holle")
public String sayHello() {
System.out.println("Hell StringMVC 2020.3.8");
return "success";
}
}
4.index.jsp
<body>
<h3>springMVC入门程序</h3>
<a href="holle">入门程序</a>
</body>
5 success.jsp
<body>
<h1>入门成功</h1>
</body>