Note: 1) 一个Hook工程可以重写多个功能,包括同时重写原有的函数和JSP页面等。
2)使用向导
1、重写原有的功能
这里建议用LiferyIDE 的向导,可以省去很多配置。
例如创建用户功能:
1) 编辑:liferay-hook.xml
<hook> <service> <service-type>com.liferay.portal.service.UserLocalService</service-type> <service-impl>com.hook.UserServiceImpl</service-impl> </service> </hook
com.hook.UserServiceImpl
public class UserServiceImpl extends UserLocalServiceWrapper{ public UserServiceImpl(UserLocalService userLocalService) { super(userLocalService); // TODO Auto-generated constructor stub } public User addUserWithWorkflow(long creatorUserId, long companyId, boolean autoPassword, String password1, String password2, boolean autoScreenName, String screenName, String emailAddress, long facebookId, String openId, Locale locale, String firstName, String middleName, String lastName, int prefixId, int suffixId, boolean male, int birthdayMonth, int birthdayDay, int birthdayYear, String jobTitle, long[] groupIds, long[] organizationIds, long[] roleIds, long[] userGroupIds, boolean sendEmail, ServiceContext serviceContext) throws PortalException, SystemException { user = super.addUserWithWorkflow(creatorUserId, companyId, autoPassword, password1, password2, autoScreenName, screenName, emailAddress, facebookId, openId, locale, firstName, middleName, lastName, prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds, roleIds, userGroupIds, sendEmail, serviceContext); user.setExpandoBridgeAttributes(serviceContext); //额外的字段 // 自己的代码 return user; } }
继续使用向导,添加要重写的JSP页面.
Note: 1) Hook 怎么转到新添加的Jsp? 例如:新添加JSP页面:addStadium.jsp (/docroot/custom_jsps/html/portlet/users_admin/addStadium.jsp)
这里要用struts_action.
首先在jsp页面中添加链接。
<!-- 添加大厅 --> <portlet:renderURL var="addStadiumURL"> <portlet:param name="redirect" value="<%=redirect%>" /> <portlet:param name="organizationId" value="<%=String.valueOf(organizationId)%>" /> <portlet:param name="struts_action" value="/users_admin/addStadium" /> </portlet:renderURL> <liferay-ui:icon image="add_location" message="addStadium" url="<%=addStadiumURL%>" />
配置:liferay-hook.xml
<hook> <language-properties> content/Language.properties </language-properties> <custom-jsp-dir>/custom_jsps</custom-jsp-dir> <service> <service-type>com.liferay.portal.service.UserLocalService</service-type> <service-impl>com.hook.UserServiceImpl</service-impl> </service> <struts-action> <struts-action-path>/users_admin/addStadium</struts-action-path> <struts-action-impl> com.action.AddStadium </struts-action-impl> </struts-action> </hook>
新建类:
com.action.AddStadium
package com.action; import javax.portlet.PortletConfig; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import com.liferay.portal.kernel.struts.BaseStrutsPortletAction; import com.liferay.portal.kernel.struts.StrutsPortletAction; public class AddStadium extends BaseStrutsPortletAction { @Override public String render( StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception { return "/portlet/users_admin/addStadium.jsp"; } }