Question

hi I am trying to Use Ajax with Spring Mvc Porltet In liferay.I Have a Jsp File and Controller Class.I want to insert a data in the form but my controller class is Not Called.So please Help Me to Solve this Problem.When I Click on submit My COntroller class is not called.

my .jsp code as follws:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>



<portlet:defineObjects/>

<portlet:renderURL var="renderOneMethodURL">
    <portlet:param name="action" value="renderOne"></portlet:param>
</portlet:renderURL>

<portlet:actionURL var="actionOneMethodURL">
<portlet:param name="action" value="actionOne"></portlet:param>
</portlet:actionURL>

<a href="${renderOneMethodURL}">Call RenderOne method</a>

<%-- <form action="${actionOneMethodURL}" method="post">
    User Name: <input type="text" name="userName">
    <input type="submit">  
</form> --%>
<div id="request">
<div id="bigForm">  
    <h2>&nbsp;REQUEST</h2>  
    <h3>&nbsp;Enter Request Details :</h3>
    <p>Name: <input name="name"> &nbsp;&nbsp;&nbsp;&nbsp;
    Code: <input name="code" size="6">&nbsp;&nbsp;&nbsp;
    Request: <input name="request"> &nbsp;&nbsp;&nbsp;
    Type: <input name="type" size="6"></p>    
          <hr></hr>   

    <div class="block2">        
        <h2>&nbsp;Attribute Value Pair(Avp)</h2>   
        <p class="remark"> Recursive structure of avp. You can nest one AVP inside another..</p>
        <div data-holder-for="branch"></div>    
     </div>    

    <div class="clear"></div>    
    <p>&nbsp;</p>       
    <p class="remark" align="right" padding-right:"1cm";>Click here to generate JSON representation of the form</p>
    <p align="right">
    <input type="submit" id="saveBigForm"></p>
</div>
<style type="text/css">a {text-decoration: none}</style>

<!-- Subforms library -->
<div style="display:none">
     <div data-name="branch">
        <table>
            <td>Name:</td> <td><input name="name"></td> 
            <td>Code:</td> <td><input name="code"></td> 
            <td>Vendor:</td> <td><input name="vendor"></td>  
            <td>Multiplicity:</td><td><input name="multiplicity"></td>  
            <td>Index:</td><td><input name="index"></td>  
            <td>CDR-Index:</td><td><input name="cdrIndex"></td> 
            <tr>
            &nbsp;&nbsp;<td >Type:</td><td><input name="type"></td>  
            <td>Value:</td><td><input name="value"></td> 
            </tr>
        </table>
        <div style="margin:10px; border-left:3px solid black" data-holder-for="branch"></div>
    </div>       
</div>    



<div id="popup"></div>
</div>

and my javascript

$('#saveBigForm').click(function(){    
        var json = $('#bigForm').jqDynaForm('get');    
        showFormJson(json);  
       // var myInfoUrl = "<c:out value="${actionOneMethodURL}" />";
        $.ajax({

            type: "POST",
             //url: "http://localhost:9090/first-spring-mvc-portlet//WEB-INF/servlet/view",
            //url : myInfoUrl,
            url : "${actionOneMethodURL}",
            data:   JSON.stringify(json),
            dataType: "json",
            success: function(response){
                // we have the response
                if(response.status == "SUCCESS"){
                    $('#info').html("Success........!Request has been added......... ");

                }else{
                    $('#info').html("Sorry, there is some thing wrong with the data provided.");
                }
            },
            error: function(e){
                alert('Error: ' + e);
            }
        });
    });

and controller class as follows

package com.myowncompany.test.springmvc.controller;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

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

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ParamUtil;

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

    private static Log log = LogFactoryUtil.getLog(MyFirstSpringMVCTestController.class);
    /*
     * maps the incoming portlet request to this method
     * Since no request parameters are specified, therefore the default
     * render method will always be this method
     */
    @RenderMapping
    public String handleRenderRequest(RenderRequest request,RenderResponse response,Model model){

        return "defaultRender";
    }

    @RenderMapping(params = "action=renderOne")
    public String openSaveSearchPopup(RenderRequest request, RenderResponse response, Model model){
        return "render1";
    }

    @RenderMapping(params = "action=renderTwo") 
    public String openDeeplinkForInfoI(RenderRequest request, RenderResponse response){
        return "render2";
    }

    @RenderMapping(params = "action=renderAfterAction") 
    public String testRenderMethod(RenderRequest request, RenderResponse response){
        log.info("In renderAfterAction method");
        return "renderAfterAction";
    }

    @ActionMapping(params = "action=actionOne") 
    public void actionOne(ActionRequest request, ActionResponse response) throws IOException {
        String userName=ParamUtil.get(request, "userName", "");
        log.info("server input stream is :::"+ request.getPortletInputStream().toString());
        System.out.println("we ri nnnnnnnnn");
        log.info("userName is==>"+userName);
        response.setRenderParameter("action", "renderAfterAction");
    }

    @ActionMapping(params = "action=actionTwo")
    public String addMyChannelAction(ActionRequest actionRequest, ActionResponse actionResponse) {
        return "action2";
    }

}

iam getting the log as follows:

09:36:56,291 INFO  [DispatcherPortlet:119] Portlet 'firstspringmvc' configured successfully
09:36:56,300 INFO  [localhost-startStop-19][PortletHotDeployListener:454] 1 portlet for first-spring-mvc-portlet is available for use
10:09:49,460 WARN  [http-bio-9090-exec-138][404_jsp:121] {msg="/$%7BactionOneMethodURL%7D", uri=/$%7BactionOneMethodURL%7D}
10:09:54,325 WARN  [http-bio-9090-exec-139][404_jsp:121] {msg="/$%7BactionOneMethodURL%7D", uri=/$%7BactionOneMethodURL%7D}
Was it helpful?

Solution 4

in .jsp page add

var actionurl =${actionOneMethodURL};

in example.js on ajax call

$.ajax({

            type: "POST",
            url : actionurl,
            data:   JSON.stringify(json),
            dataType: "json",
            success: function(response){
                // we have the response
                if(response.status == "SUCCESS"){
                    $('#info').html("Success........!Request has been added......... ");

                }else{
                    $('#info').html("Sorry, there is some thing wrong with the data provided.");
                }
            },
            error: function(e){
                alert('Error: ' + e);
            }
        });

OTHER TIPS

Try with jsp core tag,

url : "<c:url value='/VIEW/actionOne' />"

OR

url : "<c:out value='${actionOneMethodURL}' />"

Include JSTL core in JSP i.e.:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

and Change your ajax URL

url : "${actionOneMethodURL}"

to

url : "<c:url value='/VIEW/actionOne' />",

From what you have told us so far I can identify those causes:

  • your javascript has a problem and is not running at all (open with google chrome or firefox and press F12. Go to the Console tab and tell us if you have any errors there.
  • since you have a different javascript file, as the warning said, ${actionOneMethodURL}is not defined there. So you can do it like this:

In your HTML (the .jsp file you postet at top):

<script type="text/javascript">
ajaxLink = "${actionOneMethodURL}";
</script>

In your javascript file:

url : window.ajaxLink;

Experiment with this, I have no way to test it may need some tweaks like where you add the script (near the beginning of the document, near the end), and if the " surrounding the ${actionOneMethodURL} are needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top