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 添付ファイル
128311 参照数
平均 (6 投票)
平均評価は3.1666666666666665星中の5です。
コメント
コメント 作成者 日時
Hi i have followed above lines of code in my... Suhail Ahmed 2008/12/03 3:27
This was cool.. thanks Kelly C 2010/03/31 15:34
Hi, I'm having a research about strut... Tam Nguyen Minh 2010/11/30 20: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/01/17 8:48
Hi, I'm also using Liferay 6.0 bundle with... Man M 2011/04/11 5:33
Hi, I've found my problem. The solution is:... Man M 2011/04/12 9:01
For More on Liferay UI check this out ... Rohit Salecha 2011/05/19 5:29
How i can received text and image from this... Nguyen Le 2012/05/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/03 3:27
投稿日時:10/03/31 15: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 20: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,
Mati Khanへのコメント。投稿日時:11/01/17 8:48
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!
phuoc nguyenへのコメント。投稿日時:11/04/11 5:33
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.
Manuel Mへのコメント。投稿日時:11/04/12 9:01
For More on Liferay UI check this out

http://liferaydemystified.blogspot.com/2011/05/liferay-ui-tabs.html
Man Mへのコメント。投稿日時:11/05/19 5:29
How i can received text and image from this editor, please help me
Rohit Salechaへのコメント。投稿日時:12/05/27 1:47