Hi we have one of the beautiful concept is friendly URL in Liferay.
Suppose if we have URL like this in every render URL of Liferay.:-
We can generate friendly URL like below :-
(ii) Friendly Url when we call action url.
(iii) Friendly Url when we call action url passing parameter(Id).
Create one liferay plugin project name as per your requirement, in our case it's Mydetails plugin project with Mydetails portlet
Step 1 : Paste below code in mypage.jsp
Suppose if we have URL like this in every render URL of Liferay.:-
http://localhost:8080/web/guest/home?p_p_id=mydetails
_WAR_mydetailsportlet&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&p_p_col_id=column-2&p_p_col_pos=1&p_p_col_count=3&_mydetails_WAR_mydetailsportlet_mvcPath=%2Fhtml%2Fmypath%2Fmydetails.jsp
The following are the General Parameters and its values in Portlet URL
p_p_id: current portlet id
p_p_state: window sate
p_p_mode: portlet mode either view/edit
p_p_lifecycle: this is life cycle of portlet 0/1/2
0: render phase or render URL
1: action phase or action URL
2: server resource URL
We can generate friendly URL like below :-
http://localhost:8080/web/guest/home/-/mydetails/
addmydetails
(i) Friendly url when we access jsp pages(rendering from one jsp to another).(ii) Friendly Url when we call action url.
(iii) Friendly Url when we call action url passing parameter(Id).
Friendly URL Implementation
- Configure URL routes in xml file
- Friendly URL Implementation Java class
- Configure the Friendly URL information in liferay-portlet.xml file.
-----------------------------------------------------------------------------------------------------------------
Create one liferay plugin project name as per your requirement, in our case it's Mydetails plugin project with Mydetails portlet
Step 1 : Paste below code in mypage.jsp
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%> <%@page import="javax.portlet.PortletURL"%> <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <portlet:defineObjects /> <% long mydetaiId = 123; PortletURL addMydetailsURL = renderResponse.createRenderURL(); addMydetailsURL.setParameter("jspPage", "/html/crud/addmydetails.jsp"); PortletURL editMydetailsURL = renderResponse.createActionURL(); editMydetailsURL.setParameter("mydetaiId", Long.toString(mydetaiId)); editMydetailsURL.setParameter(actionRequest.ACTION_NAME, "editMydetails"); %> <p style="border:1px solid green;">Friendly Url Implementation. </p> <a href="<%=addMydetailsURL.toString()%>">Add Details</a> <a href="<%=editMydetailsURL.toString()%>" >Edit Details</a>
Step 2 : create one jsp page addmydetails.jsp and paste below code.
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%
PortletURL addMydetailsActionURL = renderResponse.createActionURL();
addMydetailsActionURL.setParameter(actionRequest.ACTION_NAME, "addMydetails");
%>
<h1> Add Details</h1>
<form action="<%=addMydetailsActionURL.toString()%>" method="POST">
My Details Id<br/>
<input type="text" name="<portlet:namespace/>mydetailsId" value=""/><br/>
My Name<br/>
<input type="text" name="<portlet:namespace/>myname" value=""/><br/>
<input type="submit" value="Add My Details"/>
</form>
Step 3 :-
Add below tags with mentioned value liferay-portlet.xml
<friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class> <friendly-url-mapping>myFriendlyUrl</friendly-url-mapping> <friendly-url-routes>com/test/routes.xml</friendly-url-routes>
Add below tags with mentioned value liferay-portlet.xml
<friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class> <friendly-url-mapping>myFriendlyUrl</friendly-url-mapping> <friendly-url-routes>com/test/routes.xml</friendly-url-routes>
<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd">
<liferay-portlet-app>
<portlet>
<portlet-name>my-details</portlet-name>
<icon>/icon.png</icon>
<friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class>
<friendly-url-mapping>myFriendlyUrl</friendly-url-mapping>
<friendly-url-routes>com/myxml/routes.xml</friendly-url-routes>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>
/js/main.js
</footer-portlet-javascript>
<css-class-wrapper>my-details-portlet</css-class-wrapper>
</portlet>
<role-mapper>
<role-name>administrator</role-name>
<role-link>Administrator</role-link>
</role-mapper>
<role-mapper>
<role-name>guest</role-name>
<role-link>Guest</role-link>
</role-mapper>
<role-mapper>
<role-name>power-user</role-name>
<role-link>Power User</role-link>
</role-mapper>
<role-mapper>
<role-name>user</role-name>
<role-link>User</role-link>
</role-mapper>
</liferay-portlet-app>
Step 4: create one xml file inside /web-inf/src/com/myxml folder with name routes.xml and paste below code.Step 5: Paste below code in java class Mydetails.java<?xml version="1.0"?> <!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> <!-- Friendly url when we access jsp pages(rendering from one jsp to another) --> <route> <pattern>/page/{jspPageName}>/pattern> <generated-parameter name="jspPage">/html/crud/{jspPageName}.jsp>/generated-parameter> </route> <!-- Friendly Url when we call action url --> <route> <pattern>/action/{actionName}>/pattern> <generated-parameter name="javax.portlet.action">{actionName}>/generated-parameter> <ignored-parameter name="p_auth"/> <ignored-parameter name="p_p_id"/> <implicit-parameter name="p_p_lifecycle">1>/implicit-parameter> <implicit-parameter name="p_p_state">normal>/implicit-parameter> <implicit-parameter name="p_p_mode">view>/implicit-parameter> </route> <!-- Friendly Url when we call action url passing parameter(Id)--> <route> <pattern>/action/{actionName}/mydetailsId/{mydetaiId:\d+}>/pattern> <generated-parameter name="javax.portlet.action">{actionName}>/generated-parameter> <generated-parameter name="mydetaiId">{mydetaiId}>/generated-parameter> <ignored-parameter name="p_auth"/> <ignored-parameter name="p_p_id"/> <implicit-parameter name="p_p_lifecycle">1>/implicit-parameter> <implicit-parameter name="p_p_state">normal>/implicit-parameter> <implicit-parameter name="p_p_mode">view>/implicit-parameter> </route> </routes>
package com.mypath; import java.io.IOException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletException; import com.liferay.util.bridges.mvc.MVCPortlet; /** * Portlet implementation class Mydetails */ public class Mydetails extends MVCPortlet { public void addmydetails(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { System.out.println("inside addmydetails..."); } public void editmydetails(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { System.out.println("inside editmydetails..."); } }