本文介绍了如何使用Liferay Portlet在db中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

How to insert data in MySql using Liferay?

我已经创建了Edit.jsp,来自edit.jsp的view.jsp,我想输入数据,并且要在view.jsp中显示数据.在edit.jsp中输入的这些数据应存储在我的mysql表中.我已经创建了service.xml,portal-ext.properties.

I have created Edit.jsp, view.jsp from edit.jsp I want to enter data and in view.jsp I want to show my data. This data which is enter in edit.jsp should be stored in my mysql table. I have created service.xml, portal-ext.properties.

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

I have java file also. Please tell me where should I write my insertion logic to store my data in to database.

这是我的Java代码.我有edit.jsp文件和view.jsp文件,我已经使用service.xml文件创建了表格,并将我的portal-ext.properties放在classes文件夹中.缺少任何东西吗?我是liferay的新人

Here is my Java code. I have edit.jsp file and view.jsp file, i have created table using service.xml file and I put my portal-ext.properties in classes folder. Is any thing missing? I am new in 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);
        }
    }
}

推荐答案

您必须使用Liferay默认API来访问和在默认Liferay表中插入数据,或者要访问/插入自定义(自制)表中在数据库中,则必须使用Liferay Service Builder.

You have to use either Liferay default API for accessing and inserting data in default Liferay tables or if you want to access/insert in Custom(self-made) tables in Database, then you have to use Liferay Service Builder.

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

In your question - you can create your tables manually in database first.

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

Then you can make service.xml and build the service using the ant build file which will give you the LocalServiceUtil classes. In your case it is testimonialLocalServiceUtil class.

根据您的代码,processAction方法的覆盖是正确的.

Also as per your code, the processAction method overriding is correct.

请检查service.xml中的表和列规格.它应该没有任何问题.

Please check the table and columns specifications in service.xml. It should not have any problem.

这是一个可能对您有帮助的链接-

Here is a link which may help you -

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

此外,如果您有任何例外情况,请与我们分享,以便我们了解问题所在.

And, if you get any exception, please share those to us, so that we can understand the problem area.

谢谢

这篇关于如何使用Liferay Portlet在db中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 13:47