Reputation: 279
Below is my View.jsp code
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<portlet:actionURL name="myAction" var="myAction">
</portlet:actionURL>
<form action="${myAction}" method="POST">
Name : <input type="text" name="name">
<input type="button" value="SUBMIT">
</form>
Below is my Portlet class code
package com.generic;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
/**
* Portlet implementation class FirstGenericDemo
*/
public class FirstGenericDemo extends GenericPortlet {
public void init() {
viewTemplate = getInitParameter("view-template");
}
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)throws IOException, PortletException {
System.out.println("view");
include(viewTemplate, renderRequest, renderResponse);
}
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);
}
}
protected String viewTemplate;
private static Log _log = LogFactoryUtil.getLog(FirstGenericDemo.class);
@Override
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
System.out.println("ok");
String name=request.getParameter("name");
System.out.println("name is : "+name);
super.processAction(request, response);
}
}
When the portlet is rendered view method gets called but when i click on submit button neither processAction method gets called nor view method. Also these is no error in stacktrace. I did tried it by deploying several times but the issue is same. can anyone please help on this. ?
Upvotes: 2
Views: 5433
Reputation: 76
As said by Miroslav Ligas there are several issues.To remove those issues you need to do changes in view.jsp as well as your Portlet class.
a) In your view.jsp you need to use the below code snippet
<portlet:actionURL name="myAction" var="myAction">
</portlet:actionURL>
<form action="${myAction}" method="POST">
Name : <input type="text" name="name">
<input type="submit" value="SUBMIT">
</form>
If you are using the input type="button" then it will not work till you use document.getElementById("myForm").submit(); in your javascript part.
b) In your Portlet Class
1) You need to remove the super.processAction(request, response); as it will generate exception.
2) You need to add below code snippet in your ProcessAction method
response.setRenderParameter("jspPage","/html/jsps/displayEmployees.jsp");
above code snippet is use so as to redirect to some view part(jsp page) where /html/jsps/displayEmployees.jsp represents the path to jsp which you want to use it as view part.
and changes suggested by me earlier like using annotation @ProcessAction(name=myAction) or changing name of action method will not be recommended as you are extending the GenericPortlet class rahter then MVCPortlet class.If you extend MVCPortlet Class then it's mandatory to use them.
Changes like adding
<portlet:namespace/>
in html tags like Name : <input type="text" name="<portlet:namespace/>name">
are optional to use in your code snippet.
Upvotes: 0
Reputation: 1307
The code has several issues.
The renaming suggested by Ajay will not work as you are using the GenericPortlet as the parent.
To properly bind the jsp/html code with portlet class you need to rename the class and annotate it as suggested by Ajay.
@ProcessAction(name=myAction)
public void myAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
//your code
}
Another option is to witch to MVCPortlet as a base class. Then the renaming will be enough (name attribute needs to match method name).
The second thing that will not work are the parameters. If you are not using the aui tags you need to add the namespace to the input names. In JAVA code you refer to the parameters just by the name (without namespace)
Name : <input type="text" name="<portlet:namespace/>name">
I suggest using the AUI tags. It's easier.
Upvotes: 2