我正在使用Java(Spring MVC和JSP视图)构建一个小型网站,并尝试找到最佳解决方案来制作和包含几个可重用的模块(如“最新消息”、“即将举行的活动”等)。
所以问题是:Portlets、tiles还是其他技术?
我正在使用Java(Spring MVC和JSP视图)构建一个小型网站,并尝试找到最佳解决方案来制作和包含几个可重用的模块(如“最新消息”、“即将举行的活动”等)。
所以问题是:Portlets、tiles还是其他技术?
@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";
}
}
<!-- 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>
如果您想在现有应用程序中简单嵌入portlet,则可以绑定一个portlet容器,例如eXo、Sun或Apache。如果您想将应用程序构建为一组portlet,则可能需要考虑一个完整的门户解决方案,例如Liferay Portal。
public class MyApplication implements EntryPoint, HistoryListener
{
static final String INIT_STATE = "status";
/**
* This is the entry point method. Instantiates the home page.
*/
public void onModuleLoad ()
{
RootPanel.get ().setStyleName ("root");
initHistorySupport ();
}
private void initHistorySupport ()
{
History.addHistoryListener (this);
// check to see if there are any tokens passed at startup via the browser’s URI
String token = History.getToken ();
if (token.length () == 0)
{
onHistoryChanged (INIT_STATE);
}
else
{
onHistoryChanged (token);
}
}
/**
* Fired when the user clicks the browser's 'back' or 'forward' buttons.
*
* @param historyToken the token representing the current history state
*/
public void onHistoryChanged (String historyToken)
{
RootPanel.get ().clear ();
Page page;
if (Page1.TOKEN.equalsIgnoreCase (historyToken))
{
page = new Page1 ();
}
else if (Page2.TOKEN.equalsIgnoreCase (historyToken))
{
page = new Page2 ();
}
else if (Page3.TOKEN.equalsIgnoreCase (historyToken))
{
page = new Page3 ();
}
RootPanel.get ().add (page);
}
}
我在使用Ajax JSF(IceFaces)和Liferay Portal的portlet方面有很多经验,但我不会向任何人推荐它们 - 当阅读教程时一切看起来都很好,但实践中却是真正的地狱。当然,我认为使用Spring MVC和JSP更加方便和轻量级,但无论如何,portlet技术在我看来并没有得到很好的支持。
我不确定在这个上下文中“可重用组件”是什么意思,但如果你的意思是希望某些常见元素出现在每个页面上,例如横幅、页脚、导航链接等等,那么你可以考虑使用SiteMesh。我的团队已经在几个国际化的Web应用程序中成功地使用了它。
Tapestry是一个Java Web应用程序框架,重点在于轻松创建可重用组件。
我曾经使用过Sitemesh,它很适合将一组页面封装在标准头部和底部中,但Tapestry更适合创建在许多页面上可能多次使用的组件。Tapestry组件可以将其他组件作为参数传递,这允许Sitemesh样式的封装。