无法在Liferay中调用serveResource()方法

时间:2017-06-28 06:52:42

标签: java liferay-6 liferay-ide

我试图通过ajax调用调用serveResource()方法但不能调用它。看我的代码。

我的JSP文件

<%@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>

我的控制器文件:

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");
        }
}

我是liferay的新手,所以对此并不了解。请帮帮我。

1 个答案:

答案 0 :(得分:0)

罪魁祸首在于:

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

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

第一行定义JSP范围中的Java脚本变量。您可以通过将其引用为<%=ajaxURL%>来使用它,因此只需在代码中替换一行:

ajaxRequest.open("GET","<%=ajaxURL%>",true);