liferay,liferay-6 , I am working on a MVC Portlet and the document i am saving is not going to db


I am working on a MVC Portlet and the document i am saving is not going to db

Question:

Tag: liferay,liferay-6

Here is my Action Class, where I have to save any document to database but its not working and getting error in console that NoSuchMethodException (javax.portlet.ActionRequest, javax.portlet.ActionResponse)

package com.test.docupload;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.PortletException;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.captcha.CaptchaMaxChallengesException;
import com.liferay.portal.kernel.captcha.CaptchaTextException;
import com.liferay.portal.kernel.captcha.CaptchaUtil;
import com.liferay.portal.kernel.dao.jdbc.OutputBlob;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.upload.UploadRequest;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.test.docup.model.Doc;
import com.test.docup.service.DocLocalServiceUtil;

/**
 * Portlet implementation class DocumentUploadAction
 */
public class DocumentUploadAction extends MVCPortlet {

    public void addDoc(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException {

        UploadRequest uploadRequest = PortalUtil.getUploadPortletRequest(resourceRequest);
        File docImage = uploadRequest.getFile("doc");
        String title = ParamUtil.getString(uploadRequest, "firstName");
        InputStream fis = new FileInputStream(docImage);
        OutputBlob dataOutputBlob = new OutputBlob(fis, docImage.length());
        try {
            Doc doc = DocLocalServiceUtil.createDoc(CounterLocalServiceUtil.increment());
            doc.setDocName(title);
            doc.setOwner(10199);
            doc.setData(dataOutputBlob);
            doc = DocLocalServiceUtil.addDoc(doc);
            if (doc != null) {
                SessionMessages.add(resourceRequest.getPortletSession(), "document-added-success");
                System.out.println("document-added-success");

            } else {
                SessionErrors.add(resourceRequest.getPortletSession(), "document-added-failed");
                System.out.println("document-added-failed::");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here is my jsp

<%@page import="com.liferay.portal.kernel.servlet.SessionMessages"%>
<%@ include file="init.jsp" %>

<c:if test='<%=SessionMessages.contains(renderRequest.getPortletSession(),"document-added-success")%>'>
    <liferay-ui:success key="document-added-success" message="Document has been added successfully." />
</c:if>

<c:if test='<%=SessionMessages.contains(renderRequest.getPortletSession(),"document-added-failed")%>'>
    <liferay-ui:error key="document-added-failed" message="Document Uploaded failed." />
</c:if>

<portlet:actionURL  var="addDocActionURL" name="addDoc"></portlet:actionURL>
<aui:form action="<%= addDocActionURL %>" method="post" name="fm" enctype="multipart/form-data">
    <aui:input name="title" value="" label="Doc Title"/>
    <aui:input  name="Doc" value="" type="file" label="Select Doc"/>
    <aui:button-row>
        <aui:button type="submit" />
    </aui:button-row>
</aui:form>

I have changed the ActionRequest to ResourceRequest but still its not working.


Answer:

There are certain mistakes in your code:

  1. You forgot to include tag reference for aui in your JSP.

  2. Attribute name for file type input was name="Doc", and you were getting parameter value in your action as

File docImage = uploadRequest.getFile("doc");

It should be consistent, either "Doc" / "doc" in both view and action.

  1. You created URL using <portlet:actionURL> tag but were linking it to action type:

public void addDoc(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException {

which is valid for the URL created using <portlet:resourceURL> tag. For actionURL your action method should be:

public void addDoc(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {

  1. Replace all resourceRequest with actionRequest

  2. UploadRequest should be UploadPortletRequest

  3. Link your action class <portlet-class> to JSP in portlet.xml as,

com.test.docupload.DocumentUploadAction

So, following is the updated code:

Portlet.xml

<?xml version="1.0"?>

<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" 
             version="2.0">
    <portlet>
        <portlet-name>document</portlet-name>
        <display-name>Document Portlet</display-name>
        <portlet-class>com.test.docupload.DocumentUploadAction</portlet-class>
        <init-param>
            <name>view-template</name>
            <value>/view.jsp</value>
        </init-param>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
        </supports>
        <portlet-info>
            <title>Document Portlet</title>
            <short-title>Document Portlet</short-title>
            <keywords>Document Portlet</keywords>
        </portlet-info>
        <security-role-ref>
            <role-name>administrator</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>guest</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>power-user</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>user</role-name>
        </security-role-ref>
    </portlet>
</portlet-app>

JSP:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>

<portlet:defineObjects />

<%@page import="com.liferay.portal.kernel.servlet.SessionMessages"%>
<%@ include file="init.jsp" %>

<c:if test='<%=SessionMessages.contains(renderRequest.getPortletSession(),"document-added-success")%>'>
    <liferay-ui:success key="document-added-success" message="Document has been added successfully." />
</c:if>

<c:if test='<%=SessionMessages.contains(renderRequest.getPortletSession(),"document-added-failed")%>'>
    <liferay-ui:error key="document-added-failed" message="Document Uploaded failed." />
</c:if>

<portlet:actionURL var="addDocActionURL" name="addDoc"></portlet:actionURL>
<aui:form action="<%= addDocActionURL %>" method="post" name="fm" enctype="multipart/form-data">
    <aui:input name="title" value="" label="Doc Title"/>
    <aui:input name="doc" value="" type="file" label="Select Doc"/>
    <aui:button-row>
        <aui:button type="submit" />
    </aui:button-row>
</aui:form>

DocumentUploadAction:

package com.test.docupload;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.PortletException;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.captcha.CaptchaMaxChallengesException;
import com.liferay.portal.kernel.captcha.CaptchaTextException;
import com.liferay.portal.kernel.captcha.CaptchaUtil;
import com.liferay.portal.kernel.dao.jdbc.OutputBlob;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

import com.liferay.portal.kernel.upload.UploadPortletRequest;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

/**
 * Portlet implementation class DocumentUploadAction
 */
public class DocumentUploadAction extends MVCPortlet {

    public void addDoc(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {

        UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
        File docImage = uploadRequest.getFile("doc");
        String title = ParamUtil.getString(uploadRequest, "firstName");
        InputStream fis = new FileInputStream(docImage);
        OutputBlob dataOutputBlob = new OutputBlob(fis, docImage.length());
        try {
            Doc doc = DocLocalServiceUtil.createDoc(CounterLocalServiceUtil.increment());
            doc.setDocName(title);
            doc.setOwner(10199);
            doc.setData(dataOutputBlob);
            doc = DocLocalServiceUtil.addDoc(doc);
            if (true) {
                SessionMessages.add(actionRequest.getPortletSession(), "document-added-success");
                System.out.println("document-added-success");

            } else {
                SessionErrors.add(actionRequest.getPortletSession(), "document-added-failed");
                System.out.println("document-added-failed::");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related:


Form submit portlet with Spring MVC


java,jsp,spring-mvc,liferay,portlet
I'm trying to achieve a Liferay portlet of submit form using spring MVC. The model: package com.model; public class Person { String firstName; String middleName; public String getFirstName() { return this.firstName; } public String getMiddleName() { return this.middleName; } public void setFirstName(String firstName) { this.firstName=firstName; } public void setMiddleName(String middleName)...

Cons of a liferay cms while website development using Community Edition


content-management-system,liferay
I am new to LIFERAY CMS. i need cons of a liferay cms i.e., difficulties faced while developing and using liferay cms. can you please list cons of liferay cms.

Service builder doesn't delete table in DB


liferay,liferay-6,liferay-service-builder
Hi all, I'm using LR 6.1. I created a new entity Called " Recommendation" in service.xml then a run BuildServices with Ant to generate the service builder Files. Now I want to delete the Entity. So I Deleted if from service.xml, but nothing happens in Data base and the other...

Lifefay freemarker ADT: methods unavailable?


java,liferay,liferay-6,freemarker
I'm trying to work on some ADT for asset publisher (Documents and Media type), but I often have a message telling me that : Expression assetRenderer.getDataRepositoryId is undefined or Expression assetRenderer.getFileEntryId is undefined I found some threads where people said that we must add velocity.engine.restricted.classes= velocity.engine.restricted.variables= freemarker.engine.restricted.classes= freemarker.engine.restricted.variables= in portal-ext.properties...

Portlet event send array of objects


arrays,events,liferay,portlet
We have multiple projects with multiple portlets and need to send an array of objects between them. Our situation: One of the porlets is like a "Master-portlet", it will be responsible for all the REST-calls and consume json-data and parse it to Java-Objects. All the other portlets will receive an...

Does'nt Liferay work at different ports?


liferay,server,portal
I have developed a portal page using Liferay and the port number is 8080. I wanted to test SSO, I decided not to try with the existing one as there might be some problems and I might end up crashing the whole thing. As a result I downloaded a new...

How can I configure AOP in Liferay Service Builder?


liferay,aop
I want to to intercept a method in Service Builder, for example: XXXLocalService.update(). But I don't know the correct way to do this. I have done some research but I haven't found a clear way to do this. Any help will be greatly appreciated....

How manage styles in Vaadin portlet in Liferay


css3,liferay,vaadin,portlet
I have a Portlet, where is used Valo theme. Most graphical components are displayed properly, but TextField not. It should have rounded border radius and height should be about 35px. It has normally about 15px and the field is angular. When I narrow width of browser window to about 200px...

How to upload file in relative directory


java,liferay
I have made a application where we can upload any file which will save in our local given directory. I want to modify it, i want to add a drop down (with multiple option i.e floor, store, section) for department. i.e if we want to upload a file in 'Store'...

Liferay roles and permissions for publishing


permissions,liferay-6,roles
I am new to Liferay, since there are many roles in Liferay, i'm in confusion. I need to implement a particular scenario like, Admin --> can publish Users(under admin) --> can edit content and make request to admin for publishing can any one explain how to implement this...

How can I change the zIndex attribute of aui-datepicker in Liferay Portlet 6.2


datepicker,liferay,z-index,liferay-aui,alloy-ui
I have discoveder one problem with date field in portlet in LF 6.2. The problem is that after clicking on the input field, the datepicker is displaying, but with zIndex=0, that means it is under the portlet. If I change on firebug that parameter to 1, everything is OK. I...

Identify that the logged in user is an administrator or not using velocity


liferay,liferay-6,liferay-theme
I would like to identify that the logged in user is an administrator or not using velocity. The goal is to show the dockbar to the administrator user(s) only. I have tried following code, but is not working for me: #if ($is_signed_in && $permissionChecker.isGroupAdmin($group_id)) #dockbar() #end Please help me!!!...

Users in Guest role see default theme instead custom one for given layouts


liferay,liferay-6,liferay-theme
Users in Guest role see default theme instead custom one for pages which inherit from defined custom theme. This seems to be very strange problem. Even though layouts have specified to inherit theme from parent theme: Use the same look and feel of the Public Pages. Guests see them as...

commandButton action method not invoked in Liferay


jsf,liferay,portlet
I've the below portlet view.xhtml: <?xml version="1.0"?> <f:view xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"> <h:body> <h:form> <h:commandButton value="TESTButton" action="#{navigationViewBean.submit}" /> <h:outputText value="TESTGetter: #{navigationViewBean.testField}" /> </h:form>...

liferay portlet non-liferay JNDI data source null


oracle,tomcat7,jndi,liferay-6,portlet
For a Liferay 6.2 custom portlet accessing a non liferay Oracle database we are running into an issue where the data source returned is null. We have configured the tomcat/conf/context.xml <!-- Adding custom New non liferay datasource --> <Resource name="jdbc/NewPool" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST...

Customizing My Sites portlet in Liferay


liferay
I want to customize My Sites portlet (which is out of box portlet) in liferay using ext. How do I do this? How do I get the source code for the my sites portlet?

Incorrect liferay-plugins dependency


gradle,dependencies,liferay,liferay-6.2
In https://github.com/liferay/liferay-plugins/blob/6.2.x/build-plugins.gradle#L9 following dependency is defined: dependencies { classpath group: "com.liferay.portal", name: "portal-service", version: "7.0.0-SNAPSHOT" } How can a 6.2.x version have a dependency on 7.0.0 version? Is it a bug? When I peek to the history of the file, the dependency has always been there. I am not able...

Liferay control panel to code


java,liferay-6
I'm new to Liferay but I have to update (fix bug and add some enhancement) a site made with Liferay 6.1.1 I have a form that create an organization and a blank site for it. The problem is that currently, an admin have to go to the control panel, change...

In liferay market place, I am unable to submit the project


liferay,liferay-6
In Liferay market place i am stuck at the place "Upload Liferay Plugin Packages " When ever i am trying to upload a war file Please help me to short it out :)...

Get list of users in liferay


liferay
I want to display users list on liferay. and I am having problem with it. Here is my action class. public void userList(ActionRequest actionRequest, ActionResponse actionResponse) throws SystemException { // Todo Logic for user code try { int countUser = UserLocalServiceUtil.getUsersCount(); log.info("User Present In DB" + countUser); List < User...

I am working on a MVC Portlet and the document i am saving is not going to db


liferay,liferay-6
Here is my Action Class, where I have to save any document to database but its not working and getting error in console that NoSuchMethodException (javax.portlet.ActionRequest, javax.portlet.ActionResponse) package com.test.docupload; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.portlet.ResourceRequest; import javax.portlet.ResourceResponse; import javax.portlet.PortletException; import com.liferay.counter.service.CounterLocalServiceUtil; import...

How to create service builder for liferay plugin project with maven


liferay,liferay-ide,liferay-service-builder
I have Already create Liferay Plugin project. and maven install also over. it gives ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] --------------------------------------------------------------------- [INFO] Total time: 1:29.773s [INFO] Finished at: Wed Jun 17 15:46:10 IST 2015 [INFO] Final Memory: 34M/151M [INFO] -----------------------------. But unable to add service builder. When i try to add...

How to ignore Liferay user removed from LDAP errors?


ldap,log4j,liferay,liferay-6,openldap
I have a Liferay 6.1 instance that is connected to LDAP. New users get imported nicely, but when I remove a user from the LDAP directory, Liferay starts throwing exceptions when it tries to sync users from LDAP. These seem to be safe to ignore, but they produce several megabytes...

Liferay 6.2 clustering issue with multicast


lucene,cluster-computing,liferay-6,ehcache,jgroups
I am trying to cluster ehcache and lucene with Liferay 6.2 EE sp2 bundle on 2 servers with mutlicast enabled. WE have Apache HTTPD servers fronting tomcat servers using reverse proxy. A valid 6.2 license is deployed on both the nodes. We user the following properties in the portal-ext.properties: cluster.link.enabled=true...

AJAX Call not working in liferay 6.2.2?


ajax,liferay,hook,liferay-6
I am trying to perform a AJAX Call. But what i get is ResourceURL as undefined when i see the error in chrome. Below is my code. Declaration: <liferay-portlet:resourceURL copyCurrentRenderParameters="<%= false %>" var="mainURL" /> Implementation: xhr2.open("POST", resourceURL , false); But at implmentation i get resourceURL as undefined. In 6.1 it...

How to pass data from page to Portlet class through AJAX call?


ajax,jsp,liferay,portlet
I'm trying to pass a value in an AJAX 'GET' request to my Portlet class, but I can't quite figure out a way to access such variable's value in the class. My AJAX code is: function loadXMLDoc() { var nocache = new Date().getTime(); var xmlhttp=new XMLHttpRequest(); var url = "${mURL}";...

How to pass HTTP request to Web-Service


ajax,web-services,liferay,liferay-6,portlet
From my portlet JSP, I am trying to make a Ajax call to web-service. My aim is to pass HTTP request object as a parameter to web-service method. Please suggest.

How to host a newportal instance in liferay


liferay,liferay-6
I have created a new portal instance in liferay. I want to host it in my local Ubuntu machine. What i have done is created a WEB ID as newportal.com Virtual host as www.newportal.com and the mail domain as newportal.com thanks...

How to delete a portlet in Liferay 6.1 programmatically from code


java,javascript,java-ee,liferay,liferay-6
I'm working on Liferay 6.1 I want to delete a portlet in Liferay 6.1 from my code. What I have done so far is: <a onclick="Liferay.Portlet.close('#p_p_id_28_'); return false;">Remove</a> Above code is working fine. But it is working on the current page only i.e. it can delete the portlet(s) which is...

how to bypass the necessity of having to have email address firstname and lastname in liferay to import authenticating users


ldap,liferay
My application uses liferay to connect to LDAP server and import authenticating users. But many of the user records in the ldap schema doesn't have email id. This seems to be preventing me from importing these users as liferay requires email id, among other things, inorder to successfully import new...

Difference between Liferay Mobile SDK and Liferay Screens


mobile,liferay,liferay-6
Can someone tell me the difference between Liferay Mobile SDK and Liferay Screens? Thanks a lot....

Text internationalization in Javascript using liferay-ui:message


javascript,internationalization,liferay
How can I do internationalization for this : alert('<liferay-ui:message key="please-choose-file-type" />'); Notice that I want to internationalize the message inside Alert using Liferay:ui:message tag. Thanks....

Liferay workflow Flow(ie:Kaleo WorkFlow)


liferay
I have a requirement.We have a form where we create users in liferay and it should go through approval process(ie:single approver).If the omni admin fills that form then the user must not go through approval process and when the same form is accessed by any other user it must go...

Include ftl file in ext plugin


maven,liferay,liferay-6
I have ext-plugin and I would like to override this file: portal-impl\src\com\liferay\portlet\dynamicdatamapping\dependencies\ddm\documentlibrary.ftl. After maven build, this file isn't include in my porta-impl-ext.jar. In my portal-impl-ext's pom.xml I have this code: <build> <plugins> <plugin> <groupId>com.liferay.maven.plugins</groupId> <artifactId>liferay-maven-plugin</artifactId> <version>${liferay.maven.plugin.version}</version> <configuration>...

Customizing liferay Calendar portlet


liferay,liferay-6,liferay-ide
I'm trying to customize Calendar portlet using this below instructions. https://www.liferay.com/it/documentation/liferay-portal/6.2/development/-/ai/creating-plugins-to-extend-plugins-liferay-portal-6-2-dev-guide-03-en Its basically creating Plugins to extend plugins. In order to achieve above, I need "Calendar-portlet.war" . I don't find any war file for calendar portlet for my version. So i decided to build myself. Then i downloaded Portal plugins...

Bootstrap dropdown-menu not working in Liferay 6.2


jquery,html,css,twitter-bootstrap,liferay-6
I'm trying to do a new navbar in Liferay 6.2, but dropdown menu isn't working. I suspect AUI is guilty of this, but not sure here you got my code: <div class="navbar"> <div class="navbar-inner"> <div class="container"> .... <div class="btn-group"> <button type="button" class="btn btn-danger" data-toggle="dropdown" data-target="#demo"> simple collapsible </button> <ul class="dropdown-menu...