简单的说,完全不知道Liferay下应该如何做开发。所以完全凭借SSH的背景知识,自己写了个独立的山寨版web应用弄进去,好在是跑起来了。如果哪位达人对Liferay下的开发比较熟悉,还望指点一下。
插件开发准备步骤,见以前的文章,现在直接上代码。
一个简单的Portlet:
package com.hrms.recruiting.portlet; import java.io.IOException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.GenericPortlet; import javax.portlet.PortletException; import javax.portlet.PortletRequestDispatcher; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.hrms.recruiting.common.BeanManager; import com.hrms.recruiting.entity.Candidate; /** * @author Clay Zhong, [email protected] * * @date May 24, 2009 */ public class BasePortlet extends GenericPortlet { private static final Log log = LogFactory.getLog(BasePortlet.class); protected String editJSP; protected String helpJSP; protected String viewJSP; public void init() throws PortletException { editJSP = getInitParameter("edit-jsp"); helpJSP = getInitParameter("help-jsp"); viewJSP = getInitParameter("view-jsp"); } public void doDispatch(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { Candidate candidate = new Candidate(); candidate.setName("clay"); candidate.setEmail("[email protected]"); BeanManager.getServiceManager().getCandidateService().create(candidate); String jspPage = renderRequest.getParameter("jspPage"); if (jspPage != null) { include(jspPage, renderRequest, renderResponse); } else { super.doDispatch(renderRequest, renderResponse); } } public void doEdit(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { System.out.println("into do edit"); if (renderRequest.getPreferences() == null) { super.doEdit(renderRequest, renderResponse); } else { include(editJSP, renderRequest, renderResponse); } } public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { System.out.println("into do view"); include(viewJSP, renderRequest, renderResponse); } public void doHelp(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { System.out.println("into do help"); include(helpJSP, renderRequest, renderResponse); } public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { System.out.println("into process action"); } protected void include(String path, RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { PortletRequestDispatcher portletRequestDispatcher = getPortletContext().getRequestDispatcher(path); if (portletRequestDispatcher == null) { log.error(path + " is not a valid include"); } else { portletRequestDispatcher.include(renderRequest, renderResponse); } } }
Spring用了一个工厂类,这个做法很ugly,因为现在还不知道把portlet交给spring托管然后注入service会有什么后果,暂且如此吧
/** * @author Clay Zhong, [email protected] * * @date May 24, 2009 */ public class ServiceManager { private CandidateService candidateService; public CandidateService getCandidateService() { return candidateService; } public void setCandidateService(CandidateService candidateService) { this.candidateService = candidateService; } }
package com.hrms.recruiting.service.impl; import java.util.List; import com.hrms.recruiting.dao.CandidateDao; import com.hrms.recruiting.entity.Candidate; import com.hrms.recruiting.service.CandidateService; /** * @author Clay Zhong, [email protected] * * @date May 24, 2009 */ public class CandidateServiceImpl implements CandidateService { private CandidateDao candidateDao; public void create(Candidate candidate) { candidateDao.save(candidate); } public List<Candidate> list() { return candidateDao.list(); } public CandidateDao getCandidateDao() { return candidateDao; } public void setCandidateDao(CandidateDao candidateDao) { this.candidateDao = candidateDao; } }
通过读取context获取工厂类实例:
package com.hrms.recruiting.common; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hrms.recruiting.service.ServiceManager; /** * @author Clay Zhong, [email protected] * * @date May 24, 2009 */ public class BeanManager { static String configFile = "../context/*.xml"; private static BeanFactory beanFactory = new ClassPathXmlApplicationContext(configFile); public static Object getBean(String tag) { return beanFactory.getBean(tag); } public static ServiceManager getServiceManager() { return (ServiceManager) beanFactory.getBean("serviceManager"); } }
紧接着当然是DAO和POJO:
package com.hrms.recruiting.dao; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.hrms.recruiting.entity.Candidate; /** * @author Clay Zhong, [email protected] * * @date May 24, 2009 */ public class CandidateDao extends HibernateDaoSupport { public void save(Candidate candidate) { getHibernateTemplate().save(candidate); } @SuppressWarnings("unchecked") public List<Candidate> list() { return getHibernateTemplate().loadAll(Candidate.class); } }
package com.hrms.recruiting.entity; /** * @author Clay Zhong, [email protected] * * @date May 24, 2009 */ public class Candidate { private Integer id; private String name; private String email; private String resume; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getResume() { return resume; } public void setResume(String resume) { this.resume = resume; } }
然后当然是配置Spring和Hibernate,这个不用多说了
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> WEB-INF/context/applicationContext.xml, WEB-INF/context/applicationContext-dao.xml, WEB-INF/context/applicationContext-service.xml </param-value> </context-param> </web-app>
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/hrms?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>com/hrms/recruiting/entity/Candidate.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.jdbc.batch_size">20</prop> </props> </property> </bean> </beans>
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="candidateService" class="com.hrms.recruiting.service.impl.CandidateServiceImpl"> <property name="candidateDao" ref="candidateDao" /> </bean> <bean id="serviceManager" class="com.hrms.recruiting.service.ServiceManager"> <property name="candidateService" ref="candidateService" /> </bean> </beans>
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="daoTemplate" abstract="true"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="candidateDao" class="com.hrms.recruiting.dao.CandidateDao" parent="daoTemplate" /> </beans>
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.hrms.recruiting.entity.Candidate" table="candidate" catalog="hrms"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="45" not-null="true" /> </property> <property name="email" type="java.lang.String"> <column name="EMAIL" length="256" not-null="true" /> </property> </class> </hibernate-mapping>
定义一个默认的显示页面:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <portlet:defineObjects /> This is the <b>Sample JSP Portlet</b>. Use this as a quick way to include JSPs.
最后就是配置Portlet了
<?xml version="1.0"?> <!DOCTYPE display PUBLIC "-//Liferay//DTD Display 5.2.0//EN" "http://www.liferay.com/dtd/liferay-display_5_2_0.dtd"> <display> <category name="HRMS"> <portlet id="recruiting-portlet" /> </category> </display>
<?xml version="1.0"?> <!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 5.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_5_2_0.dtd"> <liferay-portlet-app> <portlet> <portlet-name>recruiting-portlet</portlet-name> <icon>/icon.png</icon> <instanceable>true</instanceable> <header-portlet-css>/css/test.css</header-portlet-css> <footer-portlet-javascript>/js/test.js</footer-portlet-javascript> </portlet> <role-mapper> <role-name>administrator</role-name> <role-link>Administrator</role-link> </role-mapper> <role-mapper> <role-name>guest</role-name> <role-link>Guest</role-link> </role-mapper> <role-mapper> <role-name>power-user</role-name> <role-link>Power User</role-link> </role-mapper> <role-mapper> <role-name>user</role-name> <role-link>User</role-link> </role-mapper> </liferay-portlet-app>
<?xml version="1.0"?> <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"> <portlet> <portlet-name>recruiting-portlet</portlet-name> <display-name>Human Resource Recruiting</display-name> <portlet-class>com.hrms.recruiting.portlet.BasePortlet</portlet-class> <init-param> <name>view-jsp</name> <value>/view.jsp</value> </init-param> <expiration-cache>0</expiration-cache> <supports> <mime-type>text/html</mime-type> </supports> <portlet-info> <title>Human Resource Recruiting</title> <short-title>Recruiting</short-title> <keywords>Human Recruiting</keywords> </portlet-info> <security-role-ref> <role-name>administrator</role-name> </security-role-ref> <security-role-ref> <role-name>guest</role-name> </security-role-ref> <security-role-ref> <role-name>power-user</role-name> </security-role-ref> <security-role-ref> <role-name>user</role-name> </security-role-ref> </portlet> </portlet-app>
差不多就是这样了,运行Plugin项目下的build,就可以将此发布到Liferay下。因为直接用的Liferay下的Spring和Hibernate包,所以备注一下其他需要引用的jar:
commons-collections.jar
commons-logging.jar
dom4j.jar
javassist.jar
jta.jar
log4j.jar
mysql-connector-java-5.1.7-bin.jar
slf4j-api.jar
slf4j-log4j12.jar
util-bridges.jar
util-java.jar
util-taglib.jar