【问题标题】:how to insert data in db using liferay portlet如何使用 liferay portlet 在数据库中插入数据
【发布时间】:2013-03-30 08:00:50
【问题描述】:

如何使用 Liferay 在 MySql 中插入数据?

我已经从edit.jsp 创建了Edit.jsp、view.jsp 我想输入数据并且在view.jsp 中我想显示我的数据。在 edit.jsp 中输入的数据应该存储在我的 mysql 表中。我已经创建了 service.xml、portal-ext.properties。

我也有 java 文件。请告诉我应该在哪里编写插入逻辑以将数据存储到数据库中。

这是我的 Java 代码。我有 edit.jsp 文件和 view.jsp 文件,我使用 service.xml 文件创建了表,并将我的 portal-ext.properties 放在类文件夹中。有什么遗漏吗?我是liferay的新手

package com.portlet;

import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portlet.model.testimonial;
import com.liferay.portlet.service.testimonialLocalServiceUtil;

public class Testimonial extends GenericPortlet {

    protected String editJSP;
    protected String viewJSP;
    private static Log _log = LogFactory.getLog(Testimonial.class);

    public void init() throws PortletException 
    {
        editJSP = getInitParameter("edit-jsp");
        viewJSP = getInitParameter("view-jsp");
    }

    public void doEdit(RenderRequest renderRequest,RenderResponse renderResponse) throws IOException, PortletException 
    {
        renderResponse.setContentType("text/html");
        PortletURL addNameURL = renderResponse.createActionURL();
        addNameURL.setParameter("addName", "addName");
        renderRequest.setAttribute("addNameURL", addNameURL.toString());
        include(editJSP, renderRequest, renderResponse);
    }

    public void doView(RenderRequest renderRequest,RenderResponse renderResponse) throws  IOException, PortletException
    {
        PortletPreferences prefs = renderRequest.getPreferences();
        String username = (String) prefs.getValue("name", "");
        String area=(String)prefs.getValue("area", "testimonial");
        String email=(String)prefs.getValue("email", "");
        String subject=(String)prefs.getValue("subject", "");
        String company=(String)prefs.getValue("company", "");
        String designation=(String)prefs.getValue("designation", "");

        if (username.equalsIgnoreCase ("")) 
        {
            username = "";
        }

        renderRequest.setAttribute("userName", username);
        renderRequest.setAttribute("area",area);
        renderRequest.setAttribute("email",email);
        renderRequest.setAttribute("subject",subject);
        renderRequest.setAttribute("designation",designation);
        renderRequest.setAttribute("company",company);

        include(viewJSP, renderRequest, renderResponse);
    }

    public void processAction(ActionRequest actionRequest, ActionResponse  actionResponse) throws IOException, PortletException 
    {       
        String addName = actionRequest.getParameter("addName");

        if (addName != null)
        {
            PortletPreferences prefs = actionRequest.getPreferences();
            prefs.setValue("name", actionRequest.getParameter("username"));
            prefs.setValue("area",actionRequest.getParameter("area"));
            prefs.setValue("email",actionRequest.getParameter("email"));
            prefs.setValue("subject",actionRequest.getParameter("subject"));
            prefs.setValue("designation",actionRequest.getParameter("designation"));
            prefs.setValue("company",actionRequest.getParameter("company"));

            prefs.store();

            testimonial testimonial = null;

            try {
                testimonialLocalServiceUtil.createtestimonial(CounterLocalServiceUtil.increment());
            } catch (SystemException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            testimonial.setSubject(actionRequest.getParameter("subject"));
            testimonial.setArea(actionRequest.getParameter("area"));
            testimonial.setUsername(actionRequest.getParameter("username"));
            testimonial.setEmail(actionRequest.getParameter("email"));
            testimonial.setCompany(actionRequest.getParameter("company"));
            testimonial.setDesignation(actionRequest.getParameter("designation"));

            try {
                testimonialLocalServiceUtil.addtestimonial(testimonial);
            } catch (SystemException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            actionResponse.setPortletMode(PortletMode.VIEW);    
        }   
    }

    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);
        }
    }
}

【问题讨论】:

  • 正如我之前的帖子中提到的。我使用 service.xml 创建表,然后构建服务,但我无法在数据库中看到表。我把 portal-ext.properties 放在类中
  • 你好告诉我..我应该遵循哪一步?
  • 啊,对不起。我被你的 mysql 标签误导了。你想坚持我现在看到的 PortletPreferences。

标签: java mysql liferay


【解决方案1】:

按照以下步骤操作:

    File-->new-->Liferay Project
    Right Click on WEB-INF-->new-->Liferay Service Builder-->Enter the package path(com.database) and namespace(A)-->Finish
    Now you have service.xml-->change the properties accoding to your requirement-->save it
    Right click on service.xml-->Liferay-->Build Services.
    Now you will get  some folders,classes and interfaces under docroot/ WEB-INF/src
    change in the  LiferayLocalServiceImpl.java  under com.database.service.impl
    create one package  like com.portlet in docroot/ WEB-INF/src -->create a class and continue the application

服务.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.database">
    <author>name</author>
    <namespace>S</namespace>

    <entity name="Book" local-service="true" remote-service="true">

        <!-- PK fields -->

        <column name="fooId" type="long" primary="true" />

        <!-- Audit fields -->

        <column name="companyId" type="long" />
        <column name="userId" type="long" />
        <column name="bookName" type="String" />
        <column name="author" type="String" />

    </entity>
</service-builder>

在jsp中:

    <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    <portlet:actionURL var="addBURL" name="addB"/>

    <aui:form method="post" action="<%=addBURL.toString()%>" name="fm"
        id="fm">
    <!--    <input type="text" name="bookName"/>

        <input type="text" name="author"/> -->

            <aui:input name="bookName" label="BookName" type="text"></aui:input>
            <aui:input name="author" label="Author" type="text"></aui:input>
            <aui:button type="submit" value="Submit" />

    <!-- <input type="button" name="submit" value="Submit"> -->
    </aui:form>

com.database.service.impl.BookLocalServiceImpl

    package com.database.service.impl;

    import com.database.model.Book;
    import com.database.service.base.BookLocalServiceBaseImpl;
    import com.liferay.portal.kernel.exception.SystemException;

    public class BookLocalServiceImpl extends BookLocalServiceBaseImpl {

        public Book aaddBook(Book bookParam) {

            Book book;

            try {
                book = bookPersistence.create(counterLocalService
                        .increment(Book.class.toString()));
            } catch (SystemException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return book = null;
            }
            book.setAuthor(bookParam.getAuthor());
            book.setBookName(bookParam.getBookName());
            book.setCompanyId(bookParam.getCompanyId());
            book.setUserId(bookParam.getUserId());
            try {
                return bookPersistence.update(book, false);
            } catch (SystemException e) {
                e.printStackTrace();

            }

            return book;
        }

    }

com.portlet 示范课

    package com.portlet;

    import java.io.IOException;

    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.portlet.PortletException;

    import com.database.model.Book;
    import com.database.model.impl.BookImpl;
    import com.database.service.BookLocalServiceUtil;
    import com.liferay.util.bridges.mvc.MVCPortlet;

    public class Demo extends MVCPortlet{

        public void addB(ActionRequest actionRequest,
                ActionResponse actionResponse) throws IOException, PortletException {
            System.out.println("Action");
            String author=actionRequest.getParameter("author");
            String bookName=actionRequest.getParameter("bookName");
            Book book=new BookImpl();
            book.setAuthor(author);
            book.setBookName(bookName);
            BookLocalServiceUtil.aaddBook(book);

        }

    }

【讨论】:

    【解决方案2】:

    您必须使用 Liferay 默认 API 访问和插入默认 Liferay 表中的数据,或者如果您想访问/插入数据库中的自定义(自制)表,则必须使用 Liferay Service Builder。

    在您的问题中 - 您可以先在数据库中手动创建表。

    然后您可以创建 service.xml 并使用 ant 构建文件构建服务,该文件将为您提供 LocalServiceUtil 类。在您的情况下,它是 testimonialLocalServiceUtil 类。

    同样根据您的代码,processAction 方法覆盖是正确的。

    请检查 service.xml 中的表和列规范。应该没有问题。

    这是一个可以帮助你的链接-

    http://www.phloxblog.in/liferay-service-builder-step-step/

    并且,如果您有任何异常,请与我们分享,以便我们了解问题所在。

    谢谢

    【讨论】:

    • liferay默认API如何使用?
    • 当我右键单击 myportlet 时,我看不到服务构建器选项。我必须手动制作 service.xml 文件。是不是少了什么东西?
    • 这些是 Liferay 默认的类和方法,它们在 SDK 中作为库公开。但是您不能对自定义表使用默认类和方法。在那里你必须通过服务构建器提供服务。
    • 请确保您处于 Eclipse 中的“Liferay”透视图中。然后选择项目->新建->Liferay Service Builder。谢谢。
    • 我已经从你提供的那个链接下载了 eclipse.. 我有 ubuntu 32 位。我从本地驱动器中删除了我的旧 Eclipse,然后我解压缩了我最新下载的 Eclipse 版本。但是当我尝试查看服务生成器时,我仍然看不到..请帮助我应该怎么做... :(
    【解决方案3】:

    我不同意@kartik 关于打电话给Book book = new BookImpl(); 改用Book book = BookLocalServiceUtil.createBook(0)

    https://web.liferay.com/community/forums/-/message_boards/message/79177774

    为了节省,我也推荐这篇文章。

    https://javaeasyforu.blogspot.de/2014/06/insert-data-into-db-using-service.html

    【讨论】:

      猜你喜欢
      • 2015-08-20
      • 1970-01-01
      • 2019-11-18
      • 2014-05-16
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多