You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-user@portals.apache.org by robin <ro...@adm-soft.com> on 2010/09/27 04:45:11 UTC

How to use JQuery ajax in jetspeed

Hi All

 Is there any document guide to use JQuery Ajax in jetspeed2, I want to using jsTree + struts2 in a jsr168Dispatcher portlet, but i can't get request parameter value in action, and can't return json data to jsTree. 

code segment as following

 
"json_data" : { 
     "data" :  <%=req.getAttribute("res").toString()%>,    // init tree data
             "ajax" : { 
              "url" : "<s:url action="loadAction"></s:url>",  //struts 2 url, it works
              "type" : "POST",
              "data" : function (n) {   // the  tree's node id
                   return { "parentId" : n.attr("id") }  // parameter of the ajax request
                }
         }  
}

portlet config:
=============================================================
 <portlet id="StrutsPortlet">
    <description xml:lang="EN">Struts Test Portlet</description>
    <portlet-name>StrutsPortlet</portlet-name>
    <display-name xml:lang="EN">Struts Test Portlet</display-name>
    
    <portlet-class>org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher</portlet-class>
    <init-param>
<!-- The view mode namespace. Maps to a namespace in the xwork config file -->
<name>viewNamespace</name>
<value>/view</value>
</init-param>
    <init-param>
<!-- The default action to invoke in view mode -->
<name>defaultViewAction</name>
<value>index</value>
</init-param>
<init-param>
<!-- The view mode namespace. Maps to a namespace in the xwork config file -->
<name>editNamespace</name>
<value>/edit</value>
</init-param>
    <init-param>
<!-- The default action to invoke in view mode -->
<name>defaultEditAction</name>
<value>index</value>
</init-param>

<init-param>
<!-- The view mode namespace. Maps to a namespace in the xwork config file -->
<name>helpNamespace</name>
<value>/help</value>
</init-param>
    <init-param>
<!-- The default action to invoke in view mode -->
<name>defaultHelpAction</name>
<value>index</value>
</init-param>
    
    <expiration-cache>0</expiration-cache>
    <supports>
      <mime-type>text/html</mime-type>
      <portlet-mode>edit</portlet-mode>
      <p

  <portlet-mode>help</portlet-mode>
    </supports>
    <supported-locale>en</supported-locale>
    <portlet-info>
      <title>My StrutsPortlet portlet</title>
      <short-title>SP</short-title>
      <keywords>struts,portlet</keywords>
    </portlet-info>
  </portlet>

thanks you.

2010-09-27 



Robin 

Re: Re: How to use JQuery ajax in jetspeed

Posted by robin <ro...@adm-soft.com>.
it works, the key point is the url must be traditional struts url

"json_data" : { 
     "data" :  <%=req.getAttribute("res").toString()%>,    // init tree data
             "ajax" : { 
              "url" : "<%request.getContextPath()/view/loadAction.action%>",  
              "type" : "POST",
              "data" : function (n) {   // the  tree's node id
                   return { "parentId" : n.attr("id") }  // parameter of the ajax request
                }
         }  
}

<s:url action="loadAction"></s:url>  will create url like this:http://localhost:8081/jetexpress/portal/_ns:YXRlbXBsYXRlLXRvcDJfX3BhZ2UtdGVtcGxhdGVfX2pzbWluLTJfX1AtMTJiOGViYzE2N2YtMTAwMDB8YzB8ZDF8ZXN0cnV0cy5wb3J0bGV0Lm1vZGU9MT12aWV3fGVzdHJ1dHMucG9ydGxldC5hY3Rpb249MT0vdmlldy9pbml0VHJlZQ__/

2010-10-09 



Robin 



发件人: Roberto Rossi 
发送时间: 2010-09-27  16:04:27 
收件人: Jetspeed Users List 
抄送: 
主题: Re: How to use JQuery ajax in jetspeed 
 
I don't know if there are guides about integrating jQuery (and AJAX in
general) in Jetspeed2 but some months ago, reading messages in this and
the Jetspeed Developers mailing lists , I found a way to do this in the
doView portlet method.
First of all you have to enable the portlet-pipeline in the avalaible
Jetspeed pipelines.
Usually you can do this modifying the pipelines.xml file, uncomment, if
necessary, these lines:
<bean id="portlet-pipeline"
        class="org.apache.jetspeed.pipeline.JetspeedPipeline"
        init-method="initialize"
  >
   <constructor-arg>
       <value>PortletPipeline</value>
   </constructor-arg>
   <constructor-arg>
    <list>
        <ref bean="portalURLValve"/>
        <ref bean="capabilityValve"/>
        <ref bean="securityValve"/>
        <ref bean="localizationValve"/>
        <ref bean="profilerValve"/>
        <ref bean="containerValve"/>
        <ref bean="portletValveTitleInHeader"/>
    </list>
    </constructor-arg>
  </bean>
and
            <entry key='/portlet'>
                <value>portlet-pipeline</value>
            </entry>
Maybe there can differences between my lines and yours. 
I'm currenty using Jetspeed 2.1.3 implementation.
Second you should pass some special parameters to your request to let
Jetspeed communicate only with your "ajax" portlet.
This is an example of a jQuery ajax request that can receive a JSON
response:
                $.ajax({
    url: '<yourjetspeedappname>/portlet/<yourpage.psml>',
    type: 'GET',
    dataType: 'json',
    data: {"entity": '<yourportletentityname>'},
    // 1 minute wait
    timeout: 60000,
    error: function() {
        alert('errors');
    },
    success: function(json) {
        alert(json.message);
    }
});
You should change <yourjetspeedappname> , <yourpage.psml> and
<yourportletentityname> accordingly with your Jetspeed installation and
page and portlet infos.
You can also use POST instead of GET.
Then in your portlet you should create a JSONObject response like this:
...
JSONObject jsonObject = new JSONObject();
jsonObject.put("type", "success");
jsonObject.put("message", "that's OK");
context.put("jsonObject", jsonObject);
...
the Velocity file (for me: ajax.vm), the view file for this ajax
portlet, contains only this code:
$!jsonObject 
If all the steps are ok you should receive the portlet json message in a
javascript alert in your html code.
I hope this can help!
ROb

Re: How to use JQuery ajax in jetspeed

Posted by Roberto Rossi <ro...@cone.it>.
I don't know if there are guides about integrating jQuery (and AJAX in
general) in Jetspeed2 but some months ago, reading messages in this and
the Jetspeed Developers mailing lists , I found a way to do this in the
doView portlet method.

First of all you have to enable the portlet-pipeline in the avalaible
Jetspeed pipelines.
Usually you can do this modifying the pipelines.xml file, uncomment, if
necessary, these lines:

<bean id="portlet-pipeline"
        class="org.apache.jetspeed.pipeline.JetspeedPipeline"
        init-method="initialize"
  >
   <constructor-arg>
       <value>PortletPipeline</value>
   </constructor-arg>
   <constructor-arg>
    <list>
        <ref bean="portalURLValve"/>
        <ref bean="capabilityValve"/>
        <ref bean="securityValve"/>
        <ref bean="localizationValve"/>

        <ref bean="profilerValve"/>
        <ref bean="containerValve"/>
        <ref bean="portletValveTitleInHeader"/>
    </list>
    </constructor-arg>
  </bean>

and

            <entry key='/portlet'>
                <value>portlet-pipeline</value>
            </entry>

Maybe there can differences between my lines and yours. 
I'm currenty using Jetspeed 2.1.3 implementation.

Second you should pass some special parameters to your request to let
Jetspeed communicate only with your "ajax" portlet.
This is an example of a jQuery ajax request that can receive a JSON
response:

                $.ajax({
		    url: '<yourjetspeedappname>/portlet/<yourpage.psml>',
		    type: 'GET',
		    dataType: 'json',
		    data: {"entity": '<yourportletentityname>'},
		    // 1 minute wait
		    timeout: 60000,
		    error: function() {
		        alert('errors');
		    },
		    success: function(json) {
		        alert(json.message);
		    }
		});

You should change <yourjetspeedappname> , <yourpage.psml> and
<yourportletentityname> accordingly with your Jetspeed installation and
page and portlet infos.
You can also use POST instead of GET.
Then in your portlet you should create a JSONObject response like this:

...
JSONObject jsonObject = new JSONObject();
jsonObject.put("type", "success");
jsonObject.put("message", "that's OK");
context.put("jsonObject", jsonObject);
...

the Velocity file (for me: ajax.vm), the view file for this ajax
portlet, contains only this code:

$!jsonObject 

If all the steps are ok you should receive the portlet json message in a
javascript alert in your html code.

I hope this can help!

ROb