Spring in action 阅读笔记3

2017-06-08  本文已影响20人  言西枣
5.3接受请求的输入

@RequestParam(value="", defaultValue="") long max
尽管defaultValue是String类型的值,但是绑定到max参数时,会转换成Long类型

"/spittles/12345" vs "/spittles/show?spittle_id=12345"
前者能够识别出要查询的资源,后者描述的是带有参数的一个操作-本质上是通过HTTP发起的RPC

7.1配置的替代方案

添加其他的Servlet与Filter
实现WebApplicationInitializer,注册其他组件

7.2Multipart
  1. MultipartResolver
    A. StandardServletMultipartResolver 配置信息在DispatcherServlet之中
@Bean
public MultipartResolver multipartResolver() throws IOException {
return new StandardServletMultipartResolver();
}
// 1.WebApplicationInitializer
DispatcherServlet ds = new DispatcherServlet();
Dynamic registration = context.addServlet("appServlet", ds);
registration.addMapping("/");
registration.setMultipartConfig(
new MultipartConfigElement("/tmp/spittr/uploads"));
//
// 2.AbstractAnnotationConfigDispatcherServletInitializer
@Override
protected void customizeRegistration(Dynamic registration) {
registration.setMultipartConfig(
new MultipartConfigElement("/tmp/spittr/uploads"));
}
//
// 3.web.xml
<servlet>
    <servlet-name>SpringDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <multipart-config>
        <location>/tmp</location>
        <max-file-size>5242880</max-file-size><!--5MB-->
        <max-request-size>20971520</max-request-size><!--20MB-->
        <file-size-threshold>0</file-size-threshold>
    </multipart-config>
</servlet>

B. CommonsMultipartResolver
只需要配置bean

@Bean
public MultipartResolver multipartResolver() throws IOException {
CommonsMultipartResolver multipartResolver =
new CommonsMultipartResolver();
multipartResolver.setUploadTempDir(
new FileSystemResource("/tmp/spittr/uploads"));
multipartResolver.setMaxUploadSize(2097152);
multipartResolver.setMaxInMemorySize(0);
return multipartResolver;
}

MultipartResolver在DispatcherServlet转发请求之前对request进行处理,如果是Multipart的请求,就分解为MultipartHttpServletRequest

  1. 请求
    POST enctype="multipart/form-data"
    <input type="file" name="" accept="" />
  2. 处理Multipart请求
    A. @RequestPart("name") byte[] filebyte
    B. @RequestPart("name") MultipartFile/Part file
    [Facepalm][Facepalm]如果使用Part参数接收文件,就不需要配置MultipartResolver了
7.3异常处理
public interface HandlerExceptionResolver {

    /**
     * Try to resolve the given exception that got thrown during on handler execution,
     * returning a ModelAndView that represents a specific error page if appropriate.
     * <p>The returned ModelAndView may be {@linkplain ModelAndView#isEmpty() empty}
     * to indicate that the exception has been resolved successfully but that no view
     * should be rendered, for instance by setting a status code.
     * @param request current HTTP request
     * @param response current HTTP response
     * @param handler the executed handler, or {@code null} if none chosen at the
     * time of the exception (for example, if multipart resolution failed)
     * @param ex the exception that got thrown during handler execution
     * @return a corresponding ModelAndView to forward to,
     * or {@code null} for default processing
     */
    ModelAndView resolveException(
            HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);

}

上一篇下一篇

猜你喜欢

热点阅读