Issue
In Liferay hook development, one common question is:
How to import/use/access custom java classes in a JSP hook?
The question can be described as:
1. Create a hook project (for example: sample-hook)
2. Create a custom class (for example: com.kzhang.CustomJavaClass) in hook project.
3. Override a Liferay portlet jsp (for example, html/portlet/blogs/view.jsp) and try to use the CustomJavaClass in the jsp. Liferay will throw "class CustomJavaClass can't be resolved to a type" exception or ClassNotFound exception.
Cause
The jsps in hook plugin is deployed to ROOT servlet context. for example, the html/portlet/blogs/view.jsp in hook plugin will be copied to ${container.deploy.folder}/ROOT/html/portlet/blogs/view.jsp, and the origional view.jsp will be renamed as view.portal.jsp.
The custom classes are still remains in the hook servlet context. for example, the com.kzhang.CustomJavaClass in hook plugin will be still in ${container.deploy.folder}/sample-hook/WEB-INF/classes/com/kzhang/CustomJavaClass.class.
In html/portlet/blogs/view.jsp it can not find CustomJavaClass because each servlet context can only see it's own classes and those of the parent classloaders. In our case, the html/portlet/blogs/view.jsp is in ROOT servlet context which can not see the CustomJavaClass in sample-hook servlet context
Solution
There are some discussions in Liferay forum:
http://www.liferay.com/community/forums/-/message_boards/message/7454307
http://www.liferay.com/community/forums/-/message_boards/message/13467179
http://www.liferay.com/community/forums/-/message_boards/message/21397246
http://www.liferay.com/community/forums/-/message_boards/message/11508053
The most common solution is to use the ext plugin instead of hook plugin.
Solution 1: Use Spring + PortletBeanLocatorUtil + JAVA reflection
Step 1. Create the custom-spring.xml in /sample-hook/docroot/WEB-INF/src/context/custom-spring.xml folder.
Step 2. In custom-spring.xml, define the bean for your custom java class:
Step 3. In web.xml, add the context-param to load the custom-spring.xml:
Step 4. In custom_jsps/html/portlet/blogs/view.jsp, use PortletBeanLocatorUtil to load the custom java class:
Example here.
Solution 2: Override Struts Action and call the custom classes in Struts Action.
Setp 1. In liferay-hook.xml, override the view action of blogs portlet:
Step 2. Create the CustomStrutsAction, invoke the custom class in CustomStrutsAction and set the result in to renderRequest attribute:
Step 3. In custom_jsps/html/portlet/blogs/view.jsp, get the result from the renderRequest attribute:

