Liferay Struts2 完整实例 修正.docx
《Liferay Struts2 完整实例 修正.docx》由会员分享,可在线阅读,更多相关《Liferay Struts2 完整实例 修正.docx(17页珍藏版)》请在冰点文库上搜索。
LiferayStruts2完整实例修正
目录
一.Liferay,ext开发环境的搭建1
二.集成struts23
1.依赖包:
3
2.使用Struts23
三.使用Hibernate.Spring4
1.建立bean4
2.建立事务层:
5
3.建立持久层7
4.Action中调用事务层9
三.将portlet注册到portal10
四.加入国际化12
五.加入处理页面12
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-->showview-->ant),在ant视图中,点击添加,
选择:
\portalspace\portal\目录下build.xml
顺序执行clean,start,build-ext
4.将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/ext
app.server.type=tomcat
app.server.tomcat.dir=D:
/myspace/servers/tomcat-6.0.18
5.将myspace/ext工程导入到eclipse中。
6.打开window-->preference-->myeclipse-->applicationservers将其他应用服务器disabble掉,
找到tomcat6,将tomcathomediretory指向D:
\myspace\servers\tomcat-6.0.18,
展开tomcat6,JDK选择我们配置好的JDK1.5+,
JDK选项下面optionaljavavmargument输入如下内容:
-Xms128m
-Xmx256m
-XX:
MaxPermSize=128m
7.建立数据库lportal。
在D:
\myspace\ext\ext-impl\src\portal-ext.properties文件中添加数据库连接信息
#MySQL
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:
mysql:
//localhost:
3306/lportal
jdbc.default.username=root
jdbc.default.password=root
hibernate.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登录
二.集成struts2
1.依赖包:
除以上包以外还需要加入
struts2-portlet-plugin-2.1.8.1.jar
准备好以上依赖包之后,将它们放到ext/ext-lib/portal/下。
然后选择projectpropertiesjavabuildpathLibrariesaddjar选中刚才加入的包确认。
2.使用Struts2
在ext-impl/src目录下新建struts.xml,事例如下:
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http:
//struts.apache.org/dtds/struts-2.0.dtd">
namespace="/edit">
/html/portlet/test/view.jsp
除了extends=”struts-portlet-default”不同之外,其它配置与平常配置一样,编写好自己的action类即完成struts2的集成。
不需要在web.xml中加入监听
三.使用Hibernate.Spring
1.建立bean
找到ext/ext-service/src新建包:
com.softcreate.portlet.car.model
新建类Car.java内容如下:
publicclassCar{
privateStringid;
privateStringname;
privateStringnumber;
privateStringdate;
privateStringuserId;
privateStringaddtext;
privateIntegerisAble;
privateIntegerisUsed;
privateStringdept;
setter(),getter()…
}
找到ext/ext-impl/src/META-INF新建car-hbm.xml内容如下:
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN""
table="oa_t_car_detail">
2.建立事务层:
找到ext/ext-service/src新建包:
com.softcreate.portlet.car.service
在包下新建接口:
CarService.java,内容如下:
packagecom.softcreate.portlet.car.service;
importjava.util.List;
importcom.liferay.portal.PortalException;
importcom.liferay.portal.SystemException;
importcom.liferay.portal.kernel.annotation.Isolation;
importcom.liferay.portal.kernel.annotation.Transactional;
@Transactional(isolation=Isolation.PORTAL,rollbackFor={
PortalException.class,SystemException.class})
publicinterfaceCarEntryService{
publicListfindAll()throwsSystemException;
}
在此包下新建工具类:
CarServiceUtil,内容如下:
packagecom.softcreate.portlet.car.service;
publicclassCarServiceUtil{
privatestaticCarService_service;
publicstaticCarServicegetService(){
if(_service==null){
thrownewRuntimeException("CarServiceisnotset");
}
return_service;
}
publicvoidsetService(CarServiceservice){
_service=service;
}
}
找到ext/ext-impl/src,新建包com.softcreate.portlet.car.service.base,内容如下,新建CarServiceBaseImpl:
packagecom.softcreate.portlet.car.service.base;
importjava.util.List;
importcom.liferay.portal.SystemException;
importcom.liferay.portal.kernel.annotation.BeanReference;
importcom.liferay.portal.service.base.PrincipalBean;
importcom.liferay.portal.util.PortalUtil;
importcom.softcreate.portlet.car.service.CarService;
importcom.softcreate.portlet.car.service.persistence.CarEntryPersistence;
publicabstractclassCarServiceBaseImplextendsPrincipalBeanimplements
CarService{
//name中的内容为spring中配置的beanid
/*
以下为ext-spring.xml加入的信息。
主要做的操作是注入service,persistence.
如果beanid自定义取的名字如何取到实例,或者取不到数据库的数据,请务必改为*Service.impl形式。
可参考我的命名。
class="com.softcreate.portlet.oa.system.car.service.impl.CarServiceImpl"/>
id="com.softcreate.portlet.oa.system.car.service.impl.CarService.velocity"
class="org.springframework.aop.framework.ProxyFactoryBean"parent="baseVelocityService">
ref="com.softcreate.portlet.oa.system.car.service.impl.CarService.impl"/>
class="com.softcreate.portlet.oa.system.car.service.CarServiceUtil">
ref="com.softcreate.portlet.oa.system.car.service.impl.CarService.impl"/>
id="com.softcreate.portlet.oa.system.car.service.persistence.CarPersistence.impl"
class="com.softcreate.portlet.oa.system.car.service.persistence.CarPersistenceImpl"
parent="basePersistence"/>
class="com.softcreate.portlet.oa.system.car.service.persistence.CarUtil">
ref="com.softcreate.portlet.oa.system.car.service.persistence.CarPersistence.impl"/>
*/
@BeanReference(name="com.softcreate.portlet.car.service.impl.CarService.impl")
protectedCarServicecarService;
@BeanReference(name="com.softcreate.portlet.car.service.persistence.CarPersistence.impl")
protectedCarPersistencecarPersistence;
publicCarServicegetCarService(){
returncarService;
}
publicvoidsetCarService(CarServicecarService){
this.carService=carService;
}
publicCarPersistencegetCarPersistence(){
returncarPersistence;
}
publicvoidsetCarPersistence(CarPersistencecarPersistence){
this.carPersistence=carPersistence;
}
protectedvoidrunSQL(Stringsql)throwsSystemException{
try{
PortalUtil.runSQL(sql);
}catch(Exceptione){
thrownewSystemException(e);
}
}
}
找到ext/ext-impl/src,新建包com.softcreate.portlet.car.service.impl,新建类CarServiceImpl.java内容如下:
packagecom.softcreate.portlet.car.service.impl;
importjava.util.List;
importcom.liferay.portal.SystemException;
importcom.softcreate.portlet.car.service.base.CarServiceBaseImpl;
publicclassCarServiceImplextendsCarServiceBaseImpl{
publicListfindAll()throwsSystemException{
returncarPersistence.findAll();
}
}
3.建立持久层
找到ext/ext-service/src新建包:
com.softcreate.portlet.car.service.persistence
在包下新建接口:
CarPersistence.java,内容如下:
packagecom.softcreate.portlet.car.service.persistence;
importcom.liferay.portal.service.persistence.BasePersistence;
publicinterfaceCarEntryPersistenceextendsBasePersistence{
publicjava.util.ListfindAll()throwscom.liferay.portal.SystemException;
}
在此包下新建工具类:
CarPersistenceUtil,内容如下:
packagecom.softcreate.portlet.car.service.persistence;
publicclassCarPersistenceUtil{
privatestaticCarPersistence_persistence;
publicstaticjava.util.ListfindAll()
throwscom.liferay.portal.SystemException{
returngetPersistence().findAll();
}
publicstaticCarPersistencegetPersistence(){
return_persistence;
}
publicvoidsetPersistence(CarPersistencepersistence){
_persistence=persistence;
}
}
找到ext/ext-impl/src,新建包com.softcreate.portlet.car.service.persistence,新建类CarPersistenceImpl.java内容如下:
packagecom.softcreate.portlet.car.service.persistence;
importjava.util.List;
importcom.liferay.portal.SystemException;
importcom.liferay.portal.kernel.dao.orm.Session;
importcom.liferay.portal.service.persistence.impl.BasePersistenceImpl;
importcom.softcreate.portlet.car.model.impl.Car;
publicclassCarPersistenceImplextendsBasePersistenceImplimplements
CarPersistence{
publicListfindAll()throwsSystemException{
Sessionsession=null;
Listlist=null;
try{
session=openSession()