If you are using Spring MVC, then I would recommend using Portlets. In Spring, portlets are just lightweight controllers since they are only responsible for a fragment of the whole page, and are very easy to write. If you are using Spring 2.5, then you can enjoy all the benefits of the new annotation support, and they fit nicely in the whole Spring application with dependency injection and the other benefits of using Spring.
A portlet controller is pretty much the same as a servlet controller, here is a simple example:
@RequestMapping("VIEW")
@Controller
public class NewsPortlet {
private NewsService newsService;
@Autowired
public NewsPortlet(NewsService newsService) {
this.newsService = newsService;
}
@RequestMapping(method = RequestMethod.GET)
public String view(Model model) {
model.addAttribute(newsService.getLatests(10));
return "news";
}
}
Here, a NewsService will be automatically injected into the controller. The view method adds a List object to the model, which will be available as ${newsList} in the JSP. Spring will look for a view named news.jsp based on the return value of the method. The RequestMapping tells Spring that this contoller is for the VIEW mode of the portlet.
The XML configuration only needs to specify where the view and controllers are located:
<!-- look for controllers and services here -->
<context:component-scan base-package="com.example.news"/>
<!-- look for views here -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/news/"/>
<property name="suffix" value=".jsp"/>
</bean>
If you want to simply embed the portlets in your existing application, the you can bundle a portlet container, such as eXo, Sun, or Apache. If you want to build your application as a set of portlets, the you might want to consider a full blown portlal solution, such as Liferay Portal.