Add HTML Editor to Portlet

Security Precaution #

Allowing users to embed HTML on your site is risky! XSS and Phishing attacks become possible, so it is important that any HTML permissions are only given to trusted users.

Introduction #

If you are building a portlet that takes in user input, you may be interested in adding a WYSIWYG HTML editor to your portlet. Using the portal's existing tags, it doesnt take much to add this. Here are the steps to take:

Getting access to the Portals Tags in your JSP#

You must add these lines to your JSP before you can start using the Portal's tags

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>

<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

Adding the Editor#

We will be adding two lines of code. The first line will add the actual WYSIWYG editor. The second line of code is a hidden input field which will store the actual html code of the wysiwyg content. You may or may not need the second line depending on how you are using this WYSIWYG editor. If you need the value of the WYSIWYG editor to be submitted with a form, then you will need this line. However, if you are just using javascript to parse the value (and perhaps sending it via ajax), then you will not need this hidden field.

<liferay-ui:input-editor />

<input name="<portlet:namespace />htmlCodeFromEditorPlacedHere" type="hidden" value="" />

Adding Javascript#

By default, the editor will call "initEditor()" to try and pre-populate the body of the editor. In this example, when the editor loads, it will have the value of "scott was here" in bold.

The second method is used to pull out the html code from the editor. One common way to use this method is to attach it to your "save/submit" button so that the value in the wysiwyg editor will be copied into the hidden input field just before the form is submitted.

<script type="text/javascript">
            function <portlet:namespace />initEditor() {
                        return '<font style="font-weight: bold">scott was here</font>';
            }

            function <portlet:namespace />extractCodeFromEditor() {
                        var x = document.<portlet:namespace />fm.<portlet:namespace />htmlCodeFromEditorPlacedHere.value = window.<portlet:namespace />editor.getHTML();
 
                        alert(x);
            }
</script>
0 附件
128313 查看
平均 (6 票)
满分为 5,平均得分为 3.1666666666666665。
评论
讨论主题回复 作者 日期
Hi i have followed above lines of code in my... Suhail Ahmed 2008年12月3日 上午3:27
This was cool.. thanks Kelly C 2010年3月31日 下午3:34
Hi, I'm having a research about strut... Tam Nguyen Minh 2010年11月30日 下午8:04
Hi, Good Wiki, But I want to add one thing... Mati-ur-Rehman Khan 2010年12月29日 上午6:31
Hi Mati Khan, I'm using Liferay 6.0 bundle... phuoc nguyen 2011年1月17日 上午8:48
Hi, I'm also using Liferay 6.0 bundle with... Man M 2011年4月11日 上午5:33
Hi, I've found my problem. The solution is:... Man M 2011年4月12日 上午9:01
For More on Liferay UI check this out ... Rohit Salecha 2011年5月19日 上午5:29
How i can received text and image from this... Nguyen Le 2012年5月27日 上午1:47

Hi


i have followed above lines of code in my liferay4.4.2, it's throwing javascript error that

"textarea.value = parent.initEditor()"

is not defined

waiting for your reply


thanks in advance
在 08-12-3 上午3:27 发帖。
在 10-3-31 下午3:34 发帖。
Hi,
I'm having a research about strut portlet, so i use beans to communicate with database. But i have a problem when insert text from fck into database. What should i do if i want to integrate a bean into fck ?
Thanks
在 10-11-30 下午8:04 发帖。
Hi,

Good Wiki, But I want to add one thing more to it.

For getting value from editor using

window.<portlet:namespace />editor.getHTML();

doesn't work all the time.

My scenario was, in AUI.Dialog I wanted to show Editor, then read value, and afterwards post it using Ajax post. So First time it works well.
But second time when i try to get value it says "editor.getHTML function not exists".
So way around was using
document.getElementById('<portlet:namespace/>editor').contentWindow.getHTML();

Ho­pe it helps someone.

Anyways thanks Liferay for a nice editor approach.

Cheers emoticon
在 10-12-29 上午6:31 发帖。
Hi Mati Khan,

I'm using Liferay 6.0 bundle Tomcat. I follow this instruction but I can't get the content of CKEditor. Could you please kindly tell me how to do?

Thanks and best regards,
在 11-1-17 上午8:48 发帖以回复 Mati Khan
Hi,
I'm also using Liferay 6.0 bundle with Tomcat and I have the same problem as phuoc nguyen. I can't get the content of the editor.
I've tried it as explained above, but it doesn't work.

My Code:
<form method="post" action="<%=send_email%>" enctype="multipart/form-data" name="<portlet:namespace />fm" >
<liferay-ui:input-editor />
<input name="<portlet:namespace />htmlCodeFromEditorPlacedHere" type="hidden" value="" />

<script type="text/javascript">
function <portlet:namespace />initEditor() {
return '<font style="font-weight: bold">scott was here</font>';
}
function <portlet:namespace />extractCodeFromEditor() {
var x = document.<portlet:namespace />fm.<portlet:namespace />htmlCodeFromEditorPlacedHere.value = window.<portlet:namespace />editor.getHTML();

alert(x);
}
</script>


<input type="submit" onclick="extractCodeFromEditor()"
value="<liferay-ui:message key="send" />">

In my java ActionClass I want to read the content by using:
String getHiddenValue = ParamUtil.getString(
actionRequest, "htmlCodeFromEditorPlacedHere");
But the String getHiddenValue is always empty.

I also tried it with:
<input type="button" onclick="extractCodeFromEditor()"
value="<liferay-ui:message key="send" />">
and:
function <portlet:namespace />extractCodeFromEditor() {
var x = document.<portlet:namespace />fm.<portlet:namespace />htmlCodeFromEditorPlacedHere.value = window.<portlet:namespace />editor.getHTML();
alert(x);
submitForm(document.<portlet:namespace />fm);
}
In this case when I click on the button nothing happens. I think submitForm is not called

Using document.getElementById('<portlet:namespace/>editor').contentWindow.getHTML();
do­es also not work.

Does anyone know what I did wrong? And how I can solve my problem.
thanks in advance!
在 11-4-11 上午5:33 发帖以回复 phuoc nguyen
Hi,
I've found my problem.
The solution is:
Because I'm using enctype="multipart/form-data" in my form-tag, I can't use actionRequest,
I have to use UploadPortletRequest req = PortalUtil .getUploadPortletRequest(actionRequest);
With String body = ParamUtil.getString(req, "content"); I can use the content in my java Action Class.

The JavaScript Method should be called in the onsubmit of the form-tag and the <portlet:namespace /> is important.
<form method="post" action="<%=send_email%>" enctype="multipart/form-data" onsubmit="<portlet:namespace />extractCodeFromEditor()" name="<portlet:namespace />fm" >

Best Regards.
在 11-4-12 上午9:01 发帖以回复 Manuel M
For More on Liferay UI check this out

http://liferaydemystified.blogspot.com/2011/05/liferay-ui-tabs.html
在 11-5-19 上午5:29 发帖以回复 Man M
How i can received text and image from this editor, please help me
在 12-5-27 上午1:47 发帖以回复 Rohit Salecha