目录一.Liferay,ext开发环境的搭建 (1)二.集成struts2 (3)1.依赖包: (3)2.使用Struts2 (3)三.使用Hibernate. Spring (4)1.建立bean (4)2.建立事务层: (5)3 .建立持久层 (8)4. Action 中调用事务层 (10)三.将portlet注册到portal (11)四.加入国际化 (13)五.加入处理页面 (13)2010年12月20日,修改在页面上的标签找不到国际化信息。
一.Liferay,ext开发环境的搭建1.进入myeclipse,修改liferay-portal-src-5.2.3文件名为portal。
导入portal到eclipse.2.展开portal工程目录,我们可看到一个release.properties文件,新建一个它的扩展配置文件,文件名为release.${username}.properties。
${username}是计算机当前用户名我文件名是release.Administrator.properties,文件内容只一行:lp.ext.dir=D:\myspace\ext 即扩展工程放置路径。
3.打开myeclipse里面ant视图(window--> show view --> ant) , 在ant视图中,点击添加,选择:\portalspace\portal\目录下build.xml顺序执行clean ,start ,build-ext4.将liferay-portal-tomcat-6.0-5.2.3.zip解压到D:\portalspace\目录下,并更名为servers。
在D:\portalspace\ext目录下我们可以看到app.server.properties文件,同样我们也要建立一个扩展配置文件app.server.${username}.properties,文件内容两行:lp.ext.dir=D:/myspace/extapp.server.type=tomcatapp.server.tomcat.dir=D:/ myspace /servers/tomcat-6.0.185.将myspace/ext 工程导入到eclipse中。
6.打开window --> preference --> myeclipse --> application servers 将其他应用服务器disabble掉,找到tomcat6,将tomcat home diretory 指向D:\myspace\servers\tomcat-6.0.18,展开tomcat6,JDK选择我们配置好的JDK1.5+,JDK选项下面optional java vm argument输入如下内容:-Xms128m-Xmx256m-XX:MaxPermSize=128m7.建立数据库lportal 。
在D:\myspace\ext\ext-impl\src\portal-ext.properties文件中添加数据库连接信息# MySQLjdbc.default.driverClassName=com.mysql.jdbc.Driverjdbc.default.url=jdbc:mysql://localhost:3306/lportalername=rootjdbc.default.password=roothibernate.dialect=org.hibernate.dialect.MySQLDialect若不添加以上内容liferay就会使用自带默认数据库.8.选择 :\myspace\ext\目录下build.xml 顺序执行clean,deploy 将ext项目部署到tomcat-6.0.18. 然后选择eclipse中的tomcat6.0启动即可9.在浏览器里输入http://localhost:8080/可以看到portal系统了。
输入emal:test@ password: test登录二.集成struts21.依赖包:除以上包以外还需要加入struts2-portlet-plugin-2.1.8.1.jar准备好以上依赖包之后,将它们放到ext/ext-lib/portal/下。
然后选择project→properties→ java build path→ Libraries→add jar→选中刚才加入的包确认。
2.使用Struts2在ext-impl/src 目录下新建struts.xml,事例如下:<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""/dtds/struts-2.0.dtd"><struts><package name="edit"extends="struts-portlet-default"namespace="/edit"><action name="submit"class="com.softcreate.portlet.car.action.SubmitCarAction"method="getParam"><result name="success">/html/portlet/test/view.jsp</result> </action></package></struts>除了extends=”struts-portlet-default”不同之外,其它配置与平常配置一样,编写好自己的action类即完成struts2的集成。
不需要在web.xml中加入监听三.使用Hibernate. Spring1.建立bean找到ext/ext-service/src新建包:com.softcreate.portlet.car.model新建类Car.java 内容如下:public class Car {private String id;private String name;private String number;private String date;private String userId;private String addtext;private Integer isAble;private Integer isUsed;private String dept;setter() , getter() …}找到 ext/ext-impl/src/META-INF新建car-hbm.xml 内容如下:<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""/hibernate-mapping-3.0.dtd"> <hibernate-mapping><class name="com.softcreate.portlet.car.model.Car"table="oa_t_car_detail"><id name="id"type="ng.String"><column name="id"length="100"/><generator class="assigned"/></id><property name="name"type="ng.String"><column name="name_"length="100"/></property><property name="number"type="ng.String"><column name="number_"length="100"/></property><property name="date"type="ng.String"><column name="date_"length="100"/></property><property name="userId"type="ng.String"> <column name="user_id"length="100"/></property><property name="addtext"type="ng.String"> <column name="addtext"length="500"/></property><property name="isAble"type="ng.Integer"> <column name="is_able"/></property><property name="isUsed"type="ng.Integer"> <column name="is_used"/></property><property name="dept"type="ng.String"><column name="dept"length="100"/></property></class></hibernate-mapping>2.建立事务层:找到ext/ext-service/src新建包:com.softcreate.portlet.car.service 在包下新建接口:CarService.java,内容如下:package com.softcreate.portlet.car.service;import java.util.List;import com.liferay.portal.PortalException;import com.liferay.portal.SystemException;import com.liferay.portal.kernel.annotation.Isolation;import com.liferay.portal.kernel.annotation.Transactional;@Transactional(isolation = Isolation.PORTAL, rollbackFor = {PortalException.class, SystemException.class })public interface CarEntryService {public List findAll() throws SystemException;}在此包下新建工具类:CarServiceUtil, 内容如下:package com.softcreate.portlet.car.service;public class CarServiceUtil {private static CarService _service;public static CarService getService() {if (_service == null) {throw new RuntimeException("CarService is not set");}return_service;}public void setService(CarService service) {_service = service;}}找到ext/ext-impl/src,新建包com.softcreate.portlet.car.service.base,内容如下,新建CarServiceBaseImpl:package com.softcreate.portlet.car.service.base;import java.util.List;import com.liferay.portal.SystemException;import com.liferay.portal.kernel.annotation.BeanReference;import com.liferay.portal.service.base.PrincipalBean;import com.liferay.portal.util.PortalUtil;import com.softcreate.portlet.car.service.CarService;import com.softcreate.portlet.car.service.persistence.CarEntryPersistence;public abstract class CarServiceBaseImpl extends PrincipalBean implementsCarService {// name中的内容为spring中配置的bean id/*以下为ext-spring.xml 加入的信息。