2017-06-28 12 views
0

ajax呼び出しでserveResource()メソッドを呼び出そうとしていますが、呼び出すことができません。私のコードを見てください。Liferayで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に新しいです。これで私を助けてください。

答えて

0

原因は、これらの行である:

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

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

最初の行はJSPスコープにJavaスクリプト変数を定義します。 <%=ajaxURL%>と参照して使用することができますので、コード内の1行を置き換えてください:

ajaxRequest.open("GET","<%=ajaxURL%>",true); 
関連する問題