領先一步
VMware 提供培訓和認證,以加速您的進步。
瞭解更多在 Spring 3.0 中,Spring MVC 將透過 REST 支援進行增強。 這篇文章描述瞭如何使用 REST 支援在簡單的示例應用程式之上實現 AtomView。 按照此分步過程,瞭解使用 Spring MVC 中的新 REST 支援在簡單應用程式之上實現 AtomView 是多麼容易。
接下來,使用“匯入 > 將現有專案匯入工作空間”嚮導(從“檔案”選單中)在 Eclipse 中載入專案。 該應用程式是一個簡單的 Eclipse 動態 Web 專案,其中包含 Spring MVC 設定的所有基礎結構。 因此,如果您熟悉 Spring MVC,這應該不是什麼大問題。
接下來,在 com.springsource.samples.rest 包中,您將找到一個包含兩個控制器方法的 ContentController。
@Controller
public class ContentController {
private List<SampleContent> contentList = new ArrayList<SampleContent>();
@RequestMapping(value="/content.*", method=RequestMethod.GET)
public ModelAndView getContent() {
ModelAndView mav = new ModelAndView();
mav.setViewName("content");
mav.addObject("sampleContentList", contentList);
return mav;
}
@RequestMapping(value="/content.html", method=RequestMethod.POST)
public String addContent() {
contentList.add(SampleContent.generateContent("Alef Arendsen", new Date()));
return "redirect:content.html";
}
}
第一個處理程式返回 SampleContent 專案的列表。 第二個處理程式透過使用 SampleContent.generateContent() 方法新增新的 SampleContent 專案。 第一個處理程式響應 GET 請求,第二個處理程式響應 POST 請求。 這些方法本身已使用 @RequestMapping 註釋進行註釋以執行此操作。
在 rest-servlet.xml 應用程式上下文檔案中,除了元件掃描器之外,您還將找到一個 ViewResolver,在本例中為 InternalResourceViewResolver。 它將負責檢視名稱(getContent() 處理程式中的“content”情況)和 JSP 頁面之間的轉換。
在將此部署到例如 Tomcat 之後,您應該能夠轉到 https://:8080/spring-rest/rest。 這會將您重定向到 /rest/content,該 URL 由處理程式選取。
在 com.springsource.samples.rest 包中建立一個名為 SampleContentAtomView 的類,然後貼上以下程式碼。 該程式碼使用 Spring MVC 中的 Atom 支援類和來自 Rome 專案的 Atom 提要的文件物件模型。
public class SampleContentAtomView extends AbstractAtomFeedView {
@Override
protected void buildFeedMetadata(Map<String, Object> model, Feed feed, HttpServletRequest request) {
feed.setId("tag:springsource.com");
feed.setTitle("Sample Content");
@SuppressWarnings("unchecked")
List<SampleContent> contentList = (List<SampleContent>)model.get("sampleContentList");
for (SampleContent content : contentList) {
Date date = content.getPublicationDate();
if (feed.getUpdated() == null || date.compareTo(feed.getUpdated()) > 0) {
feed.setUpdated(date);
}
}
}
@Override
protected List<Entry> buildFeedEntries(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response) throws Exception {
@SuppressWarnings("unchecked")
List<SampleContent> contentList = (List<SampleContent>)model.get("sampleContentList");
List<Entry> entries = new ArrayList<Entry>(contentList.size());
for (SampleContent content : contentList) {
Entry entry = new Entry();
String date = String.format("%1$tY-%1$tm-%1$td", content.getPublicationDate());
// see http://diveintomark.org/archives/2004/05/28/howto-atom-id#other
entry.setId(String.format("tag:springsource.com,%s:%d", date, content.getId()));
entry.setTitle(String.format("On %s, %s wrote", date, content.getAuthor()));
entry.setUpdated(content.getPublicationDate());
Content summary = new Content();
summary.setValue(content.getText());
entry.setSummary(summary);
entries.add(entry);
}
return entries;
}
}
在一個完美的世界中,客戶端會要求使用 Accept HTTP 標頭首選的特定表示形式。 Accept HTTP 標頭(由 HTTP 規範定義)可以採用一個或多個媒體型別,並且應由瀏覽器(或任何 HTTP 客戶端)傳送,以指示它首選哪種表示形式。 例如,Atom 提要的適當媒體型別為“application/atom+xml”,而 HTML 檢視可能僅傳送“text/html”或“text/xhtml”作為 Accept 標頭。 然而,這存在一個小問題。 瀏覽器通常具有一組固定的媒體型別,它們作為 Accept HTTP 標頭髮送,並且沒有辦法(除了使用 JavaScript)來修改瀏覽器傳送的 Accept 標頭。 這就是副檔名是向伺服器指示您想要什麼表示形式的一個很好的替代方法的原因。
Spring 3.0 具有 ContentNegotiatingViewResolver,它可以與擴充套件以及 Accept 標頭一起使用。 在確定了適當的媒體型別之後,它會委派給一組其他檢視解析器來為我們執行此操作。 需要將以下內容貼上到 rest-servlet.xml 中才能使其工作。 如您所見,ContentNegotiatingViewResolver 將委派給 BeanNameViewResolver(如果需要,解析為 SampleContentAtomView)或 InternalResourceViewResolver。 順便說一句,下面的程式碼段應替換已經在您的 rest-servlet.xml 檔案中配置的 InternalResourceViewResolver。
ContentNegotiatingViewResolver 首先檢視副檔名,然後使用 Accept 標頭(順便說一句,這在一定程度上是可定製的)。 我們必須將適當的副檔名對映到適當的媒體型別。 在此示例中,我們將 .html 對映到媒體型別 text/html,並將 .atom 對映到 application/atom+xml。 這將確保呈現適當的檢視。
如果傳入 .atom 請求,ContentNegotiatingViewResolver 將查詢與 application/atom+xml 媒體型別匹配的檢視。 檢視解析器將查詢一個檢視,該檢視呈現帶有媒體型別 text/html 的內容,如果傳入 .html 請求。
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="atom" value="application/atom+xml"/>
<entry key="html" value="text/html"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
<bean id="content" class="com.springsource.samples.rest.SampleContentAtomView"/>
在將此放入您的應用程式上下文中之後,重新啟動伺服器應該可以解決問題。 轉到 https://:8080/spring-rest/rest/content.html 以檢視所有內容專案並生成新的內容專案。 轉到 https://:8080/spring-rest/rest/content.atom 以訂閱 Atom 提要。
我希望這篇小部落格文章向您展示了向您的應用程式新增 Atom 提要是多麼簡單。 除了 Atom 之外,Spring 還具有用於呈現 PDF 和 Excel 檔案、JSON 表示形式和 XML 文件的檢視支援類。 檢查它們,讓我們知道您的想法!