Reputation: 434
How to insert data in MySql using Liferay?
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.
I have java file also. Please tell me where should I write my insertion logic to store my data in to database.
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);
}
}
}
Upvotes: 1
Views: 12970
Reputation: 364
I would disagree with @kartik about calling Book book = new BookImpl()
;
Use Book book = BookLocalServiceUtil.createBook(0)
instead
https://web.liferay.com/community/forums/-/message_boards/message/79177774
And for saving I would also recommend this article.
https://javaeasyforu.blogspot.de/2014/06/insert-data-into-db-using-service.html
Upvotes: 0
Reputation: 147
Follow the below steps:
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
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.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>
In 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 Demo Class
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);
}
}
Upvotes: 5
Reputation: 1764
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.
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.
Also as per your code, the processAction
method overriding is correct.
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.
Thanks
Upvotes: 2