java - how to to retrive data in jsp from MySQL using Liferay? -
i want retrieve data , insert data in mysql.
i m providing 3 file 1 java file , 2 jsp file edit.jsp
, view.jsp
edit , view data respectively.
i have created table using servicebuilder
, have put portal-ext.properties
in classes folder, tell me perfect method? m doing correct way?
i want first insert data , want retrieve data database.
i m inserting data through following jsp file -
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 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>
i have written insertion logic in following java file -
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 + " not valid include"); } else { portletrequestdispatcher.include(renderrequest, renderresponse); } } protected string editjsp; protected string viewjsp; private static log _log = logfactory.getlog(testimonial1.class); }
i have written view logic in following file -
view.jsp
, want retrive data database in following file:<%@ 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 testimonial portlet......... how u ..........</p> <p>subject ....<%=subject %></p> <p>testimonial .....<%=area %></p> <p>hello <%= username %>!</p> <p>your email ......<%=email %></p> <p>your company .....<%=company %></p> <p>you .......<%=designation %></p>
my
service.xml
file<?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>
my
portal-ext.properties
file:# # 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
i have put files, please tell me have data insertion , retrieval.
pls tell right in insertion code? , how retrive data database?
you may take @ liferay's service builder.
http://www.liferay.com/community/wiki/-/wiki/main/service+builder
http://www.liferay.com/documentation/liferay-portal/6.0/development/-/ai/service-build-2
if data not in same database liferay, can still use service builder well
Comments
Post a Comment