Ajax调用没有击中控制器

时间:2014-05-14 10:11:50

标签: java javascript ajax spring-mvc liferay-6

嗨我正在尝试使用Ajax与Spring Mvc Porltet在liferay.I有一个Jsp文件和控制器类。我想在表单中插入数据,但我的控制器类是不调用的。所以请帮助我解决这个问题问题。当我点击提交时,我的COntroller类没有被调用。

我的.jsp代码如下:

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

和我的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);
            }
        });
    });

和控制器类如下

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

}

我按如下方式获取日志:

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}

4 个答案:

答案 0 :(得分:0)

在JSP中包含JSTL核心,即:

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

并更改您的ajax网址

url : "${actionOneMethodURL}"

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

答案 1 :(得分:0)

尝试使用jsp核心标记,

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

OR

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

答案 2 :(得分:0)

到目前为止,根据你告诉我们的情况,我可以找出原因:

  • 您的javascript出现问题且根本没有运行(使用谷歌浏览器或Firefox启动并按F12。转到控制台标签,告诉我们您是否有任何错误。
  • 因为你有一个不同的javascript文件,正如警告所说,${actionOneMethodURL}没有在那里定义。所以你可以这样做:

在你的HTML(你顶部的.jsp文件)中:

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

在您的javascript文件中:

url : window.ajaxLink;

试验一下,我无法测试它可能需要一些调整,例如你添加script的位置(靠近文档的开头,接近结尾),以及周围的"需要${actionOneMethodURL}

答案 3 :(得分:-1)

在.jsp页面添加

var actionurl =${actionOneMethodURL};
在ajax调用的example.js中

$.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);
            }
        });