Reputation: 338
I am looking for a solution for developing a dynamic content Liferay Portlet with different URLs. I am not going create separate pages in Liferay.All the information are stored in separate database and all pages are generated by using a Liferay Portlet.My current Liferay version is 6.2 CE.
Sample URLs are ,
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
How do I implement different URLs with out creating separate pages in Liferay? If I need to use Liferay API for generate dynamic URLs , what are the API components do I need to use?
Upvotes: 2
Views: 2252
Reputation: 2862
You can get very similar urls with Liferay friendly url mapping:
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
To make it work, you need to configure the mapping in 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>
The hotel
part of the url that comes right after /-/
is defined by <friendly-url-mapping>hotel</friendly-url-mapping>
value.
The configuration refers to the routes defined in friendly-url-routes.xml
. Just one route definition is necessary:
<?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>
Sample Liferay MVCPortlet method reading the parameters:
@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");
...
}
Sample Spring controller method reading the parameters:
@RenderMapping
public String renderHotel(@RequestParam String region, @RequestParam String town, @RequestParam String hotel) {
...
return "hotel/view";
}
See FriendlyURLMapper for detailed coverage.
Upvotes: 1
Reputation: 338
Another solution is writing a separate servlet filter hook plugin project.The idea is to transfer the current URL into Liferay specific URL.
As an example:
https://localhost/travel/hotel/Lanzarote/Costa Teguise/Hotel Beatriz Costa & Spa
Converted in to,
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
First configure the mapping in sample hook project 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>
Sample servlet filter class :
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);
}
At the end in your original 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") );
Upvotes: 0