Liferay Development

Liferay Consulting

Liferay Training

Your Trusted Liferay Solutions Partner

Wednesday, August 14, 2013

Liferay DWR portlet

Download portlet from following link


Steps to create DWR Portlet

Create MVC portlet using Liferay IDE
Configure DWR serve let in  portlet web.xml file.
<servlet>
                        <servlet-name>dwr-invoker</servlet-name>
                        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
                        <init-param>
                                    <param-name>debug</param-name>
                                    <param-value>true</param-value>
                        </init-param>
                        <init-param>
                                    <param-name>activeReverseAjaxEnabled</param-name>
                                    <param-value>true</param-value>
                        </init-param>
                        <init-param>
                                    <param-name>crossDomainSessionSecurity</param-name>
                                    <param-value>false</param-value>
                        </init-param>
                        <init-param>
                                    <param-name>initApplicationScopeCreatorsAtStartup</param-name>
                                    <param-value>true</param-value>
                        </init-param>
                        <init-param>
                                    <param-name>maxWaitAfterWrite</param-name>
                                    <param-value>-1</param-value>
                        </init-param>
                        <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
                        <servlet-name>dwr-invoker</servlet-name>
                        <url-pattern>/dwr/*</url-pattern>
            </servlet-mapping>

Create dwr.xml file in WEB-INF directory of your portlet
Add following tags in dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">

<dwr>
  <allow>
  
  </allow>
</dwr>

Create Java class based on your need

In my example my java class is com.meere.dwr.Person.java

Configure java class in dwr.xml files

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">

<dwr>
  <allow>
   <create creator="new" javascript="Person">
      <param name="class" value="com.meera.dwr.Person"/>
    </create>
  </allow>
</dwr>

Access your java class in your jsp page i.e.  Client side

Add following java script file in your jsp

<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/interface/Person.js'> </script>
<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/engine.js'> </script>

Note:

In above Person.js and engine.js file physically not existed any where  don’t worry .

And whatever the attribute value you used for JavaScript in dwr.xml file  by that name your client side java script will be created i.e. in my case Person.js

dwr.xml

<create creator="new" javascript="Person">
      <param name="class" value="com.meera.dwr.Person"/>
    </create>

Java script call in jsp page

<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/interface/Person.js'> </script>

Engine.js is common for all….

<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/engine.js'> </script>

Final code in jsp page

<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/interface/Person.js'> </script>
<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/engine.js'> </script>
<h1>Sample Liferay DWR portlet</h1>
<button id="checkDwr">GetName Using DWR</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#checkDwr").click(function(){
         Person.getData(42, {
                callback:function(str) {
                  alert(str);
                }
              });
  });
});
</script>


Liferay version 6.1x

More details go through following links




Popular Posts

Recent Posts

Recent Posts Widget