留言板

Help with Friendly URL

James Smith,修改在11 年前。

Help with Friendly URL

Junior Member 帖子: 29 加入日期: 12-2-11 最近的帖子
Hi,
I am trying to make my the Portlet URL of my form submission to be user friendly. Instead of

http://localhost:8080/web/guest/contacts?p_auth=JacSd2wm&p_p_id=contacts_WAR_contactsportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_contacts_WAR_contactsportlet_javax.portlet.action=submitContactRequest

I want something like:
http://localhost:8080/web/guest/contacts/submit

I tried using DefaultFriendlyURLMapper, but I did not have any success.


I tried extending BaseFriendlyURLMapper myself, but I keep getting

08:41:55,269 INFO [stdout] (http--127.0.0.1-8080-1) 08:41:55,268 INFO [PortalImpl:4873] Current URL /web/guest/contacts generates exception: null

if I just goto http://localhost:8080/web/guest/contacts just to view my form. Can somebody help me?



package com.website.contacts;

import java.util.Map;
import javax.portlet.PortletMode;
import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
import com.liferay.portal.kernel.portlet.LiferayPortletURL;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.GetterUtil;

/**
* Contacts' URL Mapper that maps default portlet URLs to a more user friendly URL.
*
*/
public class ContactsFriendlyURLMapper extends BaseFriendlyURLMapper
{
// Constants
protected static final String MAPPING_NAME = "contacts";
protected static final String PORTLET_ID = "contacts_WAR_contactsportlet";
protected static final String PORTLET_ID_PARAM = "p_p_id";
protected static final String LIFECYCLE_STAGE_PARAM = "p_p_lifecycle";
protected static final String PORTLET_STATE_PARAM = "p_p_state";
protected static final String PORTLET_MODE_PARAM = "p_p_mode";
protected static final String PORTLET_COL_ID_PARAM = "p_p_col_id";
protected static final String LAYOUT_COUNT_PARAM = "p_p_col_count";
protected static final String ACTION_PARAM = "action";


/**
* Returns the friendly URL mapping for this portlet.
*
* <p>
* The friendly URL mapping is used by Liferay to identify the portlet a
* friendly URL refers to. It generally appears directly after the
* <code>/-/</code> in the URL.
* </p>
*
* <p>
* For instance, the blogs portlet mapping is &quot;blogs&quot;. This
* produces friendly URLs similar to
* <code>http://www.liferay.com/web/guest/blog/-/blogs/example-post</code>
* </p>
*
* @return the friendly URL mapping for this portlet, not including any
* leading or trailing slashes
*/
public String getMapping()
{
System.out.println("getMapping()***!!!***");
return MAPPING_NAME;
}

/**
* Generates a friendly URL path from the portlet URL object.
* @param liferayPortletURL the portlet URL object to generate a friendly
* URL for
* @return the generated friendly URL, or <code>null</code> if one cannot be
* built. Returning <code>null</code> will cause a normal portlet
* URL to be generated.
*/
public String buildPath(LiferayPortletURL liferayPortletURL)
{
System.out.println("buildPath()" + "***!!!*** ==> " + liferayPortletURL);
String action = GetterUtil.getString(liferayPortletURL.getParameter(ACTION_PARAM));
String friendlyURL = StringPool.FORWARD_SLASH + getMapping() + StringPool.FORWARD_SLASH + action;

// Remove the following parameters from the friendly URL
liferayPortletURL.addParameterIncludedInPath(PORTLET_ID_PARAM);
liferayPortletURL.addParameterIncludedInPath(LIFECYCLE_STAGE_PARAM);
liferayPortletURL.addParameterIncludedInPath(PORTLET_STATE_PARAM);
liferayPortletURL.addParameterIncludedInPath(PORTLET_MODE_PARAM);
liferayPortletURL.addParameterIncludedInPath(PORTLET_COL_ID_PARAM);
liferayPortletURL.addParameterIncludedInPath(LAYOUT_COUNT_PARAM);
liferayPortletURL.addParameterIncludedInPath(ACTION_PARAM);

System.out.println("returning friendlyURL == " + friendlyURL);

return friendlyURL;
}

/**
* Returns the ID of this portlet
* @return the ID of this portlet, not including the instance ID
*/
public String getPortletId()
{
System.out.println("getPortletId()***!!!***");
return PORTLET_ID;
}

/**
* Returns <code>true</code> if the friendly URLs for this mapper should
* include the friendly URL separator.
*
* <p>
* Typically, friendly URLs will include the separator &quot;/-/&quot;
* before the friendly URL mapping. If this method returns
* <code>false</code>, a single slash will be used instead.
* </p>
*
* <p>
* This method is called by {@link com.liferay.portal.util.PortalImpl} when
* a friendly URL is processed.
* </p>
*
* <p>
* <b>It is strongly recommended that this method always return
* <code>true</code></b>.
* </p>
*
* @return <code>true</code> if the &quot;/-/&quot; separator should be
* included in friendly URLs, or <code>false</code> if only a
* &quot;/&quot; should be used
*/
public boolean isCheckMappingWithPrefix()
{
System.out.println("isCheckMappingWithPrefix()***!!!***");
return false;
}

/**
* Populates the parameter map with values parsed from the friendly URL
* path.
*
* <p>
* This method is called by {@link com.liferay.portal.util.PortalImpl} when
* a friendly URL is processed.
* </p>
*
* @param friendlyURLPath the friendly URL path, including a leading slash
* and the friendly URL mapping. For example:
* <code>/blogs/example-post</code>
* @param parameterMap the parameter map. Entries added to this map must be
* namespaced.
* @param requestContext the request context
* @see BaseFriendlyURLMapper#addParameter(Map, String, String)
* @see BaseFriendlyURLMapper#addParameter(String, Map, String, String)
*/
public void populateParams(String friendlyURLPath,
Map<String, String[]> parameterMap,
Map<String, Object> requestContext)
{
System.out.println("populateParams()***!!!*** ==> " + friendlyURLPath);

if(friendlyURLPath != null && !friendlyURLPath.equals("/contacts/"))
{
System.out.println("populateParams()11111 ***!!!*** ==> " + friendlyURLPath);

// Add back to query string
addParameter(parameterMap, PORTLET_ID_PARAM, PORTLET_ID);
addParameter(parameterMap, LIFECYCLE_STAGE_PARAM, "1");
addParameter(parameterMap, PORTLET_STATE_PARAM, "view");
addParameter(parameterMap, PORTLET_MODE_PARAM, PortletMode.VIEW);
addParameter(parameterMap, PORTLET_COL_ID_PARAM, "column-1");
addParameter(parameterMap, LAYOUT_COUNT_PARAM, "1");
addParameter(parameterMap, ACTION_PARAM, "submitContactRequest");
}
else
{
String namespace = getNamespace();
System.out.println("populateParams()22222 ***!!!*** ==> " + friendlyURLPath + "=> " + namespace);
addParameter(parameterMap, PORTLET_ID_PARAM, PORTLET_ID);
addParameter(parameterMap, LIFECYCLE_STAGE_PARAM, "0");
addParameter(parameterMap, PORTLET_STATE_PARAM, "normal");
addParameter(parameterMap, PORTLET_MODE_PARAM, PortletMode.VIEW);
addParameter(parameterMap, PORTLET_COL_ID_PARAM, "column-1");
addParameter(parameterMap, LAYOUT_COUNT_PARAM, "1");
}
}
}


<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.1.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_1_0.dtd">

<liferay-portlet-app>
<portlet>
<portlet-name>contacts</portlet-name>
<icon>/icon.png</icon>
<friendly-url-mapper-class>com.website.contacts.ContactsFriendlyURLMapper</friendly-url-mapper-class>
<instanceable>false</instanceable>
</portlet>
</liferay-portlet-app>
James Smith,修改在11 年前。

RE: Help with Friendly URL

Junior Member 帖子: 29 加入日期: 12-2-11 最近的帖子
Please help..I am stuck...emoticon
Ashraf habibi,修改在11 年前。

RE: Help with Friendly URL

Junior Member 帖子: 32 加入日期: 11-5-13 最近的帖子
Have you resolve this problem?
Ashraf habibi,修改在11 年前。

RE: Help with Friendly URL

Junior Member 帖子: 32 加入日期: 11-5-13 最近的帖子
Ashraf habibi:
Have you resolve this problem?


I resolved this problem...see this link See the post here


Thanks
Ashraf
thumbnail
Subhash Pavuskar,修改在11 年前。

RE: Help with Friendly URL

Regular Member 帖子: 234 加入日期: 12-3-13 最近的帖子
Hi James,

Did you Followed This steps Link ?
thumbnail
Bart Simpson,修改在11 年前。

RE: Help with Friendly URL

Liferay Master 帖子: 522 加入日期: 11-8-29 最近的帖子
Since you tried to go to BaseFriendlyURLMapper , almost all the request will go through it. and there is something wrong there. The url that you want to achive is more of a page url , it doesn't contain any friendlyURLSeparator (/-/) which should have been a better way to do it. And provide you mapping routes in the liferay-portlet.xml
inside the tag <friendly-url-routes>

Also check the exception you get when you try to access this url , it should give you the clue.