[转]在Liferay开发中如何避免使用EXT插件

 

尽量使用hook代替ext

假如你须要覆盖诸如 *ServiceImp l类, 好比 UserLocalServiceImpl类,能够扩展UserLocalServiceWrapper ,重写你想要的方法, 而后在 liferay-hook.xml中进行以下的声明: javascript

<hook>
    <service>
        <service-type>com.liferay.portal.service.UserLocalService</service-type>
        <service-impl>com.liferay.testhook.hook.service.impl.TestUserLocalServiceImpl</service-impl>
    </service>
</hook>

其中

public class TestUserLocalServiceImpl extends UserLocalServiceWrapper {
   // override what you want ...
}

若是你须要覆盖在liferay-hook.dtd文件中声明的类或添加event action, 好比你但愿覆盖ScreennameValidator, ScreennameGenerator, 或者添加postLoginActions, servicePreActions等, 能够在portal-ext.properties覆盖所需的hook. 这样你不须要重启应用服务器,就能够当即看到修改后的效果.html


尽可能使用自定义属性

若是你须要自定义属性,那么尽可能不要使用service.xml往数据库添加新的表字段并从新生成全部的service, 你可使用自定义的属性。二者不存在性能差异。字符串索引(只有字符串)会被索引,你能够经过liferay的管理控制台界面进行设置。你须要注意的是恰当添加view和update权限。java

以create account form为例,你不须要修改任何java类给注册过程添加新的属性,只需建立一个 hook 插件并在create-account.jsp中添加以下代码数据库

<liferay-ui:custom-attribute
       className="com.liferay.portal.model.User"
       classPK="<%= 0 %>"
       editable="<%= true %>"
       label="<%= true %>"
       name="favoriteColor"
/>

那么action类会自动得到你新加的属性并更新他们。 其余大部分的model类也如此,好比Documents, Images, Web content)服务器


尽可能使用扩展而不要覆盖

假如你须要覆盖诸如 *Action 类, 那么你应该继承来扩展它,而不是在ext中直接修改他,而且在struts-config.xml或liferay-portlet.xml 将相应的action类做替换。好比你须要添加一个叫“Favorite Color”的必填属性, 你能够这样作: app

<struts-config>
    <action-mappings>
        <action path="/login/create_account" type="com.liferay.test.ext.portlet.login.action.TestCreateAccountAction">
            <forward name="portlet.login.create_account" path="portlet.login.create_account" />
        </action>
</struts-config>

public class TestCreateAccountAction extends CreateAccountAction {
    protected void addUser(ActionRequest actionRequest, ActionResponse actionResponse)
        throws Exception {

        Map<String, Serializable> expandoBridgeAttributes =
            PortalUtil.getExpandoBridgeAttributes(
                ExpandoBridgeFactoryUtil.getExpandoBridge(User.class.getName()), actionRequest);

         String favoriteColor = (String)expandoBridgeAttributes.get("favoriteColor");

         if (Validator.isNull(favoriteColor)) {
                throw new RequiredFieldException("favoriteColor", "favoriteColorLabel"); // v6.0/EE specific code           
         }

        super.addUser(actionRequest, actionResponse);  // Don't touch current code to make upgrades painless =)
    }
}


尽可能使用hook插件修改JSP页面,而不要使用ext插件。关于hook插件的使用,请参阅http://www.liferay.com/community/wiki/-/wiki/Main/Portal+Hook+Pluginsless

好比你可使用buffer的taglib标签为建立帐户的表单作一些修改. 这里假设咱们须要将移除底部的javascript代码,将按钮的文字从"save"改为"create", 而且添加一个叫"favorite color"的自定义属性. 咱们能够在ROOT.war/html/portlet/login/create_account.jsp中添加以下的代码: jsp

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

<liferay-util:buffer var="html">
    <liferay-util:include page="/html/portlet/login/create_account.portal.jsp" />
</liferay-util:buffer>

<liferay-util:buffer var="customHtml">
   <liferay-ui:custom-attribute
        className="com.liferay.portal.model.User"
        classPK="<%= 0 %>"
        editable="<%= true %>"
        label="<%= true %>"
        name="favoriteColor"
    />
</liferay-util:buffer>

<%
int x = html.lastIndexOf("<script type=\"text/javascript\"");

if (x != -1) {
    y = html.indexOf("</script>", x);

    html = html.substring(0, x) + html.substring(y + 9);
}

html = html.replace(LanguageUtil.get(pageContext, "save"), LanguageUtil.get(pageContext, "create"));

x = html.indexOf(LanguageUtil.get(pageContext, "last-name"));

if (x != -1) {
    y = html.indexOf("</div>", x);

    html = html.substring(0, y) + customHtml + html.substring(y);
}
%>

<%= html %>
相关文章
相关标签/搜索