Spring MVC呢?
这是我的假设:
- 您正在使用Spring框架
- 您有一些要发布在供稿中的实体,例如“新闻”
- 您的“新闻”实体具有creationDate,title和shortDescription
- 您有一些存储库/仓库,例如“ NewsRepository”,它将从数据库中返回新闻
- 你想写得尽可能少
- 您不想手动格式化Atom(xml)
实际上,您实际上不需要在应用程序中使用Spring MVC。 如果这样做,请跳至步骤3。
步骤1:将Spring MVC依赖项添加到您的应用程序
使用Maven将是:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
步骤2:添加Spring MVC DispatcherServlet
使用web.xml将是:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/feed</url-pattern>
</servlet-mapping>
注意,我将url-pattern设置为“ / feed”,这意味着我不希望Spring MVC处理我的应用程序中的任何其他URL(我在其余的应用程序中使用了不同的Web框架)。 我还给它提供了一个全新的contextConfigLocation,其中仅保留了mvc配置。
请记住,将DispatcherServlet添加到已经具有Spring的应用程序时(例如,从ContextLoaderListener继承),您的上下文是从全局实例继承的,因此您不应创建在该全局实例中再次存在的bean,也不应该包含定义它们的xml。 注意两次Spring上下文,并查阅spring或servlet文档以了解发生了什么。
步骤3.添加ROME –处理Atom格式的库
与Maven是:
<dependency>
<groupId>net.java.dev.rome</groupId>
<artifactId>rome</artifactId>
<version>1.0.0</version>
</dependency>
步骤4.编写非常简单的控制器
@Controller
public class FeedController {
static final String LAST_UPDATE_VIEW_KEY = 'lastUpdate';
static final String NEWS_VIEW_KEY = 'news';
private NewsRepository newsRepository;
private String viewName;
protected FeedController() {} //required by cglib
public FeedController(NewsRepository newsRepository, String viewName) {
notNull(newsRepository); hasText(viewName);
this.newsRepository = newsRepository;
this.viewName = viewName;
}
@RequestMapping(value = '/feed', method = RequestMethod.GET)
@Transactional
public ModelAndView feed() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName(viewName);
List<News> news = newsRepository.fetchPublished();
modelAndView.addObject(NEWS_VIEW_KEY, news);
modelAndView.addObject(LAST_UPDATE_VIEW_KEY, getCreationDateOfTheLast(news));
return modelAndView;
}
private Date getCreationDateOfTheLast(List<News> news) {
if(news.size() > 0) {
return news.get(0).getCreationDate();
}
return new Date(0);
}
}
如果您想复制并粘贴(谁不想要),这里有一个测试:
@RunWith(MockitoJUnitRunner.class)
public class FeedControllerShould {
@Mock private NewsRepository newsRepository;
private Date FORMER_ENTRY_CREATION_DATE = new Date(1);
private Date LATTER_ENTRY_CREATION_DATE = new Date(2);
private ArrayList<News> newsList;
private FeedController feedController;
@Before
public void prepareNewsList() {
News news1 = new News().title('title1').creationDate(FORMER_ENTRY_CREATION_DATE);
News news2 = new News().title('title2').creationDate(LATTER_ENTRY_CREATION_DATE);
newsList = newArrayList(news2, news1);
}
@Before
public void prepareFeedController() {
feedController = new FeedController(newsRepository, 'viewName');
}
@Test
public void returnViewWithNews() {
//given
given(newsRepository.fetchPublished()).willReturn(newsList);
//when
ModelAndView modelAndView = feedController.feed();
//then
assertThat(modelAndView.getModel())
.includes(entry(FeedController.NEWS_VIEW_KEY, newsList));
}
@Test
public void returnViewWithLastUpdateTime() {
//given
given(newsRepository.fetchPublished()).willReturn(newsList);
//when
ModelAndView modelAndView = feedController.feed();
//then
assertThat(modelAndView.getModel())
.includes(entry(FeedController.LAST_UPDATE_VIEW_KEY, LATTER_ENTRY_CREATION_DATE));
}
@Test
public void returnTheBeginningOfTimeAsLastUpdateInViewWhenListIsEmpty() {
//given
given(newsRepository.fetchPublished()).willReturn(new ArrayList<News>());
//when
ModelAndView modelAndView = feedController.feed();
//then
assertThat(modelAndView.getModel())
.includes(entry(FeedController.LAST_UPDATE_VIEW_KEY, new Date(0)));
}
}
注意:在这里,我正在使用fest-assert和mockito。 依赖项是:
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
步骤5.编写非常简单的视图
这是所有魔术格式化发生的地方。 一定要看一看Entry类的所有方法,因为您可能想使用/填充很多东西。
import org.springframework.web.servlet.view.feed.AbstractAtomFeedView;
[...]
public class AtomFeedView extends AbstractAtomFeedView {
private String feedId = 'tag:yourFantastiSiteName';
private String title = 'yourFantastiSiteName: news';
private String newsAbsoluteUrl = 'http://yourfanstasticsiteUrl.com/news/';
@Override
protected void buildFeedMetadata(Map<String, Object> model, Feed feed, HttpServletRequest request) {
feed.setId(feedId);
feed.setTitle(title);
setUpdatedIfNeeded(model, feed);
}
private void setUpdatedIfNeeded(Map<String, Object> model, Feed feed) {
@SuppressWarnings('unchecked')
Date lastUpdate = (Date)model.get(FeedController.LAST_UPDATE_VIEW_KEY);
if (feed.getUpdated() == null || lastUpdate != null || lastUpdate.compareTo(feed.getUpdated()) > 0) {
feed.setUpdated(lastUpdate);
}
}
@Override
protected List<Entry> buildFeedEntries(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
@SuppressWarnings('unchecked')
List<News> newsList = (List<News>)model.get(FeedController.NEWS_VIEW_KEY);
List<Entry> entries = new ArrayList<Entry>();
for (News news : newsList) {
addEntry(entries, news);
}
return entries;
}
private void addEntry(List<Entry> entries, News news) {
Entry entry = new Entry();
entry.setId(feedId + ', ' + news.getId());
entry.setTitle(news.getTitle());
entry.setUpdated(news.getCreationDate());
entry = setSummary(news, entry);
entry = setLink(news, entry);
entries.add(entry);
}
private Entry setSummary(News news, Entry entry) {
Content summary = new Content();
summary.setValue(news.getShortDescription());
entry.setSummary(summary);
return entry;
}
private Entry setLink(News news, Entry entry) {
Link link = new Link();
link.setType('text/html');
link.setHref(newsAbsoluteUrl + news.getId()); //because I have a different controller to show news at http://yourfanstasticsiteUrl.com/news/ID
entry.setAlternateLinks(newArrayList(link));
return entry;
}
}
步骤6.将类添加到Spring上下文
我正在使用xml方法。 因为我老了,我喜欢xml。 不,很认真,我使用xml是因为我可能想用不同的视图(RSS 1.0,RSS 2.0等)声明FeedController几次。
这就是前面提到的spring-mvc.xml
<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns='http://www.springframework.org/schema/beans'
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.xsd'>
<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'/>
</list>
</property>
</bean>
<bean class='eu.margiel.pages.confitura.feed.FeedController'>
<constructor-arg index='0' ref='newsRepository'/>
<constructor-arg index='1' value='atomFeedView'/>
</bean>
<bean id='atomFeedView' class='eu.margiel.pages.confitura.feed.AtomFeedView'/>
</beans>
您完成了。
之前曾有人要求我将所有工作代码放入某个公共存储库中,所以这又是另一回事了。 我已经描述了我已经发布的内容,您可以从bitbucket中获取提交。
参考: Solid Craft博客上来自我们JCG合作伙伴 Jakub Nabrdalik的Atom Feeds与Spring MVC 。
翻译自: https://www.javacodegeeks.com/2012/10/spring-mvc-for-atom-feeds.html