If you are using Spring MVC, I would recommend using Portlets. In Spring, portlets are simply lightweight controllers because they are only responsible for a fragment of the entire page and are very easy to write. If you use Spring 2.5, then you can take full advantage of the new annotation support and fit nicely into your entire Spring application with dependency injection and other benefits of using Spring.
The portlet controller is almost the same as the 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 NewsService will be automatically entered 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 called news.jsp based on the return value of the method. RequestMapping tells Spring that this controller is for VIEW portlet mode.
The XML configuration should only indicate where the view and controllers are:
<context:component-scan base-package="com.example.news"/> <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 just want to embed portlets into an existing application, you can bundle a portlet container like eXo , Sun , or Apache . If you want to create your application as a set of portlets, you may need to consider a full-blown portal solution, such as Liferay Portal .
pjesi
source share