1
votes

Hi i try do portlet from liferay documentation. But my java class dont work.

MyGreetingPortlet

package com.liferay.samples;  
import java.io.IOException; 
import javax.portlet.ActionRequest; 
import javax.portlet.ActionResponse; 
import javax.portlet.PortletException; 
import javax.portlet.PortletPreferences; 
import com.liferay.util.bridges.mvc.MVCPortlet;  
public class MyGreetingPortlet extends MVCPortlet { @Override 
public void processAction( ActionRequest actionRequest, ActionResponse actionResponse) 
        throws IOException, PortletException { 
    PortletPreferences prefs = actionRequest.getPreferences(); 
    String greeting = actionRequest.getParameter("greeting");  
    if (greeting != null) { 
        prefs.setValue("greeting", greeting); 
        prefs.store();
        }  
    super.processAction(actionRequest, actionResponse); 
    } 
}

view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> 
<%@ page import="javax.portlet.PortletPreferences" %> 
<portlet:defineObjects />
<% PortletPreferences prefs = renderRequest.getPreferences();
String greeting = (String)prefs.getValue( "greeting", "Hello! Welcome to our portal."); %>
<p><%= greeting %></p>
<portlet:renderURL var="editGreetingURL"> <portlet:param name="jspPage" value="/edit.jsp" /> 
</portlet:renderURL> 
<p><a href="<%= editGreetingURL %>">Edit greeting</a></p> 

edit.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> 
<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %> 
<%@ page import="com.liferay.portal.kernel.util.Validator" %> 
<%@ page import="javax.portlet.PortletPreferences" %>  
<portlet:defineObjects />  
<% PortletPreferences prefs = renderRequest.getPreferences(); 
String greeting = (String)prefs.getValue( "greeting", "Hello! Welcome to our portal."); %>  
<portlet:actionURL var="editGreetingURL"> <portlet:param name="jspPage" value="/edit.jsp" /> 
</portlet:actionURL>  
<form action="<%= editGreetingURL %>" method="post"> 
<label><input type="text"><%= greeting %></label>
<input type="submit" name="submit" value="Submit">
</form>  
<portlet:renderURL var="viewGreetingURL"> 
<portlet:param name="jspPage" value="/view.jsp" /> 
</portlet:renderURL>  
<p><a href="<%= viewGreetingURL %>">&larr; Back</a></p>

After deploy jsp works fine, but when i click on Submit button nothing heppens. I change only one thing in this example: delete aui tags but dont think that chage something, couse im already try a few diffetent examples and no one works with java classes. Oh, and i use MVC portlets.

3

3 Answers

2
votes

I think your portlet-class in portlet.xml is not pointing to the custom portlet class of yours and is still referring MVCPortlet. Please change that to point your custom portlet class

---Update---

After checking your code in my local found that the way you are passing parameter is incorrect. You are not passing the name of the parameter. You should pass the parameter like shown below.

<input type="text" name="<portlet:namespace/>greeting"><%= greeting %></input>
2
votes

The file portlet.xml also needs to be changed so that it points to our new class

 <portlet>
    <portlet-name>my-greeting</portlet-name>
    <display-name>My Greeting</display-name>
    <portlet-class>com.liferay.samples.MyGreetingPortlet</portlet-
    class>
    <init-param>
    <name>view-jsp</name>
    <value>/view.jsp</value>
    </init-param>

In edit.jsp

<%
    PortletPreferences prefs = renderRequest.getPreferences();
    String greeting = (String) prefs.getValue("greeting",
            "Hello! Welcome to our portal.");
%>

After this add the below code

<liferay-ui:success key="success" message="Greeting saved successfully!" />

Regenerate the war file you will see the output. Still, if you face the problem, I will provide the complete code

0
votes

I guess it's not necessary to call

super.processAction(actionRequest, actionResponse); 

remove it and try again, then it might work.