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.
There are certain mistakes in your code:
You forgot to include tag reference for aui in your JSP.
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.
<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 {
Replace all resourceRequest
with actionRequest
UploadRequest
should be UploadPortletRequest
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();
}
}
}