Controller只是承担了Servlet的一部分功能
Spring MVC 中的五大核心组件:
- DispatcherServlet (前端控制器, 处理请求的入口)
- HandlerMapping (映射器对象, 用于管理url与对应controller的映射关系)
- Interceptors(拦截器,实现请求响应的共性处理)
- Controller (后端控制器-handler对象, 负责处理请求的控制逻辑)
- ViewResolver(视图解析器,解析对应的视图关系:前缀+view+后缀)-要自己进行配置,基于view找到对应的页面
package org.springframework.web.servlet;
import java.util.Locale;
public interface ViewResolver {
View resolveViewName(String viewName, Locale locale) throws Exception;
}
Central dispatcher for HTTP request handlers/controllers
HTTP请求处理核心
DispatcherServlet 是发号指令的对象
public class DispatcherServlet extends FrameworkServlet {
}
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {
}
public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware {
}
<param-value>classpath:spring-configs.xml</param-value>
可以写多个
<param-value>classpath:spring-*.xml</param-value>
企业里访问控制器的方法都这样写*.do
<servlet-mapping>
<servlet-name>frontController</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
package org.springframework.web.servlet.handler;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
public class HandlerMappingIntrospector implements CorsConfigurationSource {
private final List<HandlerMapping> handlerMappings;
public HandlerMappingIntrospector(ApplicationContext context) {
this.handlerMappings = initHandlerMappings(context);
}
private static List<HandlerMapping> initHandlerMappings(ApplicationContext context) {
Map<String, HandlerMapping> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
context, HandlerMapping.class, true, false);
if (!beans.isEmpty()) {
List<HandlerMapping> mappings = new ArrayList<HandlerMapping>(beans.values());
AnnotationAwareOrderComparator.sort(mappings);
return mappings;
}
return initDefaultHandlerMappings(context);
}
private static List<HandlerMapping> initDefaultHandlerMappings(ApplicationContext context) {
Properties props;
String path = "DispatcherServlet.properties";
try {
Resource resource = new ClassPathResource(path, DispatcherServlet.class);
props = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load '" + path + "': " + ex.getMessage());
}
String value = props.getProperty(HandlerMapping.class.getName());
String[] names = StringUtils.commaDelimitedListToStringArray(value);
List<HandlerMapping> result = new ArrayList<HandlerMapping>(names.length);
for (String name : names) {
try {
Class<?> clazz = ClassUtils.forName(name, DispatcherServlet.class.getClassLoader());
Object mapping = context.getAutowireCapableBeanFactory().createBean(clazz);
result.add((HandlerMapping) mapping);
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException("Could not find default HandlerMapping [" + name + "]");
}
}
return result;
}
public List<HandlerMapping> getHandlerMappings() {
return this.handlerMappings;
}
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request);
for (HandlerMapping handlerMapping : this.handlerMappings) {
Object handler = handlerMapping.getHandler(wrapper);
if (handler == null) {
continue;
}
if (handlerMapping instanceof MatchableHandlerMapping) {
return ((MatchableHandlerMapping) handlerMapping);
}
throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping");
}
return null;
}
@Override
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request);
for (HandlerMapping handlerMapping : this.handlerMappings) {
HandlerExecutionChain handler = null;
try {
handler = handlerMapping.getHandler(wrapper);
}
catch (Exception ex) {
// Ignore
}
if (handler == null) {
continue;
}
if (handler.getInterceptors() != null) {
for (HandlerInterceptor interceptor : handler.getInterceptors()) {
if (interceptor instanceof CorsConfigurationSource) {
return ((CorsConfigurationSource) interceptor).getCorsConfiguration(wrapper);
}
}
}
if (handler.getHandler() instanceof CorsConfigurationSource) {
return ((CorsConfigurationSource) handler.getHandler()).getCorsConfiguration(wrapper);
}
}
return null;
}
private static class RequestAttributeChangeIgnoringWrapper extends HttpServletRequestWrapper {
public RequestAttributeChangeIgnoringWrapper(HttpServletRequest request) {
super(request);
}
@Override
public void setAttribute(String name, Object value) {
// Ignore attribute change
}
}
}
package org.springframework.web.cors;
import javax.servlet.http.HttpServletRequest;
public interface CorsConfigurationSource {
/**
* Return a {@link CorsConfiguration} based on the incoming request.
* @return the associated {@link CorsConfiguration}, or {@code null} if none
*/
CorsConfiguration getCorsConfiguration(HttpServletRequest request);
}
DispatcherServlet渲染方法
protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Determine locale for request and apply it to the response.
Locale locale = this.localeResolver.resolveLocale(request);
response.setLocale(locale);
View view;
if (mv.isReference()) {
// We need to resolve the view name.
view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
if (view == null) {
throw new ServletException("Could not resolve view with name '" + mv.getViewName() +
"' in servlet with name '" + getServletName() + "'");
}
}
else {
// No need to lookup: the ModelAndView object contains the actual View object.
view = mv.getView();
if (view == null) {
throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " +
"View object in servlet with name '" + getServletName() + "'");
}
}
// Delegate to the View object for rendering.
if (logger.isDebugEnabled()) {
logger.debug("Rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'");
}
try {
if (mv.getStatus() != null) {
response.setStatus(mv.getStatus().value());
}
view.render(mv.getModelInternal(), request, response);
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Error rendering view [" + view + "] in DispatcherServlet with name '" +
getServletName() + "'", ex);
}
throw ex;
}
}