天天看点

Spring MVC 实践 - Component Spring MVC 实践

标签 : Java与Web

Spring MVC的数据绑定并非没有任何限制, 有案例表明: Spring在如何正确绑定数据方面是杂乱无章的. 比如: Spring总是试图用默认的语言区域将日期输入绑定到<code>java.util.Data</code>, 如果想要使用不同的日期格式(format),就需要<code>Converter</code>的协助.

Spring提供了<code>Converter</code>接口来供开发者自定义<code>Converter</code>类:

自定义<code>Converter</code>:

配置

为了能够让Spring MVC使用我们自定义的<code>Converter</code>, 需要在配置文件中配置一个<code>ConversionServiceFactoryBean</code>:

然后为<code>&lt;annotation-driven/&gt;</code>配置<code>conversion-service</code>属性:

注: 还可以使用<code>FormattingConversionServiceFactoryBean</code>来加载<code>Converter</code>, 由于其配置方法与<code>ConversionServiceFactoryBean</code>, 故在此就不再赘述.

<code>Controller</code>

<code>BindingResult</code>参数中放置了Spring的所有绑定错误.

Spring提供了<code>Interceptor</code>接口来供开发者自定义<code>Interceptor</code>类:

自定义<code>Interceptor</code>

Spring MVC提供了<code>MultipartFile</code>接口,上传到应用中的文件都被包装在一个<code>MultipartFile</code>对象中:

<code>MultipartFile</code>

描述

<code>String getName()</code>

Return the name of the parameter in the multipart form.

<code>String getOriginalFilename()</code>

Return the original filename in the client’s filesystem.

<code>long getSize()</code>

Return the size of the file in bytes.

<code>boolean isEmpty()</code>

Return whether the uploaded file is empty, that is, either no file has been chosen in the multipart form or the chosen file has no content.

<code>String getContentType()</code>

Return the content type of the file.

<code>byte[] getBytes()</code>

Return the contents of the file as an array of bytes.

<code>InputStream getInputStream()</code>

Return an InputStream to read the contents of the file from.

<code>void transferTo(File dest)</code>

Transfer the received file to the given destination file.

在Servlet 3.0及更高版本的容器中进行文件上传编程,总是围绕着<code>@MultipartConfig</code>注解和<code>Part</code>接口,处理上传文件的Servlet必须以<code>@MultipartConfig</code>注解标注, 但<code>DispatcherServlet</code>是Spring jar包已经编译好的类, 无法进行修改,值得庆幸的是Servlet 3.0还可以使用部署描述符web.xml将一个Servlet变为MultipartConfig Servlet:

此外, 在mvc-servlet.xml文件中配置一个MultipartResolver:

此时就可以进行文件上传编程了:

系统异常包含两类: 预期异常、运行时异常<code>RuntimeException</code>.前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试等手段减少运行时异常的发生.

基于Spring MVC的<code>DAO</code>、<code>Service</code>、<code>Controller</code>的异常都可以通过<code>throw</code>向上层抛出,最后统一由<code>DispatcherServlet</code>的异常处理器进行处理.

自定义异常

如果<code>Controller</code>/<code>Service</code>/<code>DAO</code>抛出此类异常说明是预期异常:

异常处理器

error.vm

注册异常处理器

JSON数据格式形式简单, 解析方便, 因此常用在接口调用、HTML页面中.

Spring MVC对其提供了如下支持:在<code>Controller</code>方法上添加<code>@ResponseBody</code>注解, Spring MVC会自动将Java对象转换成JSON字符串输出; 在方法形参上添加<code>@RequestBody</code>注解, Spring MVC会自动将JSON串转换成Java对象:

fastjson

Spring MVC默认使用jackson对<code>request</code>/<code>response</code>进行JSON转换,而在此我们选用性能更高的fastjson, 因此需要在<code>&lt;annotation-driven/&gt;</code>中另做配置.

首先, 使用fastjson需要在pom.xml中添加如下依赖:

然后在mvc-servlet.xml中做如下配置:

在web.xml配置一个编码<code>Filter</code>可以解决POST乱码问题:

对于GET乱码, 由于Tomcat 8.0之前版本默认使用<code>ISO-8859-1</code>编码, 因此有两种解决方案:

修改tomcat配置文件

修改tomcat配置文件server.xml设置编码与工程编码一致:

重新编码

将经Tomcat编码的内容解码后再重新编码为UTF-8:

注: Tomcat 8.0及更高版本的容器不用此配置.

如果将<code>DispatherServlet</code>配置成拦截所有请求<code>&lt;url-pattern&gt;/&lt;/url-pattern&gt;</code>, 则必须额外配置静态资源的映射规则, 否则Spring MVC会对像js/css之类的文件也做转发.

Spring MVC使用<code>&lt;mvc:resources/&gt;</code>元素配置对静态资源的映射: