spring mvc: rss(xml)输出
准备:
rss包插件
Rome 库及其依赖项rome-utils,jdom和slf4j <!-- rss源依赖 --> <!-- https://mvnrepository.com/artifact/com.rometools/rome --> <dependency> <groupId>com.rometools</groupId> <artifactId>rome</artifactId> <version>1.8.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.rometools/rome-utils --> <dependency> <groupId>com.rometools</groupId> <artifactId>rome-utils</artifactId> <version>1.8.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jdom/jdom --> <dependency> <groupId>org.jdom</groupId> <artifactId>jdom</artifactId> <version>2.0.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency>
需要Bean视图解析
<!-- bean视图解析 --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
同时需要 一个RSSFeedViewer.java(类似的java来实现view视图)
需要继承:org.springframework.web.servlet.view.feed.AbstractRssFeedView 并复写:buildFeedItems,buildFeedMetadata
视图名:rssViewer(id名)
<bean id="rssViewer" class="json.RSSFeedViewer"/>
RSS的基本元素
RSSMessage.java
package json; import java.util.Date; public class RSSMessage { String title; String url; String summary; Date createDate; 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 getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } }
RSSFeedViewer视图view,继承AbstractRssFeedView(org.springframework.web.servlet.view.feed.AbstractRssFeedView)
并复写:buildFeedMetadata, buildFeedItems
其中:buildFeedMetadata是增加rss(xml)的说明性的内容,buildFeedItems对数据重新组长成需要的格式
其中:List<RSSMessage> list = (List<RSSMessage>) model.get("person"); 是RssController.java传递过来的
package json; 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 List<Item> buildFeedItems(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub //获取controller渲染过来的数据 List<RSSMessage> list = (List<RSSMessage>) model.get("person"); List<Item> items = new ArrayList<Item>(list.size()); for(RSSMessage tempContent: list) { 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.getCreateDate()); items.add(item); } return items; } @Override protected void buildFeedMetadata(Map<String, Object> model, Channel feed, HttpServletRequest request) { // TODO Auto-generated method stub //设置头部信息 feed.setTitle("school"); feed.setDescription("suzhou school"); feed.setLink("http://www.sina.com.cn"); super.buildFeedMetadata(model, feed, request); } }
RssController.java
其中:mv.addObject("person", list) 是传递到RSSFeedViewer视图中去的
package json; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; 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="/rss/list", method=RequestMethod.GET) public ModelAndView rss() { List<RSSMessage> list = new ArrayList<RSSMessage>(); RSSMessage rss = new RSSMessage(); rss.setTitle("张三"); rss.setUrl("http://www.sina.com.cn"); rss.setSummary("张三合肥市委书记,省委常委..."); rss.setCreateDate(new Date()); list.add(rss); rss.setTitle("李四"); rss.setUrl("http://www.baidu.com"); rss.setSummary("李四省委秘书长,合肥市委副书记..."); rss.setCreateDate(new Date()); list.add(rss); ModelAndView mv = new ModelAndView(); mv.setViewName("rssViewer"); mv.addObject("person", list); return mv; } }
访问地址:http://localhost:8080/gugua3/rss/list
<?xml version="1.0" encoding="UTF-8"?> <rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0"> <channel> <title>school</title> <link>http://www.sina.com.cn</link> <description>suzhou school</description> <item> <title>鏉庡洓</title> <link>http://www.baidu.com</link> <content:encoded>鏉庡洓鐪佸绉樹功闀匡紝鍚堣偉甯傚鍓功璁�...</content:encoded> <pubDate>Thu, 17 May 2018 03:51:46 GMT</pubDate> </item> <item> <title>鏉庡洓</title> <link>http://www.baidu.com</link> <content:encoded>鏉庡洓鐪佸绉樹功闀匡紝鍚堣偉甯傚鍓功璁�...</content:encoded> <pubDate>Thu, 17 May 2018 03:51:46 GMT</pubDate> </item> </channel> </rss>