天天看點

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>元素配置對靜态資源的映射: