« 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 画像の固定
103977 参照数
平均 (0 投票)
平均評価は0.0星中の5です。
コメント
コメント 作成者 日時
Hi, First of all, thanks for the customization... Anand Abhyankar 2009/05/15 22:52
Hi Anand, What version are you using? This... Jeffrey Handa 2009/05/16 5:49
I am using Liferay standard edition bundled... diba M 2009/06/16 12:47
Hey Jeffrey, Its kind of urgent help I need in... diba M 2009/06/17 11:48
Hi Diba, The user_ table isn't modified when... Jeffrey Handa 2009/06/23 13:32
Hi, Thanks for this good wiki. And what about... Emilie R 2009/06/17 6:39
To access existing attributes you would make... Jeffrey Handa 2009/06/23 13:35
Hey guys, I use v5.2.3. I believe I did... Secret Developer 2009/07/01 7:38
Nothing??? No one can help editing the create... Secret Developer 2009/07/06 7:30
I don't want to be mean, don't get me wrong but... Secret Developer 2009/07/09 6:35
I don't want to be mean, don't get me wrong but... Secret Developer 2009/07/09 6:37
Found the problem. In liferay-hook.xml file... Secret Developer 2009/07/09 7:04
Hey Baris, Thanks for pointing out the... Jeffrey Handa 2009/11/13 18:29
Hi Jeffrey, Is it possible to have more... Emilie R 2009/10/09 8:04
Emilie, Did you get this figured out? If not,... Jeffrey Handa 2009/11/13 17:57
Have tried to create a jsp hook but it does not... Flavel --------- 2009/08/06 11:46
Flavel, are you still having problems? If so,... Jeffrey Handa 2009/11/13 17:52
I attempted this, and it all worked fine on my... Krista M Leopold 2009/11/03 8:22
Hi Krista, Can you also post this question in... Jeffrey Handa 2009/11/13 18:00
I did, Jeffrey, and have since resolved my... Krista M Leopold 2009/11/16 8:42
Hi Jeff, Very nicely written article. Thanks! ... Gaurav Talwar 2010/05/06 4:15
Managed to solve this.. Just override the file... Gaurav Talwar 2010/05/12 3:45
Hi Jeff, I have added a custom attribute... Gaurav Talwar 2010/05/12 3:49
Hi - thanks for the article. All works well... David Hunter 2010/06/08 13:16
Hi, Was able to configure as required, looking... Swapnil Gholap 2010/08/16 23:19
Hey Jeffrey ! Thanks for the article .. I'd... Abhay Doshi 2011/02/15 22:49
good work .i have added one field if i want to... bhavishya goyal 2011/03/03 10:17
I was able to get this working on LF6 after I... Leonardo Santos-Macias 2011/05/18 7:50
You can easily change the rendering of a... Liferay Blogger 2012/02/21 14:52
hi guys , can any one tell me how can i add... Tejas patel 2012/03/01 23:19
Hello All, I am using Liferay 6.1 CE version. I... Pradip A Bhatt 2014/02/27 21:10
When defining the custom field from the Control... Jeffrey Handa 2014/02/28 6: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
投稿日時:09/05/15 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.
anand abhyankarへのコメント。投稿日時:09/05/16 5:49
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.
Jeffrey Handaへのコメント。投稿日時:09/06/16 12:47
Hi,
Thanks for this good wiki.

And what about existing attributes? For example, the street, the country, ...
投稿日時:09/06/17 6: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.
Jeffrey Handaへのコメント。投稿日時:09/06/17 11:48
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.
diba Mへのコメント。投稿日時:09/06/23 13:32
To access existing attributes you would make use of Liferay's service layer.
Emilie Rへのコメント。投稿日時:09/06/23 13:35
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?
Jeffrey Handaへのコメント。投稿日時:09/07/01 7:38
Nothing??? No one can help editing the create account page?????
Baris Senerへのコメント。投稿日時:09/07/06 7:30
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
Baris Senerへのコメント。投稿日時:09/07/09 6:35
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
Baris Senerへのコメント。投稿日時:09/07/09 6:37
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!!
Baris Senerへのコメント。投稿日時:09/07/09 7:04
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.
投稿日時:09/08/06 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?
Jeffrey Handaへのコメント。投稿日時:09/10/09 8:04
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!
投稿日時:09/11/03 8:22
Flavel, are you still having problems? If so, can you post a message in the forums with the war file?
Flavel ---------へのコメント。投稿日時:09/11/13 17:52
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.
Emilie Rへのコメント。投稿日時:09/11/13 17:57
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.
Krista M Leopoldへのコメント。投稿日時:09/11/13 18:00
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.
Baris Senerへのコメント。投稿日時:09/11/13 18:29
I did, Jeffrey, and have since resolved my problem, thanks!!
http://www.liferay.com/web/guest/community/forums/-/message_boards/message/42473­84
Jeffrey Handaへのコメント。投稿日時:09/11/16 8:42
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
投稿日時:10/05/06 4:15
Managed to solve this.. Just override the file html/taglib/ui/custom_attribute/page.jsp
Gaurav Talwarへのコメント。投稿日時:10/05/12 3:45
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
投稿日時:10/05/12 3: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.
投稿日時:10/06/08 13:16
Hi,
Was able to configure as required, looking forward to add more fields emoticon

Thank you for the article.
投稿日時:10/08/16 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 !
投稿日時:11/02/15 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
投稿日時:11/03/03 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 */;
bhavishya goyalへのコメント。投稿日時:11/05/18 7:50
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
Leonardo Santos-Maciasへのコメント。投稿日時:12/02/21 14:52
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.
Liferay Bloggerへのコメント。投稿日時:12/03/01 23:19
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
投稿日時:14/02/27 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.
Pradip A Bhattへのコメント。投稿日時:14/02/28 6:19