Tribune

Home » Liferay Portal » English » 3. Development

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
toggle
Luca kk
How to pass parameter from javascript to processAction() method
3 aprile 2012 9.08
Risposta

Luca kk

Punteggio: Regular Member

Messaggi: 242

Data di Iscrizione: 7 dicembre 2010

Messaggi recenti

Hi all,
I have to pass parameters from a js function, in view.jsp, to method
"processAction()" without calling "DoView()" method.

How can i do?

Thanks in advance.
Tejas Kanani
RE: How to pass parameter from javascript to processAction() method
3 aprile 2012 10.21
Risposta

Tejas Kanani

Punteggio: Liferay Master

Messaggi: 653

Data di Iscrizione: 6 gennaio 2009

Messaggi recenti

Hi Luca,

What is your exact requirement ?
As once we go to action phase it will call render phase hence it will go to doView() method.

Other alternative is you can generate resourceURL and pass parameter in it from jsp and which you will get in serverResource method.
Luca kk
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 0.51
Risposta

Luca kk

Punteggio: Regular Member

Messaggi: 242

Data di Iscrizione: 7 dicembre 2010

Messaggi recenti

Hi Tejas,
in my "view.jsp", of my portlet, I have a video player and
I have to log the "play" event, for example writing in my database,
of the logged user.
To make this in view.jsp I use a javascript function of the player and
I have to pass this event information, for example a String,
from jsp to java.
If I use an hidden form I have to call necessarily a "submit" method
that calls the "processAction" method and so the render method but
I don't want this.
Is it possible to pass or to write into db from view.jsp or java class
without passing to "doView" method?

Thanks,
Luca
Tejas Kanani
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 1.01
Risposta

Tejas Kanani

Punteggio: Liferay Master

Messaggi: 653

Data di Iscrizione: 6 gennaio 2009

Messaggi recenti

You can use resourceURL for the same. And pass your parameter(String) into that url.
And you'll get that value in serveResource() method in controller.
From there you can use any service to write your data in DB.

For more details regarding resourceURL,
http://developers.sun.com/portalserver/reference/techart/jsr286/jsr286_2.html#Serving_Resources

HTH
Luca kk
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 1.29
Risposta

Luca kk

Punteggio: Regular Member

Messaggi: 242

Data di Iscrizione: 7 dicembre 2010

Messaggi recenti

Hi Tejas,
I understand the "serveResource()" method in java class,
I get my resource with:

1String resourceID = request.getResourceID();


But how do I pass that resource from my javascript function in my jsp?
Simply generating a url variable like this:

1var url = '<portlet:resourceURL id="getDataResourceURL"><portlet:param name="parameter" [b]value="1"[/b] /></portlet:resourceURL>';


where "value" will be set with my string?

Thanks,
Luca
Tejas Kanani
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 1.58
Risposta

Tejas Kanani

Punteggio: Liferay Master

Messaggi: 653

Data di Iscrizione: 6 gennaio 2009

Messaggi recenti

Try below code,


 1<%
 2    String paramValue = "yourValue";
 3%>
 4
 5<portlet:resourceURL var="requestUrl"></portlet:resourceURL>
 6
 7<aui:button name="accept"  value="accept" onClick="callAccept()"></aui:button>
 8                                    
 9<script type="text/javascript">
10
11    function callAccept(id)
12    {
13        var parameter1 = <%=paramValue%>;
14        var param ="&parameter1=" + parameter1;
15       
16        jQuery.ajax({
17            type:"GET",
18            url:"<%=requestUrl%>",
19            cache:false,
20            async:false,
21            data:param,
22            success: function(data){   
23                <!-- TODO if success-->
24            }
25        });   
26    }
27</script>



And from serverResource()

final String param1Value = resourceRequest.getParameter("parameter1");


Hope this helps.
Luca kk
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 3.10
Risposta

Luca kk

Punteggio: Regular Member

Messaggi: 242

Data di Iscrizione: 7 dicembre 2010

Messaggi recenti

Hi Tejas,
thanks for code.
I tried your code but i get an error on:

1url:"<%=[b]requestUrl[/b]%>",


Pratically it can't see the "requesUrl" variable.
Do I have to put some "page import" in my jsp?

Thanks,
Luca
Tejas Kanani
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 3.16
Risposta

Tejas Kanani

Punteggio: Liferay Master

Messaggi: 653

Data di Iscrizione: 6 gennaio 2009

Messaggi recenti

Is it possible for you to paste your entire jsp code here.
So that we'll find exact issue with it.

As it suppose to work. I've used the same thing in my previous project as well.
Luca kk
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 3.35
Risposta

Luca kk

Punteggio: Regular Member

Messaggi: 242

Data di Iscrizione: 7 dicembre 2010

Messaggi recenti

Here my jsp code:

 1<%@page contentType="text/html"%>
 2<%@page pageEncoding="UTF-8"%>
 3
 4<%-- Uncomment below lines to add portlet taglibs to jsp --%>
 5<%@page import="javax.portlet.*"%>
 6<%@page import="java.text.DateFormat"%>
 7<%@page import="java.text.SimpleDateFormat"%>
 8<%@page import="java.util.Calendar"%>
 9
10<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
11<%@taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
12
13<portlet:defineObjects />
14<%PortletPreferences prefs = renderRequest.getPreferences();%>
15<script type="text/javascript" src="/html/themes/classic/js/jwplayer.js"></script>
16
17[b]<portlet:resourceURL var="requestUrl"></portlet:resourceURL>
18
19<script type="text/javascript">
20     function callAccept(id)
21    {
22        //var parameter1 = <!--%=paramValue%-->;
23        var param ="&parameter1=" + "play";
24        alert(param);
25      
26        jQuery.ajax({
27            type:"GET",
28            url:"<%=requestUrl%>",         
29            cache:false,
30            async:false,
31            data:param,
32            success: function(data){
33                <!-- TODO if success-->
34            }
35        });
36    }
37
38 </script>[/b]
39
40 <div id="contenitore-player" class="contenitore-player">
41
42    <div id="titolo_video" class="titolo_video">
43    <p class = "titolo-sx" >Titolo video</p>
44    </div>
45    <div id="player_video" class="player_video">
46    <div id="container" style="position:absolute; top: 100px;">Loading the player ...</div>
47
48    <script type="text/javascript">
49        jwplayer("container").setup({
50       
51        ........................
52       
53       
54        });
55        [b]jwplayer().onPlay(function() {callAccept(); }[/b]
56      );
57    </script>
58    </div>
59</div>
60   
Jitendra Rajput
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 4.39
Risposta

Jitendra Rajput

Punteggio: Liferay Master

Messaggi: 709

Data di Iscrizione: 7 gennaio 2011

Messaggi recenti

arguments are mis matching ..

1jwplayer().onPlay(function() {callAccept(); }


Here you are not passing any thing ..

1  function callAccept(id)
2    {
3    



And here you are expecting id...
Tejas Kanani
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 4.46
Risposta

Tejas Kanani

Punteggio: Liferay Master

Messaggi: 653

Data di Iscrizione: 6 gennaio 2009

Messaggi recenti

Nice catch Jitendra,

Luca,
You can remove parameter id from callAccept() function.

And if still doesn't work for you, please provide error logs.
Luca kk
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 4.49
Risposta

Luca kk

Punteggio: Regular Member

Messaggi: 242

Data di Iscrizione: 7 dicembre 2010

Messaggi recenti

Hi Jitendra,
the function "callAccept()" is calling anyway even without input parameter
(I have put an alert and this works!)
The problem is that that code doesn't call the serveResource() method.

How can it call the serveResource method from jsp without using "location.href=url"?

Thanks.
Jitendra Rajput
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 5.31
Risposta

Jitendra Rajput

Punteggio: Liferay Master

Messaggi: 709

Data di Iscrizione: 7 gennaio 2011

Messaggi recenti

Any Js or other error ?


I hope your serveResource() method signature is correct ..
1
2public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
3}
Luca kk
RE: How to pass parameter from javascript to processAction() method
4 aprile 2012 5.59
Risposta

Luca kk

Punteggio: Regular Member

Messaggi: 242

Data di Iscrizione: 7 dicembre 2010

Messaggi recenti

Yes,
the serveResource() signature is correct,
I use another js (jwplayer.js) and I have no other errors.
Roshan Qureshi
RE: How to pass parameter from javascript to processAction() method
8 aprile 2012 23.26
Risposta

Roshan Qureshi

Punteggio: Regular Member

Messaggi: 143

Data di Iscrizione: 24 agosto 2010

Messaggi recenti

Hi Luca,

You can get variables defined in between scriptlet <% %> in javascript using following way.

1) first define a variable in your jsp between scriptlet like
<%
String parameter = "paramvalue";
%>

2) Now go to your javascript and write following code
<script type="text/javascript">
var parameter = "<%= parameter %>" // Name of the variable defined in jsp
function callAccept(id)
{
var url = '<portlet:resourceURL id="getDataResourceURL"><portlet:param name="parameter" value="'+ parameter+' "/></portlet:resourceURL>';
jQuery.ajax({
type:"GET",
url:url,
cache:false,
async:false,
data:param,
success: function(data){
<!-- TODO if success-->
}
});
}

Hope this will work.
Let me know if any query.