0

I am trying to call serveResource() method through an ajax call but not able to call it. See my code.

My JSP file

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

<portlet:actionURL var="addEmployeeActionURL" windowState="normal" name="addEmployee">
</portlet:actionURL>

<portlet:resourceURL var="ajaxURL">
    <portlet:param name="jsp" value="raman_data" />
</portlet:resourceURL>

<script type="text/javascript">
    function ajaxFunction(){
           var ajaxRequest;  // The variable that makes Ajax possible!
           try{

              // Opera 8.0+, Firefox, Safari
              ajaxRequest = new XMLHttpRequest();
              alert("successful 1");
           }catch (e){

              // Internet Explorer Browsers
              try{
                 ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
              }catch (e) {

                 try{
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                 }catch (e){

                    // Something went wrong
                    alert("Your browser broke!");
                    return false;
                 }
              }
           }
    }

    alert("reached here");
    ajaxRequest.open("GET","ajaxURL",true);
</script>



<h1>Enter Employee Details</h1>

<form action="<%=addEmployeeActionURL%>" name="employeeForm"  method="POST">

<b>ID</b> <br/>
<input  type="text" name="<portlet:namespace/>eid" id="<portlet:namespace/>eid" onchange="ajaxFunction()"/><br/>

<b>Name</b><br/>
<input  type="text" name="<portlet:namespace/>ename" id="<portlet:namespace/>ename"/><br/>

<b>Age</b><br/>
<input type="text" name="<portlet:namespace/>eage" id="<portlet:namespace/>eage"/><br/>

<b>Salary</b><br/>
<input type="text" name="<portlet:namespace/>esal" id="<portlet:namespace/>esal"/><br/>

<b>Deptno</b><br/>
<input type="text" name="<portlet:namespace/>deptno" id="<portlet:namespace/>deptno"/><br/>

<b>Email</b><br/>
<input type="text" name="<portlet:namespace/>email" id="<portlet:namespace/>email"/><br/>

<input type="submit" name="addEmployee" id="addEmployee" value="Add Employee"/>
</form>

My Controller file:

package html;

import hibernate.Employee;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class EmployeeForm extends MVCPortlet{

    @Override
    public void doView(RenderRequest renderRequest,
            RenderResponse renderResponse) throws IOException, PortletException {

        PortletRequestDispatcher  dispatcher = getPortletContext().getRequestDispatcher(getInitParameter("raman")); 
        include(renderRequest, renderResponse, dispatcher);

    }



        public void include(final PortletRequest request, final PortletResponse response,
                    final PortletRequestDispatcher portletRequestDispatcher) throws PortletException {
                if (portletRequestDispatcher != null) {
                    try {
                        portletRequestDispatcher.include(request, response);
                    } catch (IOException ioException) {
                        //LOGGER.error("Error while dispaching request", ioException);
                        throw new PortletException();
                    }
                }
            } 


        @Override
        public void serveResource(ResourceRequest resourceRequest,
                ResourceResponse resourceResponse) throws IOException,
                PortletException {
            System.out.println("AJAX call successful");
        }
}

I am new to liferay so doesn't know much about it. Please help me with this.

raman0369
  • 59
  • 3

1 Answers1

0

The culprit is in these lines:

<portlet:resourceURL var="ajaxURL">
    <portlet:param name="jsp" value="raman_data" />
</portlet:resourceURL>

ajaxRequest.open("GET","ajaxURL",true);

The first lines define a Java scripting variable in the JSP scope. You can use it by referencing it as <%=ajaxURL%>, so just replace one line in your code:

ajaxRequest.open("GET","<%=ajaxURL%>",true);
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90