如何调用portlet.java类的具体方法,而不是在此改变的serveResource方法? [英] How to call specific method of portlet.java class rather then overide serveResource method?

查看:146
本文介绍了如何调用portlet.java类的具体方法,而不是在此改变的serveResource方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Liferay中与阿贾克斯一定的帮助。 现在,我打电话从AJAX方法我 view.jsp的页提交一些数据。

下面是一个示例code我正在使用 view.jsp的

 <%@ include文件=/ init.jsp%>

<的portlet:actionURL名=AddTestVAR =ADD1/>
<的portlet:resourceURL ID =AddTestVAR =AddTest>< / portlet中:resourceURL>

< HTML>
< HEAD>
    <脚本类型=文/ JavaScript的SRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js>< / SCRIPT>

    <脚本类型=文/ JavaScript的>
    功能addToDo(addToDo){
        VAR待办事项=的document.getElementById('TODO')值。
        $阿贾克斯({
            网址:addToDo,
              数据:{待办事项:待办事项,CMD:addToDo},
              键入:GET,
              数据类型:文本,
            成功:功能(数据){
                  $(#TodoList的)HTML(数据);
            }
        });
    }
    < / SCRIPT>
< /头>

<身体GT;

    < portlet中:resourceURL VAR =addToDoID =addToDo>< / portlet中:resourceURL>

    <形式GT;
        <输入类型=文本名称=待办事项ID =待办事项>
        <按钮名称=添加类型=按钮的onclick =addToDo('<%= addToDo%>')>添加< /按钮>
        < D​​IV ID =TodoList的>

        < / DIV>
    < /形式GT;
< /身体GT;
< / HTML>
 

和我的 portlet.java 类有它叫这Ajax调用一个方法:

  @覆盖
公共无效的serveResource(ResourceRequest要求,ResourceResponse响应){
    如果(的request.getParameter(CMD)。等于(addToDo)){

        的System.out.println(来到这里加);

        的MediaType userToDo =新mediatypeImpl();
        //userToDo.setMediaId(12345);
        尝试 {
            userToDo.setPrimaryKey((CounterLocalServiceUtil.increment()));
            userToDo.setMedianame(的request.getParameter(待办事项));
            mediatypeLocalServiceUtil.addmediatype(userToDo);
        }
        赶上(SystemException的E){
            e.printStackTrace();
        }
    }
}
 

所以我的问题是,从任何AJAX类,现在它只是在默认情况下caling @覆盖方法。 但是,我怎么能叫 portlet.java的具体方法在Ajax调用类?

我在阿贾克斯新的蜜蜂。所以,请指导我反正U可以......

我得到的网址如下的AJAX调用时,下面的错误

 <的portlet:actionURL名=ajax_AddAdvertiseVAR =addToDoWINDOWSTATE =<%= LiferayWindowState.EXCLUSIVE.toString()%>> < / portlet中:actionURL>



06:47:03705错误[HTTP-BIO-8080-EXEC-23] [render_portlet_jsp:154] java.lang.NoSuchMethodException:emenu.advertise.portlet.RestaurantPortlet.ajax_AddAdvertise(javax.portlet.ActionRequest,javax.portlet.ActionResponse)
    在java.lang.Class.getMethod(Class.java:1605)
 

我的过程中操作方法如下:

  @ProcessAction(NAME =ajax_AddAdvertise)
    公共无效ajax_AddAdvertise(ResourceRequest要求,ResourceResponse响应){

}
 

解决方案
  

我怎么能叫portlet.java类的具体方法对Ajax调用?

我想我们不能有两个不同版本的的serveResource 的方法,例如,我们的行动方法做ATLEAST不使用默认的实现。

如果你想不同的方法,你就必须去的 Spring MVC的( @ResourceMapping 的方式有。

不过,你可以在你的的serveResource 使用方法定义不同的逻辑 RESOURCEID 如下(的一个完整的例子):

在JSP:

 < portlet中:resourceURL VAR =myResourceURLID =myResourceID01/>
 

在portlet类的的serveResource 方法将包含下列code:

 字符串RESOURCEID = request.getResourceID();

如果(resoureID.equals(myResourceID01)){
   //做myResourceID01特定的逻辑
} 其他 {
   //别人做你想做的
}
 

请记住[重要]
在portlet中,你不应该使用< HTML> < HEAD> &LT ;身体gt;由于Portlet的标签生成页面的片段,而不是整个页面。即使是允许你的结果页面将不能很好地形成,将不同的表现在不同的浏览器。而且它修改DOM元素的JavaScript将是完全无用的。

修改后的<一个href="http://stackoverflow.com/questions/13246870/how-to-call-specific-method-of-portlet-java-class-rather-then-overide-serveresou/13251286#comment18077578_13251286">this评论:
您还可以使用Ajax和操作方法:

人们使用&LT; portlet中:actionURL&GT; 与阿贾克斯一般为&LT;形式GT; - POST

有关此 actionURL 在一个稍微不同的方式产生在你的JSP是这样的:

 &LT;的portlet:actionURL名=ajax_AddAdvertiseVAR =addToDoWINDOWSTATE =&LT;%= LiferayWindowState.EXCLUSIVE.toString()%&GT;&GT;
&LT; / portlet中:actionURL&GT;
 

而在你的portlet中,你可以有(如题):

  @ProcessAction(NAME =ajax_AddAdvertise)
公共无效ajax_AddAdvertise(ActionRequest里要求,ActionResponse响应){
// ...您的code
}
 

I want some help in liferay with ajax. Right now I am calling the ajax method from my view.jsp page to submit some data.

Here is sample code I am using in view.jsp:

<%@ include file="/init.jsp"%>

<portlet:actionURL name="AddTest" var="add1" />
<portlet:resourceURL id="AddTest" var="AddTest"></portlet:resourceURL>

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

    <script type="text/javascript">
    function addToDo(addToDo){
        var todo =document.getElementById('toDo').value;
        $.ajax({
            url :addToDo,            
              data: {"todo":todo,"CMD":"addToDo"},
              type: "GET",
              dataType: "text",
            success: function(data) {              
                  $("#toDoList").html(data);
            }
        });
    }
    </script>
</head>

<body>

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

    <form>
        <input type="text" name="toDo" id="toDo">
        <button name="Add" type="button" onclick="addToDo('<%=addToDo%>')">Add</button>
        <div id="toDoList">

        </div>
    </form>
</body>
</html> 

and in my portlet.java class there is one method which is called by this ajax call:

@Override
public void serveResource(ResourceRequest request, ResourceResponse response){
    if(request.getParameter("CMD").equals("addToDo")) {

        System.out.println("came here for add");

        mediatype userToDo = new mediatypeImpl();
        //userToDo.setMediaId(12345);
        try {
            userToDo.setPrimaryKey((CounterLocalServiceUtil.increment()));
            userToDo.setMedianame(request.getParameter("todo"));
            mediatypeLocalServiceUtil.addmediatype(userToDo);
        }
        catch (SystemException e) {
            e.printStackTrace();
        }
    }
}

So my question is that right now it is just caling by default @override method from any ajax class. But how can I call specific method of portlet.java class on ajax call?

I am new bee in ajax. So please guide me anyways u can.....

I got following error when calling ajax with url of following

<portlet:actionURL name="ajax_AddAdvertise" var="addToDo" windowState="<%= LiferayWindowState.EXCLUSIVE.toString()%>"> </portlet:actionURL>



06:47:03,705 ERROR [http-bio-8080-exec-23][render_portlet_jsp:154] java.lang.NoSuchMethodException: emenu.advertise.portlet.RestaurantPortlet.ajax_AddAdvertise(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
    at java.lang.Class.getMethod(Class.java:1605)

my process action method as follows

 @ProcessAction(name = "ajax_AddAdvertise")
    public void ajax_AddAdvertise(ResourceRequest request,ResourceResponse response) {

}

解决方案

how can I call specific method of portlet.java class on ajax call?

I think we can't have two different versions of serveResource methods like we do for action methods atleast not with the default implementation.

If you want different methods you would have to go the Spring MVC (@ResourceMapping) way to have that.

Still, you can define different logic in your serveResource method using the resourceId as follows (a full example):

In the JSP:

<portlet:resourceURL var="myResourceURL" id="myResourceID01" />

In the portlet class the serveResource method will contain the following code:

String resourceID = request.getResourceID();

if(resoureID.equals("myResourceID01")) {
   // do myResourceID01 specific logic
} else {
   // else do whatever you want
}

Please keep in mind [Important]
In a portlet you should not use <html>, <head>, <body> tags since portlets generate fragment of the page and not the whole page. Even if it is allowed your resultant page will not be well-formed and will behave differently on different browsers. And moreover the javascript which modifies DOM element will be totally useless.

Edit after this comment:
You can also use ajax with action methods:

People use <portlet:actionURL> with ajax generally for <form>-POST.

For this the actionURL is generated in a slightly different way in your jsp like this:

<portlet:actionURL name="ajax_AddAdvertise" var="addToDo" windowState="<%= LiferayWindowState.EXCLUSIVE.toString()%>">
</portlet:actionURL>

And in your portlet you can have (as in the question):

@ProcessAction(name = "ajax_AddAdvertise")
public void ajax_AddAdvertise(ActionRequest request, ActionResponse response) {
// ... your code
}

这篇关于如何调用portlet.java类的具体方法,而不是在此改变的serveResource方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆