Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Arko Mahmud
Understanding DWR in Liferay
August 9, 2013 7:20 AM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

Hello Everyone
First of all I would like to mention that i am new in Liferay and Web Technology as well.

I extremely need your support. to understand DWR and Liferay. I managed to configure DWR in dwr.xml and web.xml. And I can also visit http://localhost:8080/dwrtest-portlet/dwr/index.html and check created class.

Please try to explain following things:
1. It does not show the view.jsp in deployed place. instead its giving follow Warning

14:07:04,760 WARN [NotFoundHandler:38] Page not found. pathInfo='/portal/layout' requestUrl='/c/portal/layout'
14:07:04,761 WARN [NotFoundHandler:39] In debug/test mode try viewing /[WEB-APP]/dwr/

2.

My Test Class:

 1package com.testdwr;
 2
 3import java.io.IOException;
 4
 5import javax.servlet.ServletException;
 6import javax.servlet.http.HttpServletRequest;
 7import javax.servlet.http.HttpSession;
 8
 9
10import org.directwebremoting.WebContext;
11import org.directwebremoting.WebContextFactory;
12
13public class Simpledwr {
14   
15public String sayHello(String message, HttpServletRequest request) throws InterruptedException, IOException {
16       
17        WebContext webContext = (WebContext) WebContextFactory.get();
18        //HttpSession session = webContext.getSession();
19            message = "Hello " + message;
20            System.out.println("Here It goes ");
21            request = webContext.getHttpServletRequest();
22            request.setAttribute("message", message);
23         
24         
25          try {
26                System.out.println("made html ");
27            String html = webContext.forwardToString("/html/new/view.jsp");
28            System.out.println("return html ");
29            return html;
30        } catch (ServletException e) {
31            // TODO Auto-generated catch block
32            e.printStackTrace();
33            return message;
34            
35        }
36       
37         
38       
39    }
40
41
42}


My MVC portlet class is empty

 1package com.testdwr;
 2
 3import com.liferay.util.bridges.mvc.MVCPortlet;
 4
 5/**
 6 * Portlet implementation class NewPortlet
 7 */
 8public class NewPortlet extends MVCPortlet {
 9
10
11}


My View JSP:

 1<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
 2
 3<portlet:defineObjects />
 4
 5<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
 6<script type='text/javascript' src='/dwrtest-portlet/dwr/engine.js'></script>
 7  <script type='text/javascript' src='/dwrtest-portlet/dwr/interface/Simpledwr.js'></script>
 8  <script type='text/javascript' src='/dwrtest-portlet/dwr/util.js'></script>
 9   <%
10      String message = (String)request.getAttribute("message");
11   %>
12   <b><%= message %></b>


P:S: In (http://localhost:8080/dwrtest-portlet/dwr/index.html) link I can see my class, There I get an option to check method and I get message there that reads my view.jsp

Could any one please explain me whats going on ??
Arko Mahmud
RE: Understanding DWR in Liferay
August 12, 2013 12:39 AM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

Could Anyone support me today ?
Sampsa Sohlman
RE: Understanding DWR in Liferay
August 12, 2013 6:57 AM
Answer

Sampsa Sohlman

LIFERAY STAFF

Rank: Regular Member

Posts: 219

Join Date: September 27, 2007

Recent Posts

Arko Mahmud:

 1<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
 2
 3<portlet:defineObjects />
 4
 5<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
 6<script type='text/javascript' src='/dwrtest-portlet/dwr/engine.js'></script>
 7  <script type='text/javascript' src='/dwrtest-portlet/dwr/interface/Simpledwr.js'></script>
 8  <script type='text/javascript' src='/dwrtest-portlet/dwr/util.js'></script>
 9   <%
10      String message = (String)request.getAttribute("message");
11   %>
12   <b><%= message %></b>



JS includes you should put at liferay-portlet.xml. There you can and javascript header and to footer for the portlet.

I'll look other parts of you questions later.
Priyanka Dhingra
RE: Understanding DWR in Liferay
August 12, 2013 10:07 PM
Answer

Priyanka Dhingra

Rank: Liferay Master

Posts: 501

Join Date: December 19, 2011

Recent Posts

Check this thread and verify all you have done
Arko Mahmud
RE: Understanding DWR in Liferay
August 13, 2013 1:06 AM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

Hello Sampsa and Priyanka
Thank You for your answers,
Java script moved to liferay-portlet.xml (Sampsa).
Yes, actually I followed that thread (Priyanka). My DWR configuration works fine. I can visit [My App]/dwr link and check methods there also.
My problem is, I don't know how to call it in JSP using javascript. And How can I integrate portlet class with DWR.
It would be nice if some of you help me to figure it out.

BR
Arko
Arko Mahmud
RE: Understanding DWR in Liferay
August 13, 2013 6:42 AM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

Here is the latest JSP I am trying But no Luck Yet :

 1 <script type="text/javascript">
 2   
 3   
 4      
 5      function <portlet:namespace />update(){
 6         
 7          var name = $("#demoName").val();
 8         // $("#last").html("<b>Hello world!</b>" + name);
 9      SimpleDwr.sayHello( name,
10      { callback:function(dataFromServer) {
11         <portlet:namespace />processResults("dataDivId", dataFromServer);
12      }, errorHandler:function(errorString, exception) {
13         <portlet:namespace />showError("errorDivId", errorString); }
14      } );
15      
16    
17      }
18      
19   
20      function <portlet:namespace />processResults(divId, data)
21      {
22         if (data != null) {
23       dwr.util.setValue(divId, data);
24         }
25      }
26   
27      <%-- handle AJAX error, error callback is optional --%>
28      function <portlet:namespace />showError(errorDiv, message) {
29         dwr.util.setValue(errorDiv, "Error: "+message+"");
30      }
31   </script>
32   
33look for this
34
35
36  Name:
37  <input type="text" id="demoName"/>
38  <input value="Send" type="button" onclick="<portlet:namespace />update()"/>
39  <br/>
40 
41 Reply: <span id="<portlet:namespace/>dataDivId"></span>
42    <br/>
43 Reply2: <span id="<portlet:namespace/>errorDivId"></span>
44 <br/>
45
Priyanka Dhingra
RE: Understanding DWR in Liferay
August 13, 2013 10:53 PM
Answer

Priyanka Dhingra

Rank: Liferay Master

Posts: 501

Join Date: December 19, 2011

Recent Posts

I simply did like this
1SimpleDwr.sayHello(name,myhelloCallback);
2
3var myhelloCallback = {callback:function(serverdata){
4   
5    jsonTableObject = serverdata;
6console.log(serverdata);
7}};
Arko Mahmud
RE: Understanding DWR in Liferay
August 14, 2013 2:03 AM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

I am sorry, But I could not get result this time too.

 1
 2<script>
 3    function <portlet:namespace />update(){
 4        var name = $("#demoName").val();
 5         
 6       TestDwrService.sayHello( name,myhelloCallback);
 7      
 8       var myhelloCallback = {callback:function(serverdata){
 9          
10           console.log("Here After");
11           jsonTableObject = serverdata;
12               console.log(serverdata);
13            }};
14       }
15   
16   </script>
17
18look for this Name:
19<input type="text" id="demoName"/>
20  <input value="Send" type="button" onclick="<portlet:namespace />update()"/>


I don't get anything in console of firebug, but my response looks strange. I am getting the same from yesterday. Please see the response below. [P.S. instead of SimpleDwr I changed the name to TestDwrService, ]


Priyanka Dhingra
RE: Understanding DWR in Liferay
August 14, 2013 2:48 AM
Answer

Priyanka Dhingra

Rank: Liferay Master

Posts: 501

Join Date: December 19, 2011

Recent Posts

Keep the callback out of function
 1<script>
 2    function <portlet:namespace />update(){
 3         var name = $("#demoName").val();
 4         
 5        TestDwrService.sayHello( name,myhelloCallback);
 6      
 7       }
 8   
 9        var myhelloCallback = {callback:function(serverdata){
10          
11           console.log("Here After");
12           jsonTableObject = serverdata;
13               console.log(serverdata);
14            }};
15   </script>
Arko Mahmud
RE: Understanding DWR in Liferay
August 14, 2013 3:16 AM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

Thanks for for your prompt replies..
But unfortunately Its not working.
But this time in console I get response like this... Is it something with my class that contains sayhello method ? or my portlet class ?




Just for further check here is my sayhello method, And it returns html (Checked it using System.out.println)

 1public String sayHello(String message, HttpServletRequest request ) throws InterruptedException, IOException {
 2        Thread.sleep(4000);
 3        WebContext webContext = (WebContext) WebContextFactory.get();
 4        //HttpSession session = webContext.getSession();
 5         
 6            message = "Hello " + message;
 7            System.out.println("Here It goes ");
 8             request = webContext.getHttpServletRequest();
 9            request.setAttribute("message", message);
10         
11         
12          try {
13                System.out.println("made html ");
14            String html = webContext.forwardToString("/WEB-INF/jsp/view.jsp");
15            System.out.println("return html ");
16            return html;
17        } catch (ServletException e) {
18            // TODO Auto-generated catch block
19            e.printStackTrace();
20            return message;
21            
22        }
23       
24         
25       
26    }
Meera Prince
RE: Understanding DWR in Liferay
August 14, 2013 3:29 AM
Answer

Meera Prince

Rank: Liferay Master

Posts: 842

Join Date: February 8, 2011

Recent Posts

Hi

http://www.liferaysavvy.com/2013/08/liferay-dwr-portlet.html


Regards,

Meera Prince
Priyanka Dhingra
RE: Understanding DWR in Liferay
August 14, 2013 3:31 AM
Answer

Priyanka Dhingra

Rank: Liferay Master

Posts: 501

Join Date: December 19, 2011

Recent Posts

In your sayhello method you are returning
1html = webContext.forwardToString("/WEB-INF/jsp/view.jsp");
isn't this same as your console output
Arko Mahmud
RE: Understanding DWR in Liferay
August 14, 2013 4:01 AM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

Thanks Meera, Pryianka and Sampsa
I was confused with command

WebContext webContext = (WebContext) WebContextFactory.get();

I thought it is necessary to put it in method and so many stupid imagination....emoticon
Now i figured out how simple it is. And also figured out when WebContext needs.

I appreciate you guys to bear with my stupid questions.


Arko
Meera Prince
RE: Understanding DWR in Liferay
August 15, 2013 1:51 AM
Answer

Meera Prince

Rank: Liferay Master

Posts: 842

Join Date: February 8, 2011

Recent Posts

Liferay DWR JSON Webservises...

http://www.liferaysavvy.com/2013/08/liferay-dwr-json-web-services-portlet.html


Regards

Meera Prince
http://www.liferaysavvy.com