如何使用 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);
}
}
}