- Spring MVC 基礎
- Spring MVC - 首頁
- Spring MVC - 概述
- Spring MVC - 環境搭建
- Spring MVC - Hello World 示例
- Spring MVC - 表單處理
- Spring MVC - 表單處理
- Spring MVC - 頁面重定向
- Spring MVC - 靜態頁面
- Spring MVC - 表單標籤庫
- Spring MVC - 文字框
- Spring MVC - 密碼框
- Spring MVC - 文字區域
- Spring MVC - 複選框
- Spring MVC - 多選複選框
- Spring MVC - 單選按鈕
- Spring MVC - 多選單選按鈕
- Spring MVC - 下拉列表
- Spring MVC - 列表框
- Spring MVC - 隱藏域
- Spring MVC - 錯誤處理
- Spring MVC - 檔案上傳
- Spring MVC - 處理器對映
- Bean名稱URL處理器對映
- 控制器類名處理器對映
- 簡單URL處理器對映
- Spring MVC - 控制器
- Spring MVC - 多動作控制器
- 屬性方法名解析器
- 引數方法名解析器
- 可引數化檢視控制器
- Spring MVC - 檢視解析器
- 內部資源檢視解析器
- Spring MVC - XML檢視解析器
- 資源束檢視解析器
- 多個解析器對映
- Spring MVC - 整合
- Spring MVC - Hibernate Validator
- Spring MVC - 生成RSS Feed
- Spring MVC - 生成XML
- Spring MVC - 生成JSON
- Spring MVC - 生成Excel
- Spring MVC - 生成PDF
- Spring MVC - 使用log4j
- Spring 問題與解答
- Spring - 問題與解答
- Spring 有用資源
- Spring MVC - 快速指南
- Spring MVC - 有用資源
- Spring MVC - 討論
Spring MVC - 生成RSS Feed示例
以下示例演示如何使用 Spring Web MVC 框架生成 RSS Feed。首先,我們需要一個可用的 Eclipse IDE,然後考慮以下步驟來使用 Spring Web 框架開發基於動態表單的 Web 應用程式。
| 步驟 | 描述 |
|---|---|
| 1 | 按照 Spring MVC - Hello World 章節中的說明,建立一個名為 TestWeb 的專案,位於 com.tutorialspoint 包下。 |
| 2 | 在 com.tutorialspoint 包下建立 Java 類 RSSMessage、RSSFeedViewer 和 RSSController。 |
| 3 | 從同一個 Maven 倉庫頁面下載 Rome 庫Rome及其依賴項 rome-utils、jdom 和 slf4j。將它們放入你的 CLASSPATH。 |
| 4 | 在 SRC 資料夾下建立一個屬性檔案 messages.properties。 |
| 5 | 最後一步是建立原始檔和配置檔案的內容,並匯出應用程式,如下所述。 |
RSSMessage.java
package com.tutorialspoint;
import java.util.Date;
public class RSSMessage {
String title;
String url;
String summary;
Date createdDate;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
}
RSSFeedViewer.java
package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.feed.AbstractRssFeedView;
import com.rometools.rome.feed.rss.Channel;
import com.rometools.rome.feed.rss.Content;
import com.rometools.rome.feed.rss.Item;
public class RSSFeedViewer extends AbstractRssFeedView {
@Override
protected void buildFeedMetadata(Map<String, Object> model, Channel feed,
HttpServletRequest request) {
feed.setTitle("TutorialsPoint Dot Com");
feed.setDescription("Java Tutorials and Examples");
feed.setLink("https://tutorialspoint.tw");
super.buildFeedMetadata(model, feed, request);
}
@Override
protected List<Item> buildFeedItems(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response) throws Exception {
List<RSSMessage> listContent = (List<RSSMessage>) model.get("feedContent");
List<Item> items = new ArrayList<Item>(listContent.size());
for(RSSMessage tempContent : listContent ){
Item item = new Item();
Content content = new Content();
content.setValue(tempContent.getSummary());
item.setContent(content);
item.setTitle(tempContent.getTitle());
item.setLink(tempContent.getUrl());
item.setPubDate(tempContent.getCreatedDate());
items.add(item);
}
return items;
}
}
RSSController.java
package com.tutorialspoint;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class RSSController {
@RequestMapping(value="/rssfeed", method = RequestMethod.GET)
public ModelAndView getFeedInRss() {
List<RSSMessage> items = new ArrayList<RSSMessage>();
RSSMessage content = new RSSMessage();
content.setTitle("Spring Tutorial");
content.setUrl("http://www.tutorialspoint/spring");
content.setSummary("Spring tutorial summary...");
content.setCreatedDate(new Date());
items.add(content);
RSSMessage content2 = new RSSMessage();
content2.setTitle("Spring MVC");
content2.setUrl("http://www.tutorialspoint/springmvc");
content2.setSummary("Spring MVC tutorial summary...");
content2.setCreatedDate(new Date());
items.add(content2);
ModelAndView mav = new ModelAndView();
mav.setViewName("rssViewer");
mav.addObject("feedContent", items);
return mav;
}
}
TestWeb-servlet.xml
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package = "com.tutorialspoint" /> <bean class = "org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean id = "rssViewer" class = "com.tutorialspoint.RSSFeedViewer" /> </beans>
在這裡,我們建立了一個 RSS feed POJO RSSMessage 和一個 RSS 訊息檢視器,它擴充套件了AbstractRssFeedView並覆蓋了其方法。在 RSSController 中,我們生成了一個示例 RSS Feed。
完成原始檔和配置檔案的建立後,匯出你的應用程式。右鍵單擊你的應用程式,使用匯出 → WAR 檔案選項並將TestWeb.war檔案儲存到 Tomcat 的 webapps 資料夾中。
現在,啟動你的 Tomcat 伺服器,並確保你能夠使用標準瀏覽器訪問 webapps 資料夾中的其他網頁。嘗試 URL − https://:8080/TestWeb/rssfeed,我們將看到以下螢幕。
廣告