MAO-EYE 2019-02-01 15:27 采纳率: 100%
浏览 40

没有Jquery的Liferay Ajax

I am trying to get json from the backend of my liferay project and can not even manage to get serveResource to print something to the console. So my ajax request does not do anything in the backend, but still calls success. I am using liferay 6.1.

I tried using jquery with a $.ajax but that was saying ajax method was undefined (even with a full download of jquery-3.2.1), so I think jquery is having issues with multiple includes or something bizarre. I am now using aui Every time I implement aui it will run the ajax function and constantly return success, even though it never ran anything on the backend and this.get("responseData") is null

view.jsp:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>

<portlet:defineObjects />
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>


<portlet:resourceURL var="saveData" id="saveData" ></portlet:resourceURL>

<br/>

<aui:form>
          <aui:input type="text" name="Name" id="studentName"/>
          <aui:input type="text" name="Email" id="studentEmail"/>
          <aui:input type="text" name="ContactNo" id="studentPhone"/>
          <aui:button type="button" name="saveButton"  value="Save" onclick="save();" />
</aui:form>
<aui:script>
function save(){
    AUI().use('aui-base','aui-io-request', function(A){

        var name=A.one("#<portlet:namespace />studentName").get('value');
        var email=A.one("#<portlet:namespace />studentEmail").get('value');
        var contact=A.one("#<portlet:namespace />studentPhone").get('value');

         A.io.request('<%=saveData%>',{
             dataType: 'json',
             method: 'POST',
             data: { <portlet:namespace/>name: name,
                      <portlet:namespace/>email: email,
                      <portlet:namespace/>contact: contact},
             on: {
             success: function() {
                 var data=this.get('responseData');
                 alert("Success");
                 // Actions to be performed on success
                 }
             }
         });
    });
}
</aui:script>

Menu.java (backend):

import java.io.IOException;
import java.io.PrintWriter;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.ProcessAction;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

/**
 * Portlet implementation class Menu
 */
public class Menu extends GenericPortlet {

    public void init() {
        viewTemplate = getInitParameter("view-template");
    }

    public void doView(
            RenderRequest renderRequest, RenderResponse renderResponse)
        throws IOException, PortletException {

        include(viewTemplate, renderRequest, renderResponse);
    }

    protected void include(
            String path, RenderRequest renderRequest,
            RenderResponse renderResponse)
        throws IOException, PortletException {

        PortletRequestDispatcher portletRequestDispatcher =
            getPortletContext().getRequestDispatcher(path);

        if (portletRequestDispatcher == null) {
            _log.error(path + " is not a valid include");
        }
        else {
            portletRequestDispatcher.include(renderRequest, renderResponse);
        }
    }

    protected String viewTemplate;


    private static Log _log = LogFactoryUtil.getLog(Menu.class);

    /**
     * Custom methods
     * @throws IOException 
     */

    @Override
    public void serveResource(ResourceRequest resourceRequest,
            ResourceResponse resourceResponse) throws IOException,
            PortletException {

        System.out.println("this will not print");
        PrintWriter out = resourceResponse.getWriter();
        out.println("This is sample Text");
    out.flush();    
    super.serveResource(resourceRequest, resourceResponse);
    }

    @ProcessAction(name="saveData")
    public void saveData(ResourceRequest resourceRequest,ResourceResponse resourceResponse){
        System.out.println("This won't print");
        String name = resourceRequest.getParameter("name");
        String emailId = resourceRequest.getParameter("email");
        String contact = resourceRequest.getParameter("contact");
     }    
}

I would like my serveResource function to return JSON to my .jsp file using ajax.

  • 写回答

1条回答 默认 最新

  • weixin_33696106 2019-02-02 22:00
    关注

    Sorry but there are many bad mistakes in your approach:

    Good luck

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?