0

我想从mysql中检索数据并插入数据。

我提供 3 个文件,一个 java 文件和两个 jsp 文件edit.jspview.jsp分别编辑和查看数据。

我已经使用创建表ServiceBuilder,我已经把我portal-ext.properties的类文件夹,告诉我这是完美的方法吗?我以正确的方式做吗?

我想先插入数据,然后我想从数据库中检索数据。

  1. 我正在通过以下 jsp 文件插入数据 -edit.jsp

    <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
    
    <jsp:useBean class="java.lang.String" id="addNameURL" scope="request" />
    <jsp:useBean class="java.lang.String" id="area" scope="request"/>
    <jsp:useBean class="java.lang.String" id="email" scope="request"/>
    <jsp:useBean class="java.lang.String" id="subject" scope="request"/>
    <jsp:useBean class="java.lang.String" id="compnay" scope="request"/>
    <jsp:useBean class="java.lang.String" id="designation" scope="request"/>
    
    <portlet:defineObjects />
    
    <form id="<portlet:namespace />helloForm" action="<%= addNameURL %>"method="post">
        <table>
            <tr>
                <td>Subject:</td>
                <td><input type="text" name="subject"></td>
            </tr>
            <tr>
                <td>
                Write Your Testimonial
                </td>
                <td><textarea name ="area"></textarea>
                </td>
            </tr>
            <tr>
                <td>Name:</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><input type="text" name="email"></td>
            </tr>
            <tr>
                <td>Company:</td>
                <td><input type="text" name="company"></td>
            </tr>
            <tr>
                <td>Designation:</td>
                <td><input type="text" name="designation"></td>
            </tr>
        </table>
    
        <input type="submit" id="nameButton" title="Submit" value="Submit">
    </form>
    
  2. 我已经在以下 java 文件中编写了我的插入逻辑 - Testimonial1

    package com.liferay.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 Testimonial1 extends GenericPortlet {
    
        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());
                    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"));
                    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);
            }
        }
        protected String editJSP;
        protected String viewJSP;
        private static Log _log = LogFactory.getLog(Testimonial1.class);
    }
    
  3. 我已经在以下文件中编写了我的视图逻辑 -view.jsp我想从以下文件中的数据库中检索数据:

    <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
    
    <jsp:useBean id="subject" class="java.lang.String" scope="request"/>
    <jsp:useBean id="area" class="java.lang.String" scope="request"/>
    <jsp:useBean id="userName" class="java.lang.String" scope="request" />
    <jsp:useBean id="email" class="java.lang.String" scope="request"/>
    <jsp:useBean id="company" class="java.lang.String" scope="request"/>
    <jsp:useBean id="designation" class="java.lang.String" scope="request"/>
    <portlet:defineObjects />
    
    <p>This is the Testimonial portlet......... how are u all ..........</p>
    
    <p>Subject is ....<%=subject %></p>
    <p>Testimonial is .....<%=area %></p>
    <p>Hello <%= userName %>!</p>
    <p>your Email ......<%=email %></p>
    <p>your company .....<%=company %></p>
    <p>You are .......<%=designation %></p>
    
  4. 我的service.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.liferay.portlet">
        <author>ubuntu</author>
        <namespace>perception</namespace>
    
        <entity name="testimonial" local-service="true" remote-service="true">
            <column name="subject" type="String"></column>
            <column name="area" type="String"></column>
            <column name="username" type="String"></column>
            <column name="email" type="String"></column>
            <column name="company" type="String"></column>
            <column name="designation" type="String"></column>
        </entity>
    </service-builder>
    
  5. 我的portal-ext.properties文件:

    #
    # MySQL 
    #
    
    jdbc.default.driverClassName=com.mysql.jdbc.Driver 
    jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEn
    coding=UTF-8&useFastDateParsing=false
    jdbc.default.username=root
    jdbc.default.password=ubuntu123
    
    schema.run.enabled=true
    schema.run.minimal=true
    

我已经把我所有的文件都放好了,现在请告诉我我必须做些什么来插入和检索数据。

请告诉我插入代码是否正确?以及如何从数据库中检索数据?

4

2 回答 2

1

你可以看看 liferay 的 service builder。

如果您的数据与 liferay 不在同一个数据库中,您仍然可以使用 service builder

于 2013-04-05T08:23:28.963 回答
1

这是一条评论,但对于这种格式来说太长了——因此我将其添加为答案。

据我所知,您的代码存在一个重大缺陷,与基本问题无关:您使用 portlet VIEW 模式来显示数据,并使用 EDIT 模式来操作它。这是对 JSR-286 的常见误解,我学会了讨厌这种命名。

EDIT 旨在配置当前的 portlet - 当 EDIT 操作的结果是单个数据库记录的更改时,您几乎总是使用了错误的 portlet 模式。您必须使用 VIEW 模式(这只是 portlet 模式,与您的应用程序、读取或写入数据无关)。考虑到默认情况下 EDIT 模式只能由管理员访问,并且通常用于更改 PortletPreferences,很少用于更改数据库内容(这是一种简化,但是一个很好的经验法则)

出于这个原因,Liferay 在 UI 上将 EDIT 模式命名为“Preferences”,您通常通过 portlet 的上下文菜单导航到它(“Preferences”,原文如此!)

于 2013-04-06T14:19:29.190 回答