In Spring 3, comes with a abstract class “AbstractRssFeedView” to generate RSS feed view, using java.net’s ROME package. In this tutorial, we show you how to generate a RSS feed view from Spring MVC framework.
Technologies used :
- Spring 3.0.5.RELEASE
- ROME 1.0.0
- JDK 1.6
- Eclipse 3.6
- Maven 3
At the end of the tutorial, when you visit this URL – http://localhost:8080/SpringMVC/rest/rssfeed, browser will return following RSS feed content :
<?xml version="1.0" encoding="UTF-8"?> <rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0"> <channel> <title>Mkyong Dot Com</title> <link>http://www.mkyong.com</link> <description>Java Tutorials and Examples</description> <item> <title>Spring MVC Tutorial 1</title> <link>http://www.mkyong.com/spring-mvc/tutorial-1</link> <content:encoded>Tutorial 1 summary ...</content:encoded> <pubDate>Tue, 26 Jul 2011 02:26:08 GMT</pubDate> </item> <item> <title>Spring MVC Tutorial 2</title> <link>http://www.mkyong.com/spring-mvc/tutorial-2</link> <content:encoded>Tutorial 2 summary ...</content:encoded> <pubDate>Tue, 26 Jul 2011 02:26:08 GMT</pubDate> </item> </channel> </rss>
1. Directory Structure
Review the final project structure.
2. Project Dependencies
For Maven, declares following dependencies in your pom.xml
.
<properties> <spring.version>3.0.5.RELEASE</spring.version> </properties> <dependencies> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- RSS --> <dependency> <groupId>net.java.dev.rome</groupId> <artifactId>rome</artifactId> <version>1.0.0</version> </dependency> <!-- for compile only, your container should have this --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> </dependencies>
3. Model
A simple POJO, later use this object to generate the RSS feed content.
package com.mkyong.common.model; import java.util.Date; public class SampleContent { String title; String url; String summary; Date createdDate; //getter and seeter methods }
4. AbstractRssFeedView
Create a class extends AbstractRssFeedView, and override the buildFeedMetadata
and buildFeedItems
methods, below code should be self-explanatory.
package com.mkyong.common.rss; 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.mkyong.common.model.SampleContent; import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Content; import com.sun.syndication.feed.rss.Item; public class CustomRssViewer extends AbstractRssFeedView { @Override protected void buildFeedMetadata(Map<String, Object> model, Channel feed, HttpServletRequest request) { feed.setTitle("Mkyong Dot Com"); feed.setDescription("Java Tutorials and Examples"); feed.setLink("http://www.mkyong.com"); super.buildFeedMetadata(model, feed, request); } @Override protected List<Item> buildFeedItems(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { @SuppressWarnings("unchecked") List<SampleContent> listContent = (List<SampleContent>) model.get("feedContent"); List<Item> items = new ArrayList<Item>(listContent.size()); for(SampleContent 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; } }
5. Controller
Spring MVC controller class, generate the rss feed content, and return a view name “rssViewer” (This view name is belong to above “CustomRssViewer“, will register in step 6 later).
package com.mkyong.common.controller; 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; import com.mkyong.common.model.SampleContent; @Controller public class RssController { @RequestMapping(value="/rssfeed", method = RequestMethod.GET) public ModelAndView getFeedInRss() { List<SampleContent> items = new ArrayList<SampleContent>(); SampleContent content = new SampleContent(); content.setTitle("Spring MVC Tutorial 1"); content.setUrl("http://www.mkyong.com/spring-mvc/tutorial-1"); content.setSummary("Tutorial 1 summary ..."); content.setCreatedDate(new Date()); items.add(content); SampleContent content2 = new SampleContent(); content2.setTitle("Spring MVC Tutorial 2"); content2.setUrl("http://www.mkyong.com/spring-mvc/tutorial-2"); content2.setSummary("Tutorial 2 summary ..."); content2.setCreatedDate(new Date()); items.add(content2); ModelAndView mav = new ModelAndView(); mav.setViewName("rssViewer"); mav.addObject("feedContent", items); return mav; } }
6. Spring Bean Registration
In a Spring bean definition file, enable the auto component scanning, and register your “CustomRssViewer
” class and “BeanNameViewResolver
” view resolver, so that when view name “rssViewer” is returned, Spring know it should map to bean id “rssViewer“.
File : mvc-dispatcher-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.mkyong.common.controller" /> <!-- Map returned view name "rssViewer" to bean id "rssViewer" --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean id="rssViewer" class="com.mkyong.common.rss.CustomRssViewer" /> </beans>
File content of
web.xml
is omitted, just a standard configuration, if you are interest, download this whole project at the end of the article.7. Demo
URL : http://localhost:8080/SpringMVC/rest/rssfeed
For Atom, you just need to extends
AbstractAtomFeedView
, instead of AbstractRssFeedView
.
相关推荐
spring3 MVC实战
Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了。Spring3 MVC结构简单,应了那句话简单就是美,而且他强大不失灵活,性能也很优秀。 Spring3 MVC的优点: 1、...
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。Spring MVC4是当前zuixin的版本,在众多特性上有了进一步的提升。, 在精通Spring...
spring3mvc导入包大全。里面拥有spring3MVC框架搭建的必备包。新手学习spring3必备
simple login spring, mvc is also used and spring security is also applied and eclipse is used with tocat 6.5
NULL 博文链接:https://liukai.iteye.com/blog/1019886
spring3MVC框架基本配置案例,里面两个功能案例,上传下载功能和使用xheditor发布消息功能
Spring.MVC-A.Tutorial-Spring.MVC学习指南 高清可复制版PDF
spring webmvc struts 2.5 spring webmvc struts 2.5 spring webmvc struts 2.5 spring webmvc struts 2.5 spring webmvc struts 2.5 spring webmvc struts 2.5 spring webmvc struts 2.5 spring webmvc struts 2.5 ...
Spring3mvc和hibernate整合示例代码.zip Spring3mvc和hibernate整合示例代码.zip
这里提供了Spring3 MVC中较为常见的使用方法,主要是利用Spring3官方文档建议使用的Annotation进行开发的一般步骤,包含了常见的集中Annotation的使用方法。
Spring Web MVC外文翻译,山理工本科毕业设计自用外文翻译,Spring Web MVC外文翻译,山理工本科毕业设计自用外文翻译
Spring3 MVC 深入研究 Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了。Spring3 MVC结构简单,应了那句话简单就是美,而且他强大不失灵活,性能也很优秀。
spring MVC配置详解
Spring3 MVC + POI 实现 Excel与MySQL 的导入导出
Spring3 MVC的优点: 1、Spring3 MVC的学习难度小于Struts2,Struts2用不上的多余功能太多。呵呵,当然这不是决定因素。 2、Spring3 MVC很容易就可以写出性能优秀的程序,Struts2要处处小心才可以写出性能优秀的程序...
spring3 mvc demo
spring3mvc实例
spring-mvc-3.0.xsd