Liferay Struts Portlet 3

1.Create a database structure

Move service.xml in .../ext/ext-ejb/ to .../ext/ext-ejb/src/com/ext/portlet/reports/

Create a new service.xml in .../ext/ext-ejb/ , contents:

<? xml version="1.0" ?>
<! DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 4.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_4_0_0.dtd" >
< service-builder  root-dir =".."  package-path ="com.ext.portlet" >
    
< portlet  name ="Library"  short-name ="Library"   />
    
    
< entity  name ="Book"  local-service ="true" >
        
<!--  PK fields  -->
        
< column  name ="bookId"  type ="String"  primary ="true"    />
        
<!--  Other fields  -->
        
< column  name ="title"  type ="String"   />
        
    
</ entity >
    
</ service-builder >

2.Auto generating the Service Layer code and SQL

.../ext/ext-ejb>ant build-service

3.Modifying MySQL to include new table

portal-tables.sql Updated, the following is added

create   table  Book (
    bookId 
VARCHAR ( 75 not   null   primary   key ,
    title 
VARCHAR ( 75 null
);

Login MySQL,

mysql>use training;

mysql>show tables;

confirm that the "Book" table does not exist yet,

Copy the generate SQL code, paste it into the CMD prompt

mysql>show tables;

comfirm that the "Book" table exist now.

4.Create method to add record to the database

BookLocalServiceImpl.java in .../ext/ext-ejb/src/com/ext/portlet/library/service/impl/ , content:

package  com.ext.portlet.library.service.impl;
import  java.rmi.RemoteException;
import  java.util.List;
import  com.ext.portlet.library.model.Book;
import  com.ext.portlet.library.service.persistence.BookUtil;
import  com.ext.portlet.library.service.BookLocalService;
import  com.liferay.counter.service.CounterServiceUtil;
import  com.liferay.portal.PortalException;
import  com.liferay.portal.SystemException;
public   class  BookLocalServiceImpl  implements  BookLocalService  {
    
    
public Book addBook(String userId, String title) throws PortalException, SystemException, RemoteException {
        
        Book book 
= BookUtil.create(Long.toString(CounterServiceUtil.increment(Book.class.getName())));
        book.setTitle(title);
        
return BookUtil.update(book);
    }

    
public List getAll() throws PortalException, SystemException {
        
return BookUtil.findAll();
    }

}

...ext/ext-ejb>ant build-service (regenerate service layer code)

5.Update existing files

AddBookAction.java

package  com.ext.portlet.library.action;
import  javax.portlet.ActionRequest;
import  javax.portlet.ActionResponse;
import  javax.portlet.PortletConfig;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionMapping;
import  com.ext.portlet.library.service.BookLocalServiceUtil;
import  com.liferay.portal.struts.PortletAction;

public   class  AddBookAction  extends  PortletAction  {
    
public void processAction(
            ActionMapping mapping, ActionForm form, PortletConfig config,
            ActionRequest req, ActionResponse res)
        
throws Exception {
        
        String bookTitle 
= req.getParameter("book_title");
        
        
if ( "".equals(bookTitle) || bookTitle == null ) {
            setForward(req, 
"portlet.ext.library.error");
        }

        
else {    
            BookLocalServiceUtil.addBook(req.getRemoteUser(),bookTitle);
                            
            setForward(req, 
"portlet.ext.library.success");
        }

    }

}

init.jsp

<% @ include file="/html/common/init.jsp"  %>
<% @ page import="com.ext.portlet.library.model.Book"  %>
<% @ page import="com.ext.portlet.library.service.BookLocalServiceUtil"  %>
<% @ page import="java.util.ArrayList"  %>
<% @ page import="java.util.Iterator"  %>

6.Retrieve Records from the Database for display

success.jsp

<% @ include file="/html/portlet/ext/library/init.jsp"  %>
< c:if  test ="<%= books != null %>" >
< h1 > Book Listings </ h1 >

< table  align ="center"  cellspacing ="10"  cellpadding ="3" >
    
< tr >
        
< td  style ="font-weight:bold" > Book Id </ td >
        
< td  style ="font-weight:bold" > Book Title </ td >
    
</ tr >

    
< c:if  test ="<%= books != null %>" >
<%
        Iterator itr 
= books.iterator();
    
        
while (itr.hasNext()) {
            book 
= (Book)itr.next();    
%>     
    
        
< tr >
            
< td >
                
<% =  book.getBookId()  %>
            
</ td >
            
< td >
                
<% =  book.getTitle()  %>
            
</ td >
        
</ tr >
    
<%         
        }
%>
    
</ c:if >
    
</ table >

</ c:if >

...ext/ext-ejb>ant deploy ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值