Liferay Portlet的动态URL
我正在寻找一种解决方案,用于开发具有不同URL的动态内容Liferay Portlet。我不会在Liferay中创建单独的页面。所有信息都存储在单独的数据库中,所有页面都是使用Liferay Portlet生成的。我当前的Liferay版本是6.2 CE 示例URL是Liferay Portlet的动态URL,liferay,Liferay,我正在寻找一种解决方案,用于开发具有不同URL的动态内容Liferay Portlet。我不会在Liferay中创建单独的页面。所有信息都存储在单独的数据库中,所有页面都是使用Liferay Portlet生成的。我当前的Liferay版本是6.2 CE 示例URL是 https://localhost/travel/hotel/Lanzarote/Costa Teguise/Hotel Beatriz Costa & Spa https://localhost/travel/hotel
https://localhost/travel/hotel/Lanzarote/Costa Teguise/Hotel Beatriz Costa & Spa
https://localhost/travel/hotel/Lanzarote/Costa Teguise/Club Siroco Apartments
https://localhost/travel/hotel/Lanzarote/Costa Teguise/El Guarapo Apartments
如何在Liferay中实现不同的URL而不创建单独的页面?如果我需要使用Liferay API生成动态url,我需要使用哪些API组件?您可以通过Liferay友好的url映射获得非常相似的url:
https://localhost:8080/{page}/-/hotel/Lanzarote/Costa Teguise/Hotel Beatriz Costa
https://localhost:8080/{page}/-/hotel/Lanzarote/Costa Teguise/Club Siroco Apartments
https://localhost:8080/{page}/-/hotel/Lanzarote/Costa Teguise/El Guarapo Apartments
要使其正常工作,您需要在liferayportlet.xml
中配置映射:
<portlet>
<portlet-name>my-hotel-portlet</portlet-name>
<friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class>
<friendly-url-mapping>hotel</friendly-url-mapping>
<friendly-url-routes>friendly-url-routes.xml</friendly-url-routes>
...
</portlet>
示例Liferay MVCPortlet方法读取参数:
@Override
public void doView(RenderRequest renderRequest, RenderResponse renderResponse) {
String region = ParamUtil.getString(renderRequest, "region");
String town = ParamUtil.getString(renderRequest, "town");
String hotel = ParamUtil.getString(renderRequest, "hotel");
...
}
@RenderMapping
public String renderHotel(@RequestParam String region, @RequestParam String town, @RequestParam String hotel) {
...
return "hotel/view";
}
读取参数的弹簧控制器方法示例:
@Override
public void doView(RenderRequest renderRequest, RenderResponse renderResponse) {
String region = ParamUtil.getString(renderRequest, "region");
String town = ParamUtil.getString(renderRequest, "town");
String hotel = ParamUtil.getString(renderRequest, "hotel");
...
}
@RenderMapping
public String renderHotel(@RequestParam String region, @RequestParam String town, @RequestParam String hotel) {
...
return "hotel/view";
}
有关详细内容,请参阅。另一个解决方案是编写一个单独的servlet过滤器挂钩插件项目。其想法是将当前URL转换为特定于Liferay的URL 例如:
https://localhost/travel/hotel/Lanzarote/Costa Teguise/Hotel Beatriz Costa & Spa
转化为
https://localhost/web/guest/travel/hotel?p_p_id=hotelsearch_WAR_hotelportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&_hotelsearch_WAR_hotelportlet_param1=travel&_hotelsearch_WAR_hotelportlet_param2=hotel&_hotelsearch_WAR_hotelportlet_param3=Lanzarote&_hotelsearch_WAR_hotelportlet_param4=Costa%20Teguise&_hotelsearch_WAR_hotelportlet_param5=Hotel%20Beatriz%20Costa%20&%20Spa
首先在示例钩子项目liferay-hook.xml中配置映射:
<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN"
"http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">
<hook>
<servlet-filter>
<servlet-filter-name>CustomURLPatternFilter</servlet-filter-name>
<servlet-filter-impl>com.hotel.travel.portlet.customefilter.CustomURLPatternFilter</servlet-filter-impl>
<init-param>
<param-name>hello</param-name>
<param-value>world</param-value>
</init-param>
</servlet-filter>
<servlet-filter-mapping>
<servlet-filter-name>CustomURLPatternFilter</servlet-filter-name>
<url-pattern>/travel/hotel/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</servlet-filter-mapping>
在最初的hotel portlet的末尾
HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(req));
System.out.println( httpReq.getParameter("_hotelsearch_WAR_hotelportlet_param1") );
System.out.println( httpReq.getParameter("_hotelsearch_WAR_hotelportlet_param2") );
System.out.println( httpReq.getParameter("_hotelsearch_WAR_hotelportlet_param3") );
System.out.println( httpReq.getParameter("_hotelsearch_WAR_hotelportlet_param4") );
System.out.println( httpReq.getParameter("_hotelsearch_WAR_hotelportlet_param5") );
您应该检查这里也讨论了-。如果这个URL模式是由第三方定义的呢?那么我就不能使用liferay友好的url映射概念。如果你有一个严格的url格式,你必须遵守,你可以使用url重写(正如你自己在另一个答案中建议的那样)。如果你采用url重写的方式,你不必编写自己的过滤器。Liferay利用tuckey的URLRewiteFilter(),它支持转发。添加新规则意味着修改
/ROOT/WEB-INF/urlrewrite.xml
(手动或使用ext插件)。