Sohil
Sohil

Reputation: 73

Cannot call method of a portlet using <portlet:actionURL>

I've been trying to call a method 'signinAction' of the portlet using with the help of this little tutorial Developing Portlet with Multiple Actions. But when I try to do it, I get Portlet is temporarily unavailable error. I cannot see anything in the Tomcat's server console. Also when I use processAction() I don't get the error. I don't know what is wrong.

JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:actionURL var="signinAction" name="signinAction">
</portlet:actionURL>



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>


</head>
<body onload="load()" id="body1">
<form action="<%=signinAction.toString() %>" name="signinForm" method="post" onsubmit="return signin(this)">

<center>

<div class="div-upside-down" id="div-style">    

<table  width="95%">
<tr>
    <td colspan="3"><p class="pp"></p>
    </td>
</tr>
<tr>
    <td id="td1">
        <input type="text"  id="email" name="email"  placeholder="Email" />
        <p id="one"></p>
    </td>

    <td id="td2">
        <input type="password"  id="password" name="password"  placeholder="Password" />
        <p id="one"></p>
    </td>

    <td id="td3">
        <input type= "submit" name= "submit" value="Login"/>
        <p id="one"></p>
    </td>
</tr>
</table>
</div>
</center>
</form>
</html>

Portlet:

public class HelloWorldPortlet extends GenericPortlet {

ThemeDisplay td;


 public void signinAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException{
       long companyId=10154;
       String email = actionRequest.getParameter("email");
       String password = actionRequest.getParameter("password");

       try
        {
        int authResult = 0;
        long userId = 0;
        Company company = PortalUtil.getCompany(actionRequest);
        Map headerMap = new HashMap();


        Map parameterMap = actionRequest.getParameterMap();



        authResult = UserLocalServiceUtil.authenticateByEmailAddress(company.getCompanyId(), email, password,headerMap, parameterMap, null);
        userId = UserLocalServiceUtil.getUserIdByEmailAddress(company.getCompanyId(), email);
        User user = UserLocalServiceUtil.getUserByEmailAddress(companyId, email);
        String screenId = user.getScreenName();
        td = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);  

        MethodKey key = new MethodKey("com.liferay.portlet.login.util.LoginUtil", "login", HttpServletRequest.class, HttpServletResponse.class, String.class, String.class, boolean.class, String.class);
        PortalClassInvoker.invoke(false, key, new Object[] { PortalUtil.getHttpServletRequest(actionRequest), PortalUtil.getHttpServletResponse(actionResponse),screenId, password, true, CompanyConstants.AUTH_TYPE_SN});
        actionResponse.sendRedirect(td.getPathMain());

        }
        catch (Exception e)
        {
        e.printStackTrace();
        }
        }
}

Please Help.

Upvotes: 1

Views: 1468

Answers (1)

Olaf Kock
Olaf Kock

Reputation: 48057

Some issues that you have. The first, your actual question:

It seems your portlet is inheriting from javax.portlet.GenericPortlet. This means that you'll have to add @ProcessAction(name="signinAction") to your method's signature. If you don't want this annotation, you'll need to inherit from Liferay's MVCPortlet, which finds the method by reflection as you seem to expect it

Second issue with the code you present: Your jsp contains page-level HTML, e.g. <html>, <head> and <body>: A portlet must not contain this, as it will be rendered as part of the page and it's the portal's responsibility to add these items to the page (they will be added only once, no matter how many portlets you display)

Third: As a rule of thumbs, a portlet should not have member variables - in fact, having ThemeDisplay as a portlet class member will result in random failures later: There is typically only one portlet object that handles all the requests that the application gets. You must make ThemeDisplay a local variable instead of a member to avoid concurrency issues and random user data leaking into other user's request processing

Upvotes: 5

Related Questions