Liferay Portlet的动态URL

时间:2015-11-13 10:03:43

标签: liferay

我正在寻找一种用于开发具有不同URL的动态内容Liferay Portlet的解决方案。我不会在Liferay中创建单独的页面。所有信息都存储在单独的数据库中,所有页面都是使用Liferay Portlet生成的。我目前的Liferay版本是6.2 CE。

示例网址是

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组件?

2 个答案:

答案 0 :(得分:1)

你可以使用Liferay友好的网址映射获得非常相似的网址:

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

要使其正常工作,您需要在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

中配置映射
liferay-portlet.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> 之后的网址的hotel部分由/-/值定义。

配置是指<friendly-url-mapping>hotel</friendly-url-mapping>中定义的路由。只需要一个路线定义:

friendly-url-routes.xml

示例Liferay MVCPortlet方法读取参数:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 6.2.0//EN" "http://www.liferay.com/dtd/liferay-friendly-url-routes_6_2_0.dtd">

<routes>
    <route>
        <pattern>/{region}/{town}/{hotel}</pattern>
    </route>
</routes>

示例Spring控制器方法读取参数:

@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");
    ...
}

有关详细信息,请参阅FriendlyURLMapper

答案 1 :(得分:0)

另一个解决方案是编写一个单独的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>

示例servlet过滤器类:

public class CustomURLPatternFilter implements Filter {

@Override
public void destroy() {
    LOG.info("CustomURLPatternFilter.destroy()");
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws ServletException, IOException {

    HttpServletRequest request = (HttpServletRequest) req;
    String requestURI = request.getRequestURI();
    try {

        String[] urlPaths = StringUtil.split(requestURI, StringPool.FORWARD_SLASH);

        System.out.println(urlPaths[0]);
        System.out.println(urlPaths[1]);
        System.out.println(urlPaths[2]);
        System.out.println(urlPaths[3]);
        System.out.println(urlPaths[4]);
        System.out.println(urlPaths[5]);

        if (urlPaths.length == 6) {

            String forwardPath = "/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=" + urlPaths[1]
                    + "&_hotelsearch_WAR_hotelportlet_param2=" + urlPaths[2]
                    + "&_hotelsearch_WAR_hotelportlet_param3=" + urlPaths[3]
                    + "&_hotelsearch_WAR_hotelportlet_param4=" + urlPaths[4]
                    + "&_hotelsearch_WAR_hotelportlet_param5=" + urlPaths[5];

            req.getRequestDispatcher(forwardPath).forward(req, res);

        } 
        else {
            chain.doFilter(req, res);
        }
      } catch (Exception e) {
           chain.doFilter(req, res);
        e.printStackTrace();
      }
   }

   @Override
   public void init(FilterConfig filterConfig) {
       System.out.println("Called SampleFilter.init(" + filterConfig + ")");
   }

   private static final Log LOG = 
        LogFactoryUtil.getLog(CustomURLPatternFilter.class);
}

在您原来的酒店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") );