liferay开发(二)开发详细举例--例二
来源:互联网 发布:直播截屏软件 编辑:程序博客网 时间:2024/06/05 06:37
例二
有关数据库相连的例子。(例一中的部分不动)
1. 在ext-impl/src中新建3个包,分别为com.ext.portlet.hello.beans,com.ext.portlet.hello.manager,com.ext.portlet.hello.persistence,其中,在beans中加入类HelloBeans,在manager中加入类HelloUtil,在persistence中加入类HelloPersistence,HelloPersistenceImpl。
2. 在HelloBeans中写入
package com.ext.portlet.hello.beans;
public class HelloBeans {
private int id=0;
private String name="";
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在HelloUtil中写入:
package com.ext.portlet.hello.manager;
import java.util.List;
import com.ext.portlet.hello.persistence.HelloPersistence;
import com.liferay.portal.SystemException;
public class HelloUtil {
private static HelloUtil _util;
private HelloPersistence service=null;
private static final String _UTIL = HelloUtil.class.getName();
private static HelloUtil _getUtil() {
if (_util == null) {
_util = (HelloUtil)com.liferay.portal.kernel.bean.BeanLocatorUtil.locate(_UTIL);
}
return _util;
}
public static HelloPersistence getService() {
return _getUtil().service;
}
public void setService(HelloPersistence service) {
this.service = service;
}
public static List getAll()throws SystemException{
return getService().getAll();
}
}
HelloPersistence中写入
package com.ext.portlet.hello.persistence;
import java.util.List;
import com.liferay.portal.SystemException;
public interface HelloPersistence {
public List getAll() throws SystemException;
}
在HelloPersistenceImpl中写入:
package com.ext.portlet.hello.persistence;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import com.liferay.portal.SystemException;
import com.liferay.portal.service.persistence.BasePersistence;
public class HelloPersistenceImpl extends BasePersistence
implements HelloPersistence{
public List getAll()throws SystemException{
Session session = null;
try{
session = openSession();
StringBuffer str = new StringBuffer();
str.append("From com.ext.portlet.hello.beans.HelloBeans");
Query q = session.createQuery(str.toString());
q.setCacheable(true);
return q.list();
}catch(HibernateException he){
throw new SystemException(he);
}finally{
closeSession(session);
}
}
}
修改action里的内容,将HelloAction修改:
package com.ext.portlet.hello.action;
import com.ext.portlet.hello.beans.HelloBeans;
import com.ext.portlet.hello.manager.HelloUtil;
import com.liferay.portal.struts.PortletAction;
import java.util.ArrayList;
import java.util.List;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.WindowState;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class HelloAction extends PortletAction {
public ActionForward render(ActionMapping mapping, ActionForm form,
PortletConfig config, RenderRequest req, RenderResponse res)
throws Exception {
if (req.getWindowState().equals(WindowState.NORMAL)) {
return mapping.findForward("portlet.ext.hello.view");
} else {
List hello = HelloUtil.getAll();
List reports = new ArrayList();
for (int i = 0; i < hello.size(); i++) {
reports.add(((HelloBeans) hello.get(i)).getName());
req.setAttribute("reports", reports);
}
return mapping.findForward("portlet.ext.hello.view_reports");
}
}
}
3. 添加配置文件内容:
在ext-impl/src/META-INF中的ext-hbm.xml中添加信息
<class name="com.ext.portlet.hello.beans.HelloBeans" table="hello">
<id name="id" column="id" type="int">
<generator class="native"></generator>
</id>
<property name="name" column="name" type="com.liferay.util.dao.hibernate.StringType"></property>
</class>
在ext-impl/src/META-INF中的ext-spring.xml中添加信息
<bean id="com.ext.portlet.hello.manager.HelloUtil" class="com.ext.portlet.hello.manager.HelloUtil" lazy-init="true">
<property name="service">
<ref bean="com.ext.portlet.hello.service.transaction"/>
</property>
</bean>
<bean id="com.ext.portlet.hello.service.transaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true">
<property name="transactionManager">
<ref bean="liferayTransactionManager" />
</property>
<property name="target">
<ref bean="com.ext.portlet.hello.service.impl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="search*">PROPAGATION_REQUIRED</prop>
<prop key="deleteValue">PROPAGATION_REQUIRED,-com.liferay.portal.PortalException,-com.liferay.portal.SystemException</prop>
<prop key="askValue">PROPAGATION_REQUIRED,-com.liferay.portal.PortalException,-com.liferay.portal.SystemException</prop>
</props>
</property>
</bean>
<bean id="com.ext.portlet.hello.service.impl" class="com.ext.portlet.hello.persistence.HelloPersistenceImpl" lazy-init="true">
<property name="dataSource">
<ref bean="liferayDataSource" />
</property>
<property name="sessionFactory">
<ref bean="liferaySessionFactory" />
</property>
</bean>
4. 在数据库中新建表hello,列名为id,name,分别为int,varchar,自己添加几个数据。
这样就完成了。
- liferay开发(二)开发详细举例--例二
- liferay开发(二)开发详细举例--例一
- Liferay Portal开发(二)安装配置开发环境
- Liferay 6.1开发学习(二):创建一个Portlet工程
- liferay开发之二:简单Struts Portlet开发
- SSH整合开发详细二
- 研究:Liferay研究(二)开发Liferay中几个常用的标签
- Liferay开发之二 中文问题的解决(中文附件)
- Liferay开发之二 中文问题的解决(中文附件)
- Liferay Portal 学习(二)
- Liferay全局配置(二)
- Liferay全局配置(二)
- windows驱动开发技术详细读书笔记(二)
- Myeclipse6.5 + Maven 开发web工程详细配置(二)
- Liferay开发指南(1)
- Liferay开发指南(1)
- Liferay开发(不断整理)
- Liferay开发学习(1)
- 含有/F的for(转)
- quartz一分钟教程 java任务调度
- 华天动力协同办公系统V380中小企业版功能简介
- liferay开发(二)开发详细举例--例一
- ASP.NET - 对 ObjectDataSource 控件使用参数
- liferay开发(二)开发详细举例--例二
- 如何在Linux下部署web程序(搭建vsftp服务,搭建sshd 服务器,jdk安装步骤,Tomcat安装步骤,MySQL安装步骤)
- josso1.8在tomcat6下安装配置
- 提供的费的风格
- 键盘输入字母显示数字的问题 ——FN键的特殊应用
- DevExpress.XtraCharts.ChartControl控件的简单使用 摘自http://leo43.blog.hexun.com/36926221_d.html
- VC++ WIN32用户界面设计基础之Cursor篇
- AJAX实时读取输入文本
- pku 2385