Spring MVC知识点补充

      Spring MVC的核心是DispatcherServlet,其初始化配置拥有一系列的组件。拥有默认的组件实现。

  1.DispatcherServlet装配各型组件的逻辑

组件类型 发现机制
文件上传解析器

1)查找名为multipartResolver类型为MultipartResolver的Bean作为该类型组件

2)没有默认的实现类

如果用户没有在上下文中显式的定义这一类型的组件,DispatcherServlet将不会拥有该类型的组件。

本地化解析器

1)查找名为localeResolver类型为LocaleResolver的Bean作为该类型组件

2)如果1)找不到,使用默认的实现类(AcceptHeaderLocaleResolver)创建该类型组件

主题解析器

1)查找名为themeResolver类型为ThemeResolver的Bean作为该类型组件

2)如果1)找不到,使用默认的实现类(FixedThemeResolver)创建该类型组件
处理器映射器*(多个实例 order属性确定优先级)

1)如果detectAllHandlerMappings属性为true,默认为true根据类型匹配机制查找上下文及父Spring容器中所有类型为HandlerMapping的Bean,将他们作为该类型组件

2)如果detectAllHandlerMappings属性为false,查找名为handlerMapping,类型为HandlerMapping的的Bean作为该类型组件

3)如果通过以上两种方式都找不到,使用BeanNameUrlHandlerMapping实现类创建该类型组件

处理器适配器*(多个实例 order属性确定优先级)

1)如果detectAllHandlerAdapters属性为true,默认为true根据类型匹配机制查找上下文及父Spring容器中所有类型为HandlerAdapter的Bean,将他们作为该类型组件

2)如果detectAllHandlerAdapters属性为false,查找名为handlerAdapter,类型为HandlerAdapter的的Bean作为该类型组件

3)如果通过以上两种方式都找不到,使用DispatcherServlet.proeprties配置文件中指定的三个实现类分别创建一个适配器,添加到适配器列表中
处理器异常解析器(多个实例 order属性确定优先级)

1)如果detectAllHandlerExceptionResolvers属性为true,默认为true根据类型匹配机制查找上下文及父Spring容器中所有类型为HandlerExceptionResolver的Bean,将他们作为该类型组件

2)如果detectAllHandlerExceptionResolvers属性为false,查找名为handlerExceptionResolver类型为HandlerExceptionResolver的的Bean作为该类型组件

3)如果通过以上两种方式都找不到,使用DispatcherServlet.proeprties默认实现类
视图名称翻译器

1)查找名为viewNameTranslator;类型为RequestToViewNameTranslator的Bean作为该类型组件

2)如果1)找不到,使用默认的实现类(DefaultRequestToViewNameTranslator)创建该类型组件

视图解析器(多个实例 order属性确定优先级)

1)如果detectAllResolvers属性为true,默认为true根据类型匹配机制查找上下文及父Spring容器中所有类型为ViewResolver的Bean,将他们作为该类型组件

2)如果detectAllResolvers属性为false,查找名为viewResolver,类型为ViewResolver的的Bean作为该类型组件

3)如果通过以上两种方式都找不到,使用DispatcherServlet.proeprties配置文件中定义的默认实现类(InternalResourceViewResolver)创建该类型组件

Spring  MVC

  C:Contorller ,Spring 3.0支持以注解方式的驱动器,使得普通POJO类可以成为处理HTTP请求的控制器

@Controller
public class UserContorller{

   @RequestMapping("user/register")
   public String register(){
      return "user/register";

}
   @RequestMapping("{userId}")
   public ModelAndView showDetail(@PathVariable("userId")String userId){
       ModelAndView mav=new ModelAndView();
       mav.setViewName("user/showDetail");
       mav.addObject("user",userService.getUserId(userId));
      return mav;

}

}

   @RequestMapping不仅支持标准URL,还支持Ant风格的?* **  {XXX},占位符URL是Spring3.0新增功能

    占位符可以通过@PathVariable绑定到方法的入参中

   @RequestMapping(value="/delete",method=RequestMethod.Post,params="userId",headers="content-type=text/*")

@RequestMapping除了可以使用请求URL映射请求外,还可以使用请求方法,请求头参数和请求参数(报文体和URL包含的请求参数)映射请求。

value->请求URL    method->请求方法  params->请求参数  headers->报文头

params headers支持简单的表达式 params演示:

  • param1  包含
  • !param1 不包含
  • param1!=value1 包含param1的请求参数,其值不能为value1

 Spring  MVC处理方法

处理方法签名详细说明:(设置方法入参绑定请求信息,定义返回值类型,Spring MVC对不同签名的处理方法如何进行调用等)

@RequestParam绑定请求参数值  属性 value  required defaultValue

@CookieValue 绑定请求中的Cookie值  属性 value  required defaultValue

@RequestHeader 绑定请求报文头的属性值  属性 value  required defaultValue

使用命令、表单对象绑定请求参数值  POJO类拥有若干属性,会自动装配

使用Servlet  API对象 HttpServletRequest  HttpServletResponse  HttpSession

Spring MVC定义了代理类 WebRequest  NativeWebRequest

使用IO对象作为入参 (即OutputStream=>ServletResponse.getOutputStream()  

Writer=>Serv;etResponse.getWriter()   InputStream=>ServletRequest.getInputStream()  Reader=>ServletRequest.getReader())

其他类型参数 如Locale=>  HttpServletRequest.getLocale()

                          Principle=>HttpServletRequest.getUserPrincipal()

2.HttpMessageConverter<T>

Spring3.0新增的重要接口,他负责将请求信息转换为一个对象(类型T),将对象(类型T)输出为响应信息

public interface HttpMessageConverter<T> {

	/**
	 * Indicates whether the given class can be read by this converter.
	 * @param clazz the class to test for readability
	 * @param mediaType the media type to read, can be {@code null} if not specified.
	 * Typically the value of a {@code Content-Type} header.
	 * @return {@code true} if readable; {@code false} otherwise
	 */
	boolean canRead(Class<?> clazz, MediaType mediaType);

	/**
	 * Indicates whether the given class can be written by this converter.
	 * @param clazz the class to test for writability
	 * @param mediaType the media type to write, can be {@code null} if not specified.
	 * Typically the value of an {@code Accept} header.
	 * @return {@code true} if writable; {@code false} otherwise
	 */
	boolean canWrite(Class<?> clazz, MediaType mediaType);

	/**
	 * Return the list of {@link MediaType} objects supported by this converter.
	 * @return the list of supported media types
	 */
	List<MediaType> getSupportedMediaTypes();

	/**
	 * Read an object of the given type form the given input message, and returns it.
	 * @param clazz the type of object to return. This type must have previously been passed to the
	 * {@link #canRead canRead} method of this interface, which must have returned {@code true}.
	 * @param inputMessage the HTTP input message to read from
	 * @return the converted object
	 * @throws IOException in case of I/O errors
	 * @throws HttpMessageNotReadableException in case of conversion errors
	 */
	T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
			throws IOException, HttpMessageNotReadableException;

	/**
	 * Write an given object to the given output message.
	 * @param t the object to write to the output message. The type of this object must have previously been
	 * passed to the {@link #canWrite canWrite} method of this interface, which must have returned {@code true}.
	 * @param contentType the content type to use when writing. May be {@code null} to indicate that the
	 * default content type of the converter must be used. If not {@code null}, this media type must have
	 * previously been passed to the {@link #canWrite canWrite} method of this interface, which must have
	 * returned {@code true}.
	 * @param outputMessage the message to write to
	 * @throws IOException in case of I/O errors
	 * @throws HttpMessageNotWritableException in case of conversion errors
	 */
	void write(T t, MediaType contentType, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException;

}

    HttpMessageConverter<T>实现类

实现类 功能说明
StringHttpMessageConverter

用途:将请求信息转为字符串

1)T为String类型

2)可读取所有媒体类型(*/*)的请求信息,可以通过设置supportedMediaTypes属性指定媒体类型

3)响应信息的媒体类型为text/plain(即Content-Type的值)

FormHttpMessageConverter

用途:将表单数据读取到MultiValueMap中

1)T为org.springframework.util.MultiValueMap<String,?>类型

2)支持读取application/x-www-form-urlencoded的媒体类型,但不支持读取multipart/form-data的媒体

3)可写application/x-www-form-urlencoded及multipart/form-data媒体类型的响应信息
XmlAwareFormHttpMessageConverter 拓展与FormHttpMessageConverter,如果部分表单属性是Xml数据,可用改转换器读取
ResourceHttpMessageConverter

用途:读写org.srpingframework.core.io.Resource对象

1)T类型为org.srpingframework.core.io.Resource类型

2)可以读取所有媒体类型(*/*)的请求信息

3)如果类路径下提供了JAF(Java  Activation  Framework),则根据Resource的类型指定响应的媒体类型,否则响应媒体类型为application/octet-stream

BufferedImageHttpMessageConverter

用途:读写BufferedImage对象

1)T类型为BufferedImage类型

2)可以读取所有媒体类型

3)返回BufferedImage相应的媒体类型,也可以通过contentType显示指定

 ByteArrayHttpMessageConverter

 用途:读写二进制数据

1)T为byte[]类型

2)可以读取所有的媒体类型(*/*)的请求信息,可通过设置supportedMediaTypes属性指定媒体类型

3)响应媒体类型为application/octet-stream

 HttpMessageConverter  
 HttpMessageConverter  
 HttpMessageConverter  
 HttpMessageConverter  
 HttpMessageConverter  
 HttpMessageConverter  

猜你喜欢

转载自pupin9.iteye.com/blog/2056180
今日推荐