Spring MVC 的请求
两个关键词:DispatcherServlet 和Spring 应用上下文,Spring 应用上下文位于应用程序的Servlet 上下文中。
* .继承AbstractAnnotationConfigDispatcherServletInitializer 的任意类都会自动地配置DispatcherServlet 和Spring 应用上下文
* .在servlet 3.0中,容器会在类路径中查找实现ServletContainerInitializer 接口的类,如发现则用来配置Servlet 容器。
* .Spring提供了 SpringServletContainerInitializer 实现了ServletContainerInitializer,这个类又会查找实现现 WebApplicationInitializer 的类并将配置任务交给它来完成。
* .Spring 3.2 引入AbstractAnnotationConfigDispatcherServletInitializer 实现了WebApplicationInitializer,所以我们只需继承承 AbstractAnnotationConfigDispatcherServletInitializer,容器会自动发现它,用它来配置Servlet上下文.
* .AbstractAnnotationConfigDispatcherServletInitializer 会同时创建DispatcherServlet 和ContextLoaderListener(监听器)两个应用上下文。
1.配置DispatcherServlet:新建类来继承AbstractAnnotationConfigDispatcherServletInitializer,重写三个方法。
package com.qhf.mvc.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* 返回带有@Configuration 注解的类,它将会用来配置ContextLoaderListener 创建的的应用上下文中的bean,通常是驱动应用后端的中间层和数据层组件。
* @return
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {RootConfig.class};
}
/**
* DispatcherServlet 启动的时候,会创建Spring应用上下文,加载配置文件或配置类中所声明的Bean,web组件的bean如控制器,视图解析器和处理器映射。
* 返回带有@Configuration 注解的类,它将会用来定义DispatcherServlet 应用上下文中的bean。
* @return
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {WebConfig.class};
}
/**
* getServletMappings 方法将一个或多个路径映射到DispatcherServlet 上,“/” 表示它会是应用的默认Servlet,它会处理进入应用的所有请求。
* @return
*/
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
2.启用Spring MVC
xml 中用<mvc:annotation-driven> 启动注解驱动的Spring MVC。而基于配置类操作如下:
package com.qhf.mvc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc //启动springmvc
@ComponentScan(basePackages = {"com.qhf.mvc"})
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();//它会查找jsp文件,为视图名添加特定的前缀和后缀
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
/**
* 对静态资源的请求转发给Servlet 中默认的Servlet 上,而不是使用DispatcherServlet 本身来处理此类。
* @param configurer
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
3.RootConfig 配置,暂时不理解
package com.qhf.mvc.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages = {"com.qhf.mvc"}, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)})
public class RootConfig {
}
4.编写控制器
@Controller 基于 @Component,可读性高点
返回String 类型,被SpringMVC 解读为要渲染的试图名称,DispatcherServlet会要求试图解析器将这个逻辑名称解析为实际的视图。前缀是"/WEB-INF/views/",后缀是".jsp",组合起来是返回"/WEB-INF/views/home.jsp"。
package com.qhf.mvc.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@RequestMapping(value = {"/", "/homepage"}, method = RequestMethod.GET)
public String home(){
return "home";//被SpringMvc 解读为要渲染的试图名称,DispatcherServlet会要求试图解析器将这个逻辑名称解析为实际的试图。
}
}
5.jsp页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" session="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Welcome to Spittr</h1>
<a href="<c:url value='/spittles'/>">Spittles</a> |
<a href="<c:url value='/spitter/register'/> ">Register</a>|
<a href="<c:url value='/student/students'/> ">Students</a>|
<a href="<c:url value='/student/registerjsp'/> ">stuRegister</a>|
</body>
</html>
6.结果