设为首页 加入收藏

TOP

Spring(mvc)思维导图(二)
2019-09-17 19:06:31 】 浏览:110
Tags:Spring mvc 思维
nbsp;

spring mvc同时提供了很多特殊的注解,用于处理请求和渲染视图等。DispatcherServlet初始化的过程中会默认使用这些特殊bean进行配置。如果你想指定使用哪个特定的bean,你可以在web应用上下文WebApplicationContext中简单地配置它们。

特殊bean.png

其中,常用的ViewResolver的配置。以jsp作为视图为例

 

<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

配置上传文件限制MultipartResolver

<!-- 上传限制 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
     <!-- 上传文件大小限制为31M,31*1024*1024 -->
     <property name="maxUploadSize" value="32505856"/>
</bean>

 

 

applicationContext.xml中的标签

 

applicationContext.xml配置文件标签.png

 

文件上传

前面说到DispatcherServlet中有个特殊的Bean叫MultipartResolver,可用于限制文件的上传大小等。当解析器MultipartResolver完成处理时,请求便会像其他请求一样被正常流程处理。

  • 表单
<form method="post" action="/form" enctype="multipart/form-data">
     <input type="text" name="name"/>
     <input type="file" name="file"/>
     <input type="submit"/>
</form>

 

  • 控制器
@RequestMapping(path = "/form", method = RequestMethod.POST) public String handleFormUpload(@RequestParam("name") String name, 
            @RequestParam("file") MultipartFile file) {
   if (!file.isEmpty()) {
          byte[] bytes = file.getBytes();
          // store the bytes somewhere
          return "redirect:uploadSuccess";
    }
    return "redirect:uploadFailure";
}

 

异常处理

先来说下常见的异常处理有几种方式,如下图:

 

异常处理方式.png

 

 

 

Spring的处理器异常解析器HandlerExceptionResolver接口的实现负责处理各类控制器执行过程中出现的异常。也是上面提到的,是DispatcherServlet中的特殊bean,可以自定义配置处理。

某种程度上讲,HandlerExceptionResolver与你在web应用描述符web.xml文件中能定义的异常映射(exception mapping)很相像,不过它比后者提供了更灵活的方式。比如它能提供异常被抛出时正在执行的是哪个处理器这样的信息。

  • HandlerExceptionResolver 提供resolveException接口

public interface HandlerExceptionResolver {  
    ModelAndView resolveException(  
            HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);  
}

 

 

  • 在BaseController中使用 @ExceptionHandler注解处理异常

  @ExceptionHandler(Exception.class)    
public Object exceptionHandler(Exception ex, HttpServletResponse response, 
              HttpServletRequest request) throws IOException {
        String url = "";
        String msg = ex.getMessage();
        Object resultModel = null;
try {
if (ex.getClass() == HttpRequestMethodNotSupportedException.class) { url = "admin/common/500"; System.out.println("--------毛有找到对应方法---------"); } else if (ex.getClass() == ParameterException.class) {//自定义的异常 } else if (ex.getClass() == UnauthorizedException.class) { url = "admin/common/unauth"; System.out.println("--------毛有权限---------"); } String header = req.getHeader("X-Requested-With"); boolean isAjax = "XMLHttpRequest".equalsIgnoreCase(header); String method = req.getMethod(); boolean isPos
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇FastDFS安装配置 下一篇全文检索~solr的使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目