尽可能使用hook代替ext
假如你需要覆盖诸如 *ServiceImp l类, 比如 UserLocalServiceImpl类,可以扩展UserLocalServiceWrapper ,重写你想要的方法, 然后在 liferay-hook.xml中进行如下的声明:
<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. 这样你不需要重启应用服务器,就可以立即看到修改后的效果.
尽量使用自定义属性
如果你需要自定义属性,那么尽量不要使用service.xml往数据库添加新的表字段并重新生成所有的service, 你可以使用自定义的属性。两者不存在性能差别。字符串索引(只有字符串)会被索引,你可以通过liferay的管理控制台界面进行设置。你需要注意的是恰当添加view和update权限。
以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”的必填属性, 你可以这样做:
<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+Plugins
比如你可以使用buffer的taglib标签为创建账户的表单做一些修改. 这里假设我们需要将移除底部的javascript代码,将按钮的文字从"save"改成"create", 并且添加一个叫"favorite color"的自定义属性. 我们可以在ROOT.war/html/portlet/login/create_account.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 %>
来源:oschina
链接:https://my.oschina.net/u/103999/blog/64918