2014-01-17 44 views
0

嗨我想使用Ajax與Spring Mvc Porltet在liferay.I有一個Jsp文件和控制器Class.I想要插入一個數據的形式,但我的控制器類沒有調用。所以請幫我解決這個問題。當我點擊提交我的COntroller類不被調用。我想在liferay.I中使用Ajax With Spring Mvc有一個Jsp文件和控制器Class.But控制器類不叫

<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%> 
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%> 
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %> 

<portlet:defineObjects/> 
<portlet:resourceURL id="actionOneMethodURL" var="actionOneMethodURL"> 
</portlet:resourceURL> 
<head> 
<script type="text/javascript"> 
function namet() 
{ 


    alert("in the function"); 
    var name = document.getElementById('<portlet:namespace/>name').value; 
    var age = document.getElementById('<portlet:namespace/>age').value; 
    var email = document.getElementById('<portlet:namespace/>email').value; 

    jQuery.ajax({ 
     url : "<%=actionOneMethodURL%>", 
     datatype : 'POST', 
     data : { 
      reqname : name, 
      reqage : age, 
      reqemail: email, 
      }, 
     success : function(response) 
      { 

      }, 
     }); 

} 
</script> 

</head> 
</html> 
<body> 
    <form method="post" id='<portlet:namespace/>fm' name='<portlet:namespace/>fm'> 
     <div class="control-group"> 
      <label class="control-label" for="name">Name:</label> 
      <div class="controls"> 
       <input name="<portlet:namespace/>name" id="<portlet:namespace/>name" class="field-required" type="text"> 
      </div> 
     </div> 
     <div class="control-group"> 
      <label class="control-label" for="Age">Age:</label> 
      <div class="controls"> 
       <input name="<portlet:namespace/>age" id="<portlet:namespace/>age" class="field-required" type="text"> 
      </div> 
     </div> 
     <div class="control-group"> 
      <label class="control-label" for="email">Email:</label> 
      <div class="controls"> 
       <input name="<portlet:namespace/>email" id="<portlet:namespace/>email" class="field-required" type="text"> 

      </div> 

     </div> 
     <input class="btn btn-info" type="button" value="save" name="action" onclick="namet()"> 
     <input class="btn btn-primary" type="reset" value="Reset"> 
    </form> 
</body> 
</html> 
Here Is my Jsp File... 

package com.hrms; 

import java.io.IOException; 

import javax.portlet.ActionRequest; 
import javax.portlet.ActionResponse; 
import javax.portlet.PortletConfig; 
import javax.portlet.RenderRequest; 
import javax.portlet.RenderResponse; 
import javax.portlet.ResourceRequest; 
import javax.portlet.ResourceResponse; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.portlet.bind.annotation.ActionMapping; 
import org.springframework.web.portlet.bind.annotation.RenderMapping; 
import org.springframework.web.portlet.bind.annotation.ResourceMapping; 

import com.hrms.model.HRMS; 
import com.hrms.service.HRMSLocalServiceUtil; 
import com.liferay.counter.service.CounterLocalServiceUtil; 
import com.liferay.portal.kernel.exception.SystemException; 
import com.liferay.portal.kernel.servlet.ServletResponseUtil; 
import com.liferay.portal.kernel.util.ParamUtil; 
import com.liferay.portal.kernel.util.StringPool; 
import com.liferay.portal.service.ServiceContext; 
import com.liferay.portal.util.PortalUtil; 

@Controller(value = "HrmsController") 
@RequestMapping("VIEW") 
public class HrmsController { 

    @RenderMapping 
    public String handleRenderRequest(RenderRequest request, 
      RenderResponse response, Model model) { 
     return "view"; 
    } 

    @ResourceMapping(value="actionOneMethodURL") 
    public void actionOneMethodURL(ResourceRequest request,ResourceResponse response) throws SystemException, IOException { 
     System.out.println("In the Method"); 
     int id = 0; 

     id = (int) CounterLocalServiceUtil.increment(HrmsController.class.getName()); 
     String reqname = ParamUtil.getString(request, "reqname"); 
     String reqage = ParamUtil.getString(request, "reqage"); 
     String reqemail = ParamUtil.getString(request, "reqemail"); 

     HRMS hrms = null; 
     hrms = HRMSLocalServiceUtil.createHRMS(id); 
     hrms.setName(reqname); 
     hrms.setAge(reqage); 
     hrms.setEmail(reqemail); 

     hrms = HRMSLocalServiceUtil.updateHRMS(hrms); 

     HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(response); 
     ServletResponseUtil.write(httpServletResponse, response.toString()); 
    } 

} 

and here is my Controller Class... 

回答

0
jQuery.ajax({ 
     url : "<%=actionOneMethodURL%>", 
     **datatype : 'POST',** 
     data : { 
      reqname : name, 
      reqage : age, 
      reqemail: email, 
      }, 
     success : function(response) 
      { 

      }, 
     }); 

您的代碼上述[標記黑體]似乎是錯誤的。 數據類型定義「您希望從服務器返回的數據的類型」。 類型屬性定義了用於Ajax調用的方法GET或POST。

請進行更改。 還要確保您在Controller中使用@ResourceMapping批註處理ajax資源url。

+0

我做到這一點的變化,但仍然沒有工作.. \t jQuery.ajax({ \t \t URL:「<%= actionOneMethodURL.toString ()%>」, \t \t類型: 'POST', \t \t數據:{ \t \t \t名:姓名, \t \t \t年齡:年齡, \t \t \t電子郵件:電子郵件, \t \t \t}, \t \t}); } –

+0

你有控制器中的任何方法註解@ResourceMapping? –

+0

是的,我描述了我的控制器。 –

0

更改數據類型: 'POST'類型: 'GET'

AJAX的POST被處理的processAction。

因此改變你的 '型',從 'POST' 到 'GET'