Vista combinada Visión Plana Vista de árbol
Discusiones [ Anterior | Siguiente ]
toggle
Koen Cleynhens
problem in Ajax call
6 de mayo de 2010 8:08
Respuesta

Koen Cleynhens

Ranking: Junior Member

Mensajes: 83

Fecha de incorporación: 23 de febrero de 2010

Mensajes recientes

Hello I developed a portlet to run my current J2EE web app.

In this web app there is included a function to refresh data with Ajax call.

And the point is, that it is working on the first request, but a second request does not worked anymore.

Can please someone look at this problem?

I have to do the tricky thing with exclusive, because other wise my answer for the first request was contain a lot of other data...

The code is:

<script type="text/javascript">
//<![CDATA[

var xmlHttp;

function init() {
}

function refreshCarMakes() {
xmlHttp = null;
if (window.XMLHttpRequest) xmlHttp = new XMLHttpRequest(); else if (window.ActiveXObject) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e1) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e2) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e3) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e4) {}}}}}

if (xmlHttp != null) {
<% PortletURL actionURL = renderResponse.createActionURL();
actionURL.setWindowState(LiferayWindowState.EXCLUSIVE);
%>
var url = "<%=actionURL%>";

xmlHttp.onreadystatechange = loadCarMakes; xmlHttp.open("POST", url, true); xmlHttp.send(null);
}
}


function loadCarMakes() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {

var makes = xmlHttp.responseText.split("\n");


for (i = 1 ; i < makes.length ; i++) {
if (makes != "" && makes != "carTypesList_") {
var newOption = document.createElement('option');
newOption.value = makes;
newOption.text = makes;

try {
carMakeSelect.add(newOption, null);
} catch (ex) {
carMakeSelect.add(newOption);
}
}
else if (makes == "carTypesList_")
break;
}
}
}



//]]>
</script>
Tomas Polesovsky
RE: problem in Ajax call
6 de mayo de 2010 12:03
Respuesta

Tomas Polesovsky

LIFERAY STAFF

Ranking: Liferay Master

Mensajes: 590

Fecha de incorporación: 13 de febrero de 2009

Mensajes recientes

Hello Koen,

it seems very complex. Is it really the best way?

1, Why didn't you use the jQuery or some other framework for the ajax calls? You don't need to write it from scratch and it works out of the box.
2, Why do you use the ActionURL instead of ResourceURL?

-- tom
Victor Zorin
RE: problem in Ajax call
6 de mayo de 2010 17:16
Respuesta

Victor Zorin

Ranking: Liferay Legend

Mensajes: 1176

Fecha de incorporación: 14 de abril de 2008

Mensajes recientes

Browser caching may prevent your ajax calls going back to your portal, you may check the other thread too:
http://www.liferay.com/community/forums/-/message_boards/message/4381676
Koen Cleynhens
RE: problem in Ajax call
7 de mayo de 2010 0:01
Respuesta

Koen Cleynhens

Ranking: Junior Member

Mensajes: 83

Fecha de incorporación: 23 de febrero de 2010

Mensajes recientes

@Tomas

1. it is my intention to test it with the standard way of working in Liferay. But It is an application that is working now standalone, so my fist approach is/was to modify as least as possible...
=> are there some simple example to start from?

2. Good question: I guess I toke the ActionURL because for the other request I use the <form action="<portlet:actionURL... approach.
=> What is the difference?
Tomas Polesovsky
RE: problem in Ajax call
7 de mayo de 2010 2:08
Respuesta

Tomas Polesovsky

LIFERAY STAFF

Ranking: Liferay Master

Mensajes: 590

Fecha de incorporación: 13 de febrero de 2009

Mensajes recientes

ad 1, I use the jQuery (which is bundled with Liferay), you can bundle it in your standalone application too. http://api.jquery.com/jQuery.post/

I've added an example, it is just an idea how to implement it, I haven't try to compile it or run emoticon


ad 2, Serving resources mechanism is for sending html snippets (just serveResource method is invoked), action phase is for changing the portlet state (processAction and doView/Edit/Help are invoked). For more info see the JSR-286 specification.


Portlet JSP Example:
 1<script type="text/javascript">
 2    function  <portlet:namespace />refreshCarMakes(){
 3        // url to the resource myCars
 4        var carsUrl = '<portlet:resourceURL id="myCars" />';
 5
 6        // send AJAX call using POST
 7        jQuery.post(carsUrl, function(data) {
 8            // create new content from the data
 9            var buffer = "";
10            for(i = 0; i < data.cars.length; i++){
11                var car = data.cars[ i ];
12                buffer += "<option value="+car.id+">";
13                buffer += car.name + " wheels: " + car.wheels;
14                buffer += "</option>";
15            }
16            // replace content
17            jQuery("<portlet:namespace />selectedCarOption").html(buffer);
18        }, "json");
19    }
20</script>
21
22<input type="button" value="Load cars" onclick="<portlet:namespace />refreshCarMakes()" />
23<select name="selectedCar" id="<portlet:namespace />selectedCarOption" />


Portlet Example:
 1public class MyPortlet extends GenericPortlet {
 2...   
 3    @Override
 4    public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
 5      
 6        response.setContentType("application/json");
 7        String resourceId = request.getResourceID();
 8       
 9        if (resourceId.equals("myCars")) {
10            PrintWriter writer = response.getWriter();
11            writer.println("{");
12            writer.println("  'cars': [");
13            writer.println("    {'id':1,'name':'myCar','wheels'=4},");
14            writer.println("    {'id':2,'name':'myCar','wheels'=3},");
15            writer.println("    {'id':3,'name':'myCar','wheels'=2},");
16            writer.println("    {'id':4,'name':'myCar','wheels'=1}");
17            writer.println("  ]");
18            writer.println("}");
19        }
20    }
21...
22}


-- tom
Koen Cleynhens
RE: problem in Ajax call
7 de mayo de 2010 7:23
Respuesta

Koen Cleynhens

Ranking: Junior Member

Mensajes: 83

Fecha de incorporación: 23 de febrero de 2010

Mensajes recientes

@Tomas

Thanks for the info.

I'm already one step further. I guess the solution will be in the fact to use resourceUrl.

But the problem now is that in my custom portlet I have some stuff that I need in the doView method.

So now I'm looking to have a way of working to have a call in the next method:

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

doView((RenderRequest)resourceRequest, (RenderResponse)resourceResponse);

}

=> But of course this will give some casting exception. Is there a way that I can just call the doView method from inside the serveResource method?
Koen Cleynhens
RE: problem in Ajax call
7 de mayo de 2010 7:53
Respuesta

Koen Cleynhens

Ranking: Junior Member

Mensajes: 83

Fecha de incorporación: 23 de febrero de 2010

Mensajes recientes

@Tomas:

Just to say,forget previous reply.

I just refactored my doView method so that I call another method with the PortletRequest and PortletResponse object.

No I can call this method from my serveResource method..


Thanks a lot for your help.

Maybe you know also what the problem can be for another problem for me.

I make in a action a dynamic pdf with FOP , and open it in another window. But the browser shows only the pdf code itself and not with acrobat or so...
Tomas Polesovsky
RE: problem in Ajax call
7 de mayo de 2010 8:48
Respuesta

Tomas Polesovsky

LIFERAY STAFF

Ranking: Liferay Master

Mensajes: 590

Fecha de incorporación: 13 de febrero de 2009

Mensajes recientes

Koen,

do you set correct mime type in the response?

For example:
1response.setContentType("application/pdf");


How do you generate your PDF, using serve resource or servlet?

-- tom
Koen Cleynhens
RE: problem in Ajax call
13 de mayo de 2010 11:25
Respuesta

Koen Cleynhens

Ranking: Junior Member

Mensajes: 83

Fecha de incorporación: 23 de febrero de 2010

Mensajes recientes

@Tomas:

Other point about Ajax. I just changed my portlet behaviour. The page with Ajax call's was first the first page in the portlet. Now I put another action before this action/page.

The problem now is that the resource URL is contain the parameters from the action handling before...

Can I do a kind of clean on the resource URL?
Tomas Polesovsky
RE: problem in Ajax call
14 de mayo de 2010 11:29
Respuesta

Tomas Polesovsky

LIFERAY STAFF

Ranking: Liferay Master

Mensajes: 590

Fecha de incorporación: 13 de febrero de 2009

Mensajes recientes

Hi,


The page with Ajax call's was first the first page in the portlet. Now I put another action before this action/page.

I am not sure I understand it.

The problem now is that the resource URL is contain the parameters from the action handling before...


Maybe try to look at the ticket LPS-3022. Is it the same behaviour as yours?

-- tom
Koen Cleynhens
RE: problem in Ajax call
17 de mayo de 2010 6:05
Respuesta

Koen Cleynhens

Ranking: Junior Member

Mensajes: 83

Fecha de incorporación: 23 de febrero de 2010

Mensajes recientes

YEp, the same behaviour.

But what now?

Is there a work around without modify the code?
Tomas Polesovsky
RE: problem in Ajax call
17 de mayo de 2010 12:19
Respuesta

Tomas Polesovsky

LIFERAY STAFF

Ranking: Liferay Master

Mensajes: 590

Fecha de incorporación: 13 de febrero de 2009

Mensajes recientes

If I understand it well, you should set url.setCopyCurrentRenderParameters(false) before rendering the URL.

Try something like this, it could work:
 1
 2<portlet:resourceURL id="myResource" var="myResourceURL">
 3   <portlet:param name="myParam" value="myValue" />
 4   ...
 5</portlet:resourceURL>
 6<%
 7  myResourceURL.setCopyCurrentRenderParameters(false);
 8  out.println(myResourceURL.toString());
 9%>


-- tom
Koen Cleynhens
RE: problem in Ajax call
18 de mayo de 2010 6:46
Respuesta

Koen Cleynhens

Ranking: Junior Member

Mensajes: 83

Fecha de incorporación: 23 de febrero de 2010

Mensajes recientes

Thanks,

it works now.

I just add next lines in my JSP:

<%
PortletURLImpl responseURL = (PortletURLImpl)renderResponse.createResourceURL();
responseURL.setCopyCurrentRenderParameters(false);

%>

I know it is not the most clean way, but I don't know another solution for the moment...
Tomas Polesovsky
RE: problem in Ajax call
18 de mayo de 2010 8:12
Respuesta

Tomas Polesovsky

LIFERAY STAFF

Ranking: Liferay Master

Mensajes: 590

Fecha de incorporación: 13 de febrero de 2009

Mensajes recientes

OK, good to know emoticon

I'll append it as a workround to the LPS-3022

-- tom
Manikandan S
RE: problem in Ajax call
15 de febrero de 2011 5:39
Respuesta

Manikandan S

Ranking: Expert

Mensajes: 492

Fecha de incorporación: 15 de septiembre de 2010

Mensajes recientes

Tomas Polesovsky:
If I understand it well, you should set url.setCopyCurrentRenderParameters(false) before rendering the URL.

Try something like this, it could work:
 1
 2<portlet:resourceURL id="myResource" var="myResourceURL">
 3   <portlet:param name="myParam" value="myValue" />
 4   ...
 5</portlet:resourceURL>
 6<%
 7  myResourceURL.setCopyCurrentRenderParameters(false);
 8  out.println(myResourceURL.toString());
 9%>


-- tom

I am also getting the same problem
I tried what you mentioned but I could solve
Throwing error : The method setCopyCurrentRenderParameters(boolean) is undefined for the type String....
any other solution?
Tomas Polesovsky
RE: problem in Ajax call
16 de febrero de 2011 6:34
Respuesta

Tomas Polesovsky

LIFERAY STAFF

Ranking: Liferay Master

Mensajes: 590

Fecha de incorporación: 13 de febrero de 2009

Mensajes recientes

Mani,

that was my fault, var="myResourceURL" creates String object, not ResourceURL object. You have to use it the way Koen mentioned:

<%
PortletURLImpl responseURL = (PortletURLImpl)renderResponse.createResourceURL();
responseURL.setCopyCurrentRenderParameters(false);

%>


-- tom
Manikandan S
RE: problem in Ajax call
16 de febrero de 2011 20:20
Respuesta

Manikandan S

Ranking: Expert

Mensajes: 492

Fecha de incorporación: 15 de septiembre de 2010

Mensajes recientes

<%
PortletURLImpl responseURL = (PortletURLImpl)renderResponse.createResourceURL();
responseURL.setCopyCurrentRenderParameters(false);

%>

Hi Tom,

I tried the above coding in my jsp page but its throwing this error after click the action:

ERROR [PortletRequestDispatcherImpl:107] org.apache.jasper.JasperException: An exception occurred processing JSP page
Tomas Polesovsky
RE: problem in Ajax call
17 de febrero de 2011 3:06
Respuesta

Tomas Polesovsky

LIFERAY STAFF

Ranking: Liferay Master

Mensajes: 590

Fecha de incorporación: 13 de febrero de 2009

Mensajes recientes

Hi,

the error is not root cause. It just says that there was an error in JSP. Do you have root cause?

-- tom
Manikandan S
RE: problem in Ajax call
23 de febrero de 2011 23:30
Respuesta

Manikandan S

Ranking: Expert

Mensajes: 492

Fecha de incorporación: 15 de septiembre de 2010

Mensajes recientes

Tomas Polesovsky:
Hi,

the error is not root cause. It just says that there was an error in JSP. Do you have root cause?

-- tom


Hi tom,

root cause means? what you are mentioning?
Tomas Polesovsky
RE: problem in Ajax call
24 de febrero de 2011 6:21
Respuesta

Tomas Polesovsky

LIFERAY STAFF

Ranking: Liferay Master

Mensajes: 590

Fecha de incorporación: 13 de febrero de 2009

Mensajes recientes

I mean - the JSP page cannot be processed. But you haven't posted why - what is the cause why the page cannot be processed emoticon

-- tom
Muhammad Asif
RE: problem in Ajax call
3 de junio de 2011 13:42
Respuesta

Muhammad Asif

Ranking: New Member

Mensajes: 24

Fecha de incorporación: 11 de abril de 2011

Mensajes recientes

Koen Cleynhens:
Thanks,

it works now.

I just add next lines in my JSP:

<%
PortletURLImpl responseURL = (PortletURLImpl)renderResponse.createResourceURL();
responseURL.setCopyCurrentRenderParameters(false);

%>

I know it is not the most clean way, but I don't know another solution for the moment...



I think this does not work in 6.0.6 portal and 6.0.5 plugin sdk versions. I could not fine PortletURLImpl here.

Regards,
Asif