使用 jspf 钩子自定义 LifeRay portlet

Posted

技术标签:

【中文标题】使用 jspf 钩子自定义 LifeRay portlet【英文标题】:Customizing LifeRay portlet using jspf hook 【发布时间】:2013-12-12 00:37:03 【问题描述】:

我需要自定义创建帐户 portlet,我想隐藏归档的“中间名”,但该字段存在于文件 /html/portlet/login/create_account_user_name.jspf 下,使用自定义 jsp 页面的钩子没有'无法工作,因为该文件是“jspf”而不是“jsp”,并且它不会显示在可用于挂钩更改的 jsp 文件的 Eclipse 窗口中

有人可以解决这种情况吗?

create_account_user_name.jspf 的内容:

<aui:input autoFocus="<%= windowState.equals(WindowState.MAXIMIZED) %>" model="<%= User.class %>" name="firstName" />

<aui:input model="<%= User.class %>" name="middleName" />

<aui:input model="<%= User.class %>" name="lastName">
    <c:if test="<%= PrefsPropsUtil.getBoolean(company.getCompanyId(), PropsKeys.USERS_LAST_NAME_REQUIRED, PropsValues.USERS_LAST_NAME_REQUIRED) %>">
        <aui:validator name="required" />
    </c:if>
</aui:input>

create_account.jsp 的内容:

<%@ include file="/html/portlet/login/init.jsp" %>

<%
String redirect = ParamUtil.getString(request, "redirect");

String openId = ParamUtil.getString(request, "openId");
boolean male = ParamUtil.getBoolean(request, "male", true);

Calendar birthdayCalendar = CalendarFactoryUtil.getCalendar();

birthdayCalendar.set(Calendar.MONTH, Calendar.JANUARY);
birthdayCalendar.set(Calendar.DATE, 1);
birthdayCalendar.set(Calendar.YEAR, 1970);
%>

<portlet:actionURL secure="<%= PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS || request.isSecure() %>" var="createAccountURL">
        <portlet:param name="struts_action" value="/login/create_account" />
</portlet:actionURL>

<aui:form action="<%= createAccountURL %>" method="post" name="fm">
        <aui:input name="saveLastPath" type="hidden" value="<%= false %>" />
        <aui:input name="<%= Constants.CMD %>" type="hidden" value="<%= Constants.ADD %>" />
        <aui:input name="redirect" type="hidden" value="<%= redirect %>" />
        <aui:input name="openId" type="hidden" value="<%= openId %>" />

        <liferay-ui:error exception="<%= AddressCityException.class %>" message="please-enter-a-valid-city" />
        <liferay-ui:error exception="<%= AddressStreetException.class %>" message="please-enter-a-valid-street" />
        <liferay-ui:error exception="<%= AddressZipException.class %>" message="please-enter-a-valid-postal-code" />
        <liferay-ui:error exception="<%= CaptchaMaxChallengesException.class %>" message="maximum-number-of-captcha-attempts-exceeded" />
        <liferay-ui:error exception="<%= CaptchaTextException.class %>" message="text-verification-failed" />
        <liferay-ui:error exception="<%= CompanyMaxUsersException.class %>" message="unable-to-create-user-account-because-the-maximum-number-of-users-has-been-reached" />
        <liferay-ui:error exception="<%= ContactFirstNameException.class %>" message="please-enter-a-valid-first-name" />
        <liferay-ui:error exception="<%= ContactFullNameException.class %>" message="please-enter-a-valid-first-middle-and-last-name" />
        <liferay-ui:error exception="<%= ContactLastNameException.class %>" message="please-enter-a-valid-last-name" />
        <liferay-ui:error exception="<%= DuplicateOpenIdException.class %>" message="a-user-with-that-open-id-already-exists" />
        <liferay-ui:error exception="<%= DuplicateUserEmailAddressException.class %>" message="the-email-address-you-requested-is-already-taken" />
        <liferay-ui:error exception="<%= DuplicateUserIdException.class %>" message="the-user-id-you-requested-is-already-taken" />
        <liferay-ui:error exception="<%= DuplicateUserScreenNameException.class %>" message="the-screen-name-you-requested-is-already-taken" />
        <liferay-ui:error exception="<%= EmailAddressException.class %>" message="please-enter-a-valid-email-address" />

        <liferay-ui:error exception="<%= GroupFriendlyURLException.class %>">

                <%
                GroupFriendlyURLException gfurle = (GroupFriendlyURLException)errorException;
                %>

                <c:if test="<%= gfurle.getType() == GroupFriendlyURLException.DUPLICATE %>">
                        <liferay-ui:message key="the-screen-name-you-requested-is-associated-with-an-existing-friendly-url" />
                </c:if>
        </liferay-ui:error>

        <liferay-ui:error exception="<%= NoSuchCountryException.class %>" message="please-select-a-country" />
        <liferay-ui:error exception="<%= NoSuchListTypeException.class %>" message="please-select-a-type" />
        <liferay-ui:error exception="<%= NoSuchRegionException.class %>" message="please-select-a-region" />
        <liferay-ui:error exception="<%= PhoneNumberException.class %>" message="please-enter-a-valid-phone-number" />
        <liferay-ui:error exception="<%= RequiredFieldException.class %>" message="please-fill-out-all-required-fields" />
        <liferay-ui:error exception="<%= ReservedUserEmailAddressException.class %>" message="the-email-address-you-requested-is-reserved" />
        <liferay-ui:error exception="<%= ReservedUserIdException.class %>" message="the-user-id-you-requested-is-reserved" />
        <liferay-ui:error exception="<%= ReservedUserScreenNameException.class %>" message="the-screen-name-you-requested-is-reserved" />
        <liferay-ui:error exception="<%= TermsOfUseException.class %>" message="you-must-agree-to-the-terms-of-use" />
        <liferay-ui:error exception="<%= UserEmailAddressException.class %>" message="please-enter-a-valid-email-address" />
        <liferay-ui:error exception="<%= UserIdException.class %>" message="please-enter-a-valid-user-id" />

        <liferay-ui:error exception="<%= UserPasswordException.class %>">

                <%
                UserPasswordException upe = (UserPasswordException)errorException;
                %>

                <c:if test="<%= upe.getType() == UserPasswordException.PASSWORD_CONTAINS_TRIVIAL_WORDS %>">
                        <liferay-ui:message key="that-password-uses-common-words-please-enter-in-a-password-that-is-harder-to-guess-i-e-contains-a-mix-of-numbers-and-letters" />
                </c:if>

                <c:if test="<%= upe.getType() == UserPasswordException.PASSWORD_INVALID %>">
                        <liferay-ui:message key="that-password-is-invalid-please-enter-in-a-different-password" />
                </c:if>

                <c:if test="<%= upe.getType() == UserPasswordException.PASSWORD_LENGTH %>">

                        <%
                        PasswordPolicy passwordPolicy = PasswordPolicyLocalServiceUtil.getDefaultPasswordPolicy(company.getCompanyId());
                        %>

                        <%= LanguageUtil.format(pageContext, "that-password-is-too-short-or-too-long-please-make-sure-your-password-is-between-x-and-512-characters", String.valueOf(passwordPolicy.getMinLength()), false) %>
                </c:if>

                <c:if test="<%= upe.getType() == UserPasswordException.PASSWORD_TOO_TRIVIAL %>">
                        <liferay-ui:message key="that-password-is-too-trivial" />
                </c:if>

                <c:if test="<%= upe.getType() == UserPasswordException.PASSWORDS_DO_NOT_MATCH %>">
                        <liferay-ui:message key="the-passwords-you-entered-do-not-match-each-other-please-re-enter-your-password" />
                </c:if>
        </liferay-ui:error>

        <liferay-ui:error exception="<%= UserScreenNameException.class %>" message="please-enter-a-valid-screen-name" />
        <liferay-ui:error exception="<%= WebsiteURLException.class %>" message="please-enter-a-valid-url" />

        <c:if test='<%= SessionMessages.contains(request, "openIdUserInformationMissing") %>'>
                <div class="alert alert-info">
                        <liferay-ui:message key="you-have-successfully-authenticated-please-provide-the-following-required-information-to-access-the-portal" />
                </div>
        </c:if>

        <aui:model-context model="<%= Contact.class %>" />

        <aui:fieldset column="<%= true %>">
                <aui:col >
                        <%@ include file="/html/portlet/login/create_account_user_name.jspf" %>

                        <c:if test="<%= !PrefsPropsUtil.getBoolean(company.getCompanyId(), PropsKeys.USERS_SCREEN_NAME_ALWAYS_AUTOGENERATE) %>">
                                <aui:input model="<%= User.class %>" name="screenName" />
                        </c:if>

                        <aui:input autoFocus="<%= true %>" model="<%= User.class %>" name="emailAddress">
                                <c:if test="<%= PrefsPropsUtil.getBoolean(company.getCompanyId(), PropsKeys.USERS_EMAIL_ADDRESS_REQUIRED) %>">
                                        <aui:validator name="required" />
                                </c:if>
                        </aui:input>
                </aui:col>

                <aui:col >
                        <c:if test="<%= PropsValues.LOGIN_CREATE_ACCOUNT_ALLOW_CUSTOM_PASSWORD %>">
                                <aui:input label="password" name="password1" size="30" type="password" value="" />

                                <aui:input label="enter-again" name="password2" size="30" type="password" value="">
                                        <aui:validator name="equalTo">
                                                '#<portlet:namespace />password1'
                                        </aui:validator>
                                </aui:input>
                        </c:if>

                        <c:choose>
                                <c:when test="<%= PrefsPropsUtil.getBoolean(company.getCompanyId(), PropsKeys.FIELD_ENABLE_COM_LIFERAY_PORTAL_MODEL_CONTACT_BIRTHDAY) %>">
                                        <aui:input name="birthday" value="<%= birthdayCalendar %>" />
                                </c:when>
                                <c:otherwise>
                                        <aui:input name="birthdayMonth" type="hidden" value="<%= Calendar.JANUARY %>" />
                                        <aui:input name="birthdayDay" type="hidden" value="1" />
                                        <aui:input name="birthdayYear" type="hidden" value="1970" />
                                </c:otherwise>
                        </c:choose>

                        <c:if test="<%= PrefsPropsUtil.getBoolean(company.getCompanyId(), PropsKeys.FIELD_ENABLE_COM_LIFERAY_PORTAL_MODEL_CONTACT_MALE) %>">
                                <aui:select label="gender" name="male">
                                        <aui:option label="male" value="1" />
                                        <aui:option label="female" selected="<%= !male %>" value="0" />
                                </aui:select>
                        </c:if>

                        <c:if test="<%= PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT %>">
                                <portlet:resourceURL var="captchaURL">
                                        <portlet:param name="struts_action" value="/login/captcha" />
                                </portlet:resourceURL>

                                <liferay-ui:captcha url="<%= captchaURL %>" />
                        </c:if>
                </aui:col>
        </aui:fieldset>

        <aui:button-row>
                <aui:button type="submit" />
        </aui:button-row>
</aui:form>

<liferay-util:include page="/html/portlet/login/navigation.jsp" />

【问题讨论】:

按原样将 jsp 文件添加到您的钩子中,其中包括您的 jspf 是的,我这样做了,我包含了 jsp 文件 /html/portlet/login/create_account.jsp 但该 jsp 文件没有代表“中间名”字段的 aui 标记。该标签在jspf文件中而不是jsp文件中 重命名 jspf 文件并在 create_account.jsp 中引用该新名称 这个技巧也很管用 Pankaj Kathiriya 谢谢 【参考方案1】:

只需将原始文件 /html/portlet/login/create_account_user_name.jspf 复制到您的 docroot/custom_jsp/html/portlet/login/ 目录中。不管 eclipse 向你展示了什么,你钩子的任何 jsp/jspf 都将替换原来的 liferay 文件。

我成功编辑了文件 /html/portlet/users_admin/user/details_user_name.jspf(我禁用了来自 ldap 的用户的字段修改)。

请注意,在 liferay-hook.xml 中,您可以为 jsp 覆盖设置的唯一指令是

<custom-jsp-dir>/custom_jsp</custom-jsp-dir>

你不需要指定所有的jsp文件,只需要你的根目录

【讨论】:

以上是关于使用 jspf 钩子自定义 LifeRay portlet的主要内容,如果未能解决你的问题,请参考以下文章

Liferay 7 - 博客聚合器的自定义样式

使用 liferay 和 icefaces 共享 Portlet 数据

如何使用freemarker删除liferay DXP自定义主题中的cookie?

在 Liferay 7 中创建自定义标签库

Liferay 7.3.5GA6 自定义索引搜索和ddmFieldArray

无法在 Liferay 7.2 中呈现自定义表单字段