« 返回到 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 附件
103986 查看
平均 (0 票)
满分为 5,平均得分为 0.0。
评论
讨论主题回复 作者 日期
Hi, First of all, thanks for the customization... Anand Abhyankar 2009年5月15日 下午10:52
Hi Anand, What version are you using? This... Jeffrey Handa 2009年5月16日 上午5:49
I am using Liferay standard edition bundled... diba M 2009年6月16日 下午12:47
Hey Jeffrey, Its kind of urgent help I need in... diba M 2009年6月17日 上午11:48
Hi Diba, The user_ table isn't modified when... Jeffrey Handa 2009年6月23日 下午1:32
Hi, Thanks for this good wiki. And what about... Emilie R 2009年6月17日 上午6:39
To access existing attributes you would make... Jeffrey Handa 2009年6月23日 下午1:35
Hey guys, I use v5.2.3. I believe I did... Secret Developer 2009年7月1日 上午7:38
Nothing??? No one can help editing the create... Secret Developer 2009年7月6日 上午7:30
I don't want to be mean, don't get me wrong but... Secret Developer 2009年7月9日 上午6:35
I don't want to be mean, don't get me wrong but... Secret Developer 2009年7月9日 上午6:37
Found the problem. In liferay-hook.xml file... Secret Developer 2009年7月9日 上午7:04
Hey Baris, Thanks for pointing out the... Jeffrey Handa 2009年11月13日 下午6:29
Hi Jeffrey, Is it possible to have more... Emilie R 2009年10月9日 上午8:04
Emilie, Did you get this figured out? If not,... Jeffrey Handa 2009年11月13日 下午5:57
Have tried to create a jsp hook but it does not... Flavel --------- 2009年8月6日 上午11:46
Flavel, are you still having problems? If so,... Jeffrey Handa 2009年11月13日 下午5:52
I attempted this, and it all worked fine on my... Krista M Leopold 2009年11月3日 上午8:22
Hi Krista, Can you also post this question in... Jeffrey Handa 2009年11月13日 下午6: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年5月6日 上午4:15
Managed to solve this.. Just override the file... Gaurav Talwar 2010年5月12日 上午3:45
Hi Jeff, I have added a custom attribute... Gaurav Talwar 2010年5月12日 上午3:49
Hi - thanks for the article. All works well... David Hunter 2010年6月8日 下午1:16
Hi, Was able to configure as required, looking... Swapnil Gholap 2010年8月16日 下午11:19
Hey Jeffrey ! Thanks for the article .. I'd... Abhay Doshi 2011年2月15日 下午10:49
good work .i have added one field if i want to... bhavishya goyal 2011年3月3日 上午10:17
I was able to get this working on LF6 after I... Leonardo Santos-Macias 2011年5月18日 上午7:50
You can easily change the rendering of a... Liferay Blogger 2012年2月21日 下午2:52
hi guys , can any one tell me how can i add... Tejas patel 2012年3月1日 下午11:19
Hello All, I am using Liferay 6.1 CE version. I... Pradip A Bhatt 2014年2月27日 下午9:10
When defining the custom field from the Control... Jeffrey Handa 2014年2月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-5-15 下午10: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.
在 09-5-16 上午5:49 发帖以回复 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.
在 09-6-16 下午12:47 发帖以回复 Jeffrey Handa
Hi,
Thanks for this good wiki.

And what about existing attributes? For example, the street, the country, ...
在 09-6-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.
在 09-6-17 上午11:48 发帖以回复 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.
在 09-6-23 下午1:32 发帖以回复 diba M
To access existing attributes you would make use of Liferay's service layer.
在 09-6-23 下午1:35 发帖以回复 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?
在 09-7-1 上午7:38 发帖以回复 Jeffrey Handa
Nothing??? No one can help editing the create account page?????
在 09-7-6 上午7:30 发帖以回复 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
在 09-7-9 上午6:35 发帖以回复 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
在 09-7-9 上午6:37 发帖以回复 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!!
在 09-7-9 上午7:04 发帖以回复 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.
在 09-8-6 上午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?
在 09-10-9 上午8:04 发帖以回复 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!
在 09-11-3 上午8:22 发帖。
Flavel, are you still having problems? If so, can you post a message in the forums with the war file?
在 09-11-13 下午5:52 发帖以回复 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.
在 09-11-13 下午5:57 发帖以回复 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.
在 09-11-13 下午6:00 发帖以回复 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.
在 09-11-13 下午6:29 发帖以回复 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
在 09-11-16 上午8:42 发帖以回复 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
在 10-5-6 上午4:15 发帖。
Managed to solve this.. Just override the file html/taglib/ui/custom_attribute/page.jsp
在 10-5-12 上午3:45 发帖以回复 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
在 10-5-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-6-8 下午1:16 发帖。
Hi,
Was able to configure as required, looking forward to add more fields emoticon

Thank you for the article.
在 10-8-16 下午11: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-2-15 下午10: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-3-3 上午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 */;
在 11-5-18 上午7:50 发帖以回复 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
在 12-2-21 下午2:52 发帖以回复 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.
在 12-3-1 下午11:19 发帖以回复 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
在 14-2-27 下午9: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.
在 14-2-28 上午6:19 发帖以回复 Pradip A Bhatt