web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/spring-config*.xml</param-value>
</context-param>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 字符集过滤器 -->
<filter>
<filter-name>Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 配置系统变量 contextConfigLocation,SpringMVC会根据配置的路径加载配置文件
- ContextLoaderListener 实现了接口 ServletContextListener,监听项目启动,用于Spring IoC容器初始化
- 配置 DispatcherServlet 我们给了一个 contextConfigLocation,SpringMVC会到这个路径下读取配置文件,如果不配置这个属性,SpringMVC会默认读取 /WEB-INF/dispatcher-servlet.xml 文件,没读取到会抛出异常
- DispatcherServlet 拦截请求的后缀有多种方式:
1. *.do
2. *.action
3. /
4. / *
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.5.xsd">
<!-- 扫描注解 -->
<context:component-scan base-package="com.ronin.blog.controller"/>
<!-- 配置注解驱动 -->
<mvc:annotation-driven/>
<!-- 静态资源放行 -->
<mvc:resources mapping="/static/**" location="/static/" />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 视图解析器中配置返回视图的前缀和后缀,SpringMVC根据返回的值拼接后到对应的路径下查找
Controller
完成了配置文件的书写,在 /WEB-INF/jsp/ 路径下创建一个 index.jsp 页面
@Controller
public class ArticleController {
/**
* 页面跳转
* @return
*/
@RequestMapping(value = "index",method = RequestMethod.GET)
public ModelAndView index(){
//创建模型视图对象
ModelAndView mav = new ModelAndView();
//添加视图
mav.setViewName("index");
//返回模型和视图
return mav;
}
}
启动项目,访问对应的请求路径,实现页面跳转!