« Zurück zu Custom fields

Custom Fields and JSP Hooks

Introduction #

Our goal is to take advantage of several new Liferay Portal features to add a field to the Create Account portlet, and persist submitted values to the database. In the past (prior to Liferay 5.2), this would have most likely been accomplished using the EXT environment or a combination of Portal Hook Plugins. In this example, the following Liferay Portal features will be used to accomplish this:

  • Custom User Fields
  • Plugins SDK Hooks

Note - The steps in this article rely on several fixes that were added after Liferay 5.2.2 were released. You will need to use 5.2.3 when it becomes available , or build from source.

Creating Custom User Fields #

-Login to the portal as an administrator -Select Control Panel from the Dock -Select Users' from the Portal section of the left hand nav bar -Click Custom Fields from the top of the panel -Click the Add Custom Fields button -Enter home-town-newspaper in the Key field -Select Text Field - Indexed in the Type field -Select Save

You have now created a Custom Field that will be persisted to the database. By default, the field won't be available for all users, so we'll need to modify the permissions.

-Click the Actions button that corresponds to the home-town-newspaper custom field and select Permissions. -Add a check for Update and View on the Guest role (You may also want to add Update and View permissions for the User role as well if you want users to be able to update their choice through the My Accounts screen) -Click Submit.

Your custom field is now ready to be used.

Note - By using the hyphenated name for the Key field, Liferay provides some very nice features. Adding the key to the language files allows you to localize the term. Also, if the key is a term that's already been localized then you don't have to add it, it will just work. Lastly, Liferay will apply a parser that will pretty print the key for you, so it will become Home Town Newspaper when displayed as a label in the JSP.

Modifying the Create Accounts page #

Create Portal Hook Plugin #

This section assumes that you have already configured the Liferay Plugins SDK environment and have access to the Liferay Portal source code. If not, please refer to this article http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Plugins+SDK for information on setting up the Plugins SDK. The portal source code can be downloaded from the downloads http://www.liferay.com/web/guest/downloads/portal section of Liferay.com.

-Create a new folder in your plugins/hooks folder called customjsp-hook -Create a file called build.xml with the following contents:

<?xml version="1.0"?>
<project name="hook" basedir="." default="deploy">
      <import file="../build-common-hook.xml" />
</project>

-Create a new folder inside of customjsp-hook named docroot -Create a new folder inside of docroot named WEB-INF -Create a new file inside of WEB-INF named liferay-hook.xml with the following contents: Note - there should no square brackets in the 2nd line of the xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 5.2.0//EN"  "http://www.liferay.com/dtd/liferay-hook_5_2_0.dtd" >
<hook>
	<custom-jsp-dir>/WEB-INF/jsps</custom-jsp-dir>
</hook>

-Create a new file inside of WEB-INF named liferay-plugin-package.properties with the following contents:

name=customjsp
module-group-id=liferay
module-incremental-version=1
tags=
short-description=This hook contains a custom Create Accounts JSP
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=MIT
required-deployment-contexts=

-Create the following folder structure inside of the WEB-INF folder:

jsps/html/portlet/login

-Copy the file, create_account.jsp from liferay-portal-src-5.x.x/portal-web/docroot/html/portlet/login to customjsp-hook/docroot/WEB-INF/jsps/html/portlet/login

Add Custom Fields to Custom Create Account page #

-Open the create_account.jsp file and find the email address portion of the form (Search for key="email-address") -Insert the following code between the ending </div> tag of the mail section and the ending </fieldset> tag

<div class="exp-ctrl-holder">
	<liferay-ui:custom-attribute
		className="<%= User.class.getName() %>"
		classPK="<%= 0 %>"
		editable="<%= true %>"
		label="<%= true %>"
		name="home-town-newspaper"
	/>
</div>

- Save the file -From a command prompt, navigate to the plugins/hooks/customjsp-hook folder and deploy your custom hook using the following command:

ant deploy

-Watch the command window and the console to ensure the hook is deployed and registered successfully and available for use. -Log in to Liferay and from the SIgn In portlet, select Create Account to see the updated JSP with your custom field.

1 Anhang
103976 Angesehen
Durchschnitt (0 Stimmen)
Die durchschnittliche Bewertung ist 0.0 von max. 5 Sternen.
Kommentare
Antworten im Thread Autor Datum
Hi, First of all, thanks for the customization... Anand Abhyankar 15. Mai 2009 22:52
Hi Anand, What version are you using? This... Jeffrey Handa 16. Mai 2009 05:49
I am using Liferay standard edition bundled... diba M 16. Juni 2009 12:47
Hey Jeffrey, Its kind of urgent help I need in... diba M 17. Juni 2009 11:48
Hi Diba, The user_ table isn't modified when... Jeffrey Handa 23. Juni 2009 13:32
Hi, Thanks for this good wiki. And what about... Emilie R 17. Juni 2009 06:39
To access existing attributes you would make... Jeffrey Handa 23. Juni 2009 13:35
Hey guys, I use v5.2.3. I believe I did... Secret Developer 1. Juli 2009 07:38
Nothing??? No one can help editing the create... Secret Developer 6. Juli 2009 07:30
I don't want to be mean, don't get me wrong but... Secret Developer 9. Juli 2009 06:35
I don't want to be mean, don't get me wrong but... Secret Developer 9. Juli 2009 06:37
Found the problem. In liferay-hook.xml file... Secret Developer 9. Juli 2009 07:04
Hey Baris, Thanks for pointing out the... Jeffrey Handa 13. November 2009 18:29
Hi Jeffrey, Is it possible to have more... Emilie R 9. Oktober 2009 08:04
Emilie, Did you get this figured out? If not,... Jeffrey Handa 13. November 2009 17:57
Have tried to create a jsp hook but it does not... Flavel --------- 6. August 2009 11:46
Flavel, are you still having problems? If so,... Jeffrey Handa 13. November 2009 17:52
I attempted this, and it all worked fine on my... Krista M Leopold 3. November 2009 08:22
Hi Krista, Can you also post this question in... Jeffrey Handa 13. November 2009 18:00
I did, Jeffrey, and have since resolved my... Krista M Leopold 16. November 2009 08:42
Hi Jeff, Very nicely written article. Thanks! ... Gaurav Talwar 6. Mai 2010 04:15
Managed to solve this.. Just override the file... Gaurav Talwar 12. Mai 2010 03:45
Hi Jeff, I have added a custom attribute... Gaurav Talwar 12. Mai 2010 03:49
Hi - thanks for the article. All works well... David Hunter 8. Juni 2010 13:16
Hi, Was able to configure as required, looking... Swapnil Gholap 16. August 2010 23:19
Hey Jeffrey ! Thanks for the article .. I'd... Abhay Doshi 15. Februar 2011 22:49
good work .i have added one field if i want to... bhavishya goyal 3. März 2011 10:17
I was able to get this working on LF6 after I... Leonardo Santos-Macias 18. Mai 2011 07:50
You can easily change the rendering of a... Liferay Blogger 21. Februar 2012 14:52
hi guys , can any one tell me how can i add... Tejas patel 1. März 2012 23:19
Hello All, I am using Liferay 6.1 CE version. I... Pradip A Bhatt 27. Februar 2014 21:10
When defining the custom field from the Control... Jeffrey Handa 28. Februar 2014 06:19

Hi,
First of all, thanks for the customization of the Create Account portlet.
I have followed all the above steps, and able to get the label for Home Town Newspaper, but not getting the text box to enter the data on the create account portlet.
Any ideas.....?

Thanks in advance
>>> Anand
Gepostet am 15.05.09 22:52.
Hi Anand,

What version are you using? This won't work with 5.2.2 because you aren't able to give the 'update' permission to the 'guest' role (See http://issues.liferay.com/browse/LPS-3160). You'll need to build from the latest source in the SVN, or wait for the next bundle.
Gepostet am 16.05.09 05:49 als Antwort auf anand abhyankar.
I am using Liferay standard edition bundled tomcat 5.2.3. I followed the above steps and the Create account page do not show the field label or the text box.

And again does it really create the fields to the user_ table in lportal schema? I could not locate the new field in lportal schema, so wonder how it persist.
Gepostet am 16.06.09 12:47 als Antwort auf Jeffrey Handa.
Hi,
Thanks for this good wiki.

And what about existing attributes? For example, the street, the country, ...
Gepostet am 17.06.09 06:39.
Hey Jeffrey,

Its kind of urgent help I need in this issue.

I am using Liferay standard edition bundled tomcat 5.2.3. I followed the above steps and the Create account page do not show the field label or the text box.

And again does it really create the fields to the user_ table in lportal schema? I could not locate the new field in lportal schema, so wonder how it persist.
Gepostet am 17.06.09 11:48 als Antwort auf Jeffrey Handa.
Hi Diba,

The user_ table isn't modified when using Custom Attributes. The attributes are stored using Liferay's ExpandoService.

I am not sure why you're not seeing the field label or the text box, but I would start by making sure that the hook is working correctly. Are any changes to the JSP showing up after deploying? If the hook is working, then I would double check the permissions on the custom attribute.
Gepostet am 23.06.09 13:32 als Antwort auf diba M.
To access existing attributes you would make use of Liferay's service layer.
Gepostet am 23.06.09 13:35 als Antwort auf Emilie R.
Hey guys,

I use v5.2.3. I believe I did everything perfectly but the text box is not showing up in create account page. Any one has a solution for this?
Gepostet am 01.07.09 07:38 als Antwort auf Jeffrey Handa.
Nothing??? No one can help editing the create account page?????
Gepostet am 06.07.09 07:30 als Antwort auf Baris Sener.
I don't want to be mean, don't get me wrong but can someone fix this issue???

I keep getting this error, that's why it doesn't show up on create account page.

12:56:14,195 INFO [HookHotDeployListener:442] Hook for customjsp-hook was unregistered
12:56:14,250 INFO [HookHotDeployListener:184] Registering hook for customjsp-hook
12:56:14,252 ERROR [HotDeployUtil:111] com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering hook for customjsp-hook
com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering hook for customjsp-hook
at com.liferay.portal.deploy.hot.BaseHotDeployListener.throwHotDeployException(Base­HotDeployListener.java:58)
at com.liferay.portal.deploy.hot.HookHotDeployListener.invokeDeploy(HookHotDeployLi­stener.java:101)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil._doFireDeployEvent(HotDeployU­til.java:108)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil._fireDeployEvent(HotDeployUti­l.java:153)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil.fireDeployEvent(HotDeployUtil­.java:43)
at com.liferay.portal.kernel.servlet.HookContextListener.contextInitialized(HookCon­textListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843­)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1116)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1214)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.ja­va:117)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337­)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1601)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1610)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(Containe­rBase.java:1590)
at java.lang.Thread.run(Thread.java:619)
Caused by: com.liferay.portal.kernel.xml.DocumentException: Error on line 2 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:377)
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:388)
at com.liferay.portal.kernel.xml.SAXReaderUtil.read(SAXReaderUtil.java:156)
at com.liferay.portal.deploy.hot.HookHotDeployListener.doInvokeDeploy(HookHotDeploy­Listener.java:191)
at com.liferay.portal.deploy.hot.HookHotDeployListener.invokeDeploy(HookHotDeployLi­stener.java:98)
... 15 more
Gepostet am 09.07.09 06:35 als Antwort auf Baris Sener.
I don't want to be mean, don't get me wrong but can someone fix this issue???

I keep getting this error, that's why it doesn't show up on create account page.

12:56:14,195 INFO [HookHotDeployListener:442] Hook for customjsp-hook was unregistered
12:56:14,250 INFO [HookHotDeployListener:184] Registering hook for customjsp-hook
12:56:14,252 ERROR [HotDeployUtil:111] com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering hook for customjsp-hook
com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering hook for customjsp-hook
at com.liferay.portal.deploy.hot.BaseHotDeployListener.throwHotDeployException(Base­HotDeployListener.java:58)
at com.liferay.portal.deploy.hot.HookHotDeployListener.invokeDeploy(HookHotDeployLi­stener.java:101)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil._doFireDeployEvent(HotDeployU­til.java:108)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil._fireDeployEvent(HotDeployUti­l.java:153)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil.fireDeployEvent(HotDeployUtil­.java:43)
at com.liferay.portal.kernel.servlet.HookContextListener.contextInitialized(HookCon­textListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843­)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1116)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1214)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.ja­va:117)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337­)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1601)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1610)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(Containe­rBase.java:1590)
at java.lang.Thread.run(Thread.java:619)
Caused by: com.liferay.portal.kernel.xml.DocumentException: Error on line 2 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:377)
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:388)
at com.liferay.portal.kernel.xml.SAXReaderUtil.read(SAXReaderUtil.java:156)
at com.liferay.portal.deploy.hot.HookHotDeployListener.doInvokeDeploy(HookHotDeploy­Listener.java:191)
at com.liferay.portal.deploy.hot.HookHotDeployListener.invokeDeploy(HookHotDeployLi­stener.java:98)
... 15 more
Gepostet am 09.07.09 06:37 als Antwort auf Baris Sener.
Found the problem. In liferay-hook.xml file remove the " [ ] " around the link.

After doing this, if the text box is still not showing up in the create account page, add this statement to your liferay-hook.xml file.

<custom-jsp-dir>/META-INF/custom_jsps</custom-jsp-dir>

Cheers!!
Gepostet am 09.07.09 07:04 als Antwort auf Baris Sener.
Have tried to create a jsp hook but it does not seem to override the jsp. Have the war file, if anybody is interested in helping.
Gepostet am 06.08.09 11:46.
Hi Jeffrey,

Is it possible to have more details, please?

What I exactly want to do is adding a zip field in the create account page. Do I have to use the custom attribute, or is it a way to add the existing zip field of the address?

At the moment, I can add the field "graphically", but the value isn't stored anyway...

Any idea?
Gepostet am 09.10.09 08:04 als Antwort auf Jeffrey Handa.
I attempted this, and it all worked fine on my workstation, but when I tried to deploy on my test server, which is running in a non-root context, it bombed. Does it have something to do with the war file randomly adding a version to the name of the war file, as in customjsp-hook-5.2.3.1.war ? Please help!
Gepostet am 03.11.09 08:22.
Flavel, are you still having problems? If so, can you post a message in the forums with the war file?
Gepostet am 13.11.09 17:52 als Antwort auf Flavel ---------.
Emilie,

Did you get this figured out? If not, can you post this question in the forums? You can put a link to your forum post here on this wiki. By keeping all the questions in the forums we will minimize the number of places that community members will need to look for answers. You'll also get a larger pool of possible responders.

BTW - You don't need a custom attribute to store zip, but there would be some steps to persist the zip to the db.
Gepostet am 13.11.09 17:57 als Antwort auf Emilie R.
Hi Krista,

Can you also post this question in the forum? Off the top of my head, I am not sure, but by posting this in the forum you'll get more people looking at the question that might be able to help. If you're already posted in the forum, I apologize, I've been heads-down on some projects and must have missed it.
Gepostet am 13.11.09 18:00 als Antwort auf Krista M Leopold.
Hey Baris,

Thanks for pointing out the mistake. I am not sure why, but creole keeps adding the brackets. I've added a note so hopefully others wont' run into this.
Gepostet am 13.11.09 18:29 als Antwort auf Baris Sener.
I did, Jeffrey, and have since resolved my problem, thanks!!
http://www.liferay.com/web/guest/community/forums/-/message_boards/message/42473­84
Gepostet am 16.11.09 08:42 als Antwort auf Jeffrey Handa.
Hi Jeff,

Very nicely written article. Thanks!

I want to display a checkbox on the create account page. I have managed to get a select with boolean values.

<liferay-ui:custom-attribute
className="<%= User.class.getName() %>"
classPK="<%= 0 %>"
editable="<%= true %>"
label="<%= true %>"
name="email-subscription"
/>

I want to convert the above select to checkbox in JSP file. Any ideas to do it? Thanks
Gepostet am 06.05.10 04:15.
Managed to solve this.. Just override the file html/taglib/ui/custom_attribute/page.jsp
Gepostet am 12.05.10 03:45 als Antwort auf Gaurav Talwar.
Hi Jeff,

I have added a custom attribute 'postcode' which i have added to enterprise_admin/user/details.jsp. It gets saved correctly but its not populating the field on page load.. Any ideas whats missing?


My code is:

<liferay-ui:custom-attribute
className="<%= User.class.getName() %>"
classPK="<%= 0 %>"
editable="<%= true %>"
label="<%= true %>"
name="postcode"
/>
</div>

Thanks,
Gaurav Talwar
Gepostet am 12.05.10 03:49.
Hi - thanks for the article.

All works well except the localization.

I have added Language-ext_xx.properties files under :
/hooks/docroot/WEB-INF/classes/content/
however it is not being recognised using the message key method in the Create Account jsp :
<liferay-ui:message key="CustomAttribute" />

Any ideas ? Thanks.
Gepostet am 08.06.10 13:16.
Hi,
Was able to configure as required, looking forward to add more fields emoticon

Thank you for the article.
Gepostet am 16.08.10 23:19.
Hey Jeffrey ! Thanks for the article .. I'd like to know how to display the custom attribute in Liferay 6 - as this uses the AUI tags for generating the form etc.

Thanks !
Gepostet am 15.02.11 22:49.
good work .i have added one field if i want to display this field in another jsp than what should be done for it
Gepostet am 03.03.11 10:17.
I was able to get this working on LF6 after I followed EXACTLY all the steps in the wiki.

I tried a "teacher" field and activated all permissions and didn't work. It worked only with the hyphens in "home-town-newspaper" and only the permissions in guest and user.

As per the location of the saved data, this table is created and the data saved there

CREATE TABLE `expandocolumn` (
`columnId` bigint(20) NOT NULL,
`companyId` bigint(20) DEFAULT NULL,
`tableId` bigint(20) DEFAULT NULL,
`name` varchar(75) DEFAULT NULL,
`type_` int(11) DEFAULT NULL,
`defaultData` longtext,
`typeSettings` longtext,
PRIMARY KEY (`columnId`),
UNIQUE KEY `IX_FEFC8DA7` (`tableId`,`name`),
KEY `IX_A8C0CBE8` (`tableId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `expandocolumn` VALUES (10407,10132,10406,'aboutMe',15,'',''),(11202,10132,11201,'supervisor\'s name',15,'','indexable=true\n'),(11402,10132,11018,'home-town-newspaper',15,'','­indexable=true\n');
/*!40000 ALTER TABLE `expandocolumn` ENABLE KEYS */;
Gepostet am 18.05.11 07:50 als Antwort auf bhavishya goyal.
You can easily change the rendering of a true/false custom field to a check box with a hook - I described how I did it in this article - http://liferay.bdedov.eu/2012/02/render-truefalse-custom-field-type-as.html
Gepostet am 21.02.12 14:52 als Antwort auf Leonardo Santos-Macias.
hi guys ,
can any one tell me how can i add custom fields on my account page
where user photo ,and other details are shown.
thanks,
Tejas patel.
Gepostet am 01.03.12 23:19 als Antwort auf Liferay Blogger.
Hello All,
I am using Liferay 6.1 CE version. I added my custom attribute in User entity from control panel. I want to add this my custom field in create_account.jsp hook page. I added like this way.

<div class="exp-ctrl-holder">
<liferay-ui:custom-attribute className="<%=User.class.getName()%>"
classPK="<%=0%>" editable="<%=true%>" label="<%=true%>"
name="user-default-group-name" />

After that i am trying to getting this value in UserLocalService hook class which extends UserLocalServiceWrapper class - (hook). I find each value from this but can not able to get my custom field value.

I am trying to get custom field value like this.

String defaultGroup = (String) User.getExpandoBridge().getAttribute("defaultGroupName");
log.info("Default Group Name :" + defaultGroup);

But I am getting null value or default value which I set at the time of adding custom attribute. I can not find my entered value from jsp page.

Please help me.

Regards :
Pradip Bhatt
pradip.bhatt@aspiresoftware.in
Gepostet am 27.02.14 21:10.
When defining the custom field from the Control Panel, what Key did you define? In your jsp snippet you're using user-default-group-name but in your service wrapper hook you're using defaultGroupName.
Gepostet am 28.02.14 06:19 als Antwort auf Pradip A Bhatt.