11 Replies Latest reply: May 4, 2012 8:37 AM by Jesse Booth RSS

    access java method from ftl

    amyc

      I am doing a widget which need to access the method of a Java class from a method. I had declare it in the spring.xml

       

      <bean id="deployWidgetConstants" class="com.jivesoftware.sbs.plugins.deploywidget.DeployWidgetConstants" parent="remoteSupport">
              <dwr:remote javascript="DeployWidgetConstants">
                  <dwr:include method="getPriority"/>
              </dwr:remote>
         </bean>
      

       

      and in ftl

       

       

       

      <@resource.dwr file="DeployWidgetConstants"/>
      <@resource.javascript>
           DeployWidgetConstants deploy = new DeployWidgetConstants();
      </@resource.javascript>
      
      <#assign priority = deploy.getPriority(thread) />
           ${priority}
      

       

      in DeployWidgetConstants.java

      public String getPriority(ForumThread thread) {
        return getConfigProperty("priority",thread);
        } // end getPriority
      

       

       

      but I get deploy undefined thus my priority isn't displaying.

      I try to reference Java class in the ftl in the future as I need to sort and filter too.

      Widget seems to work differently compare plugin.

      Is this the right way to reference it?

        • Re: access java method from ftl
          Ryan Rutan

          In passing right now, but check out the Do you know Jive-Fu? plugin.  There is a working example in there that should show you the way.  In general, need to make sure that any parameters that are passed in as parameters or returned are registered in DWR as a bean so the framework knows how to serialize it.  May be best to pass in the threadID and then use the ForumManager to load the thread and return the property.  Hope this helps.

            • Re: access java method from ftl
              amyc


              It seems it didnt really defined the variable "deploy" and "priority".  I am really new to this and I am a little bit confuse with the codes. Even though I wanted to pass in the threadID but I cant access any DeployWidgetConstants.java method inside the ftl nor that I can pass in any value in it.


              My spring and ftl had declare the class but it still report object undefined. Any hints?

                  • Re: access java method from ftl
                    amyc

                    I follow the examples but something is wrong.

                    Please take a look at my code. I suspect something missed out.

                    This my deploy-widget.ftl

                    <head>
                     <link rel="stylesheet" href="<@s.url value='/plugins/deploy-widget-plugin/resources/styles/deploy-widget.css' />" type="text/css">
                    </head>
                    <body>
                    <@resource.dwr file="DeployWidgetUtils"/>
                    
                    
                      <div class="deploy-projects-results-header clearfix">
                      <div class="deploy-projects-column-name"><@s.text name='projects.browse.name.colhead' /></div>
                      <div class="deploy-projects-column-status">Status</div>
                      <div class="deploy-projects-column-severity">Severity</div>
                      <div class="deploy-projects-column-priority">Priority</div>
                      </div>
                    
                      <div class="deploy-projects-results">
                      <#if discussions?exists>
                      <#list discussions as discussion>
                    
                      <div class="deploy-projects-row jive-table-row-[rowStyle] clearfix" >
                      <div class="deploy-projects-column-name">
                      <a href="http://nitalk-dev.natinst.com/threads/${discussion.getID()?c}">${discussion.getName()}</a><br/>
                      </div>
                      <div class="deploy-projects-column-status">
                      <#assign status = DeployWidgetUtils.getStatus(discussion)/>
                      ${status}
                      </div>
                      <div class="deploy-projects-column-severity">
                      <#assign severity = DeployWidgetUtils.getSeverity(discussion)/>
                      ${severity}
                      </div>
                      <div class="deploy-projects-column-priority">
                      <#assign priority = DeployWidgetUtils.getPriority(discussion)/>
                      ${priority}
                      </div>
                      </div>
                      </#list>
                    
                      </#if>
                      </div>
                    </body>
                    

                     

                    spring.xml

                     <bean id="deployWidgetUtils" class="com.jivesoftware.sbs.plugins.deploywidget.deploy.DeployWidgetUtils" parent="remoteSupport">
                            <dwr:remote javascript="DeployWidgetUtils">
                                <dwr:include method="getPriority"/>
                                <dwr:include method="getSeverity"/>
                                <dwr:include method="getStatus"/>
                            </dwr:remote>
                       </bean>
                    

                     

                    DeployWidgetUtils.java

                    public class DeployWidgetUtils extends RemoteSupport {
                      private static final Logger s_log = Logger.getLogger(DeployWidgetConstants.class);
                      public String getPriority(ForumThread thread) {
                    
                      return DeployWidgetConstants.getConfigProperty("priority",thread);
                      } // end getPriority
                    
                      public String getSeverity(ForumThread thread) {
                    
                      return DeployWidgetConstants.getConfigProperty("severity",thread);
                      } // end getSeverity
                    
                      public static String getStatus(ForumThread thread) {
                    
                      return DeployWidgetConstants.getConfigProperty("status", thread);
                      } // end getStatus
                    
                    }
                    
                      • Re: access java method from ftl
                        Ryan Rutan

                        Off the top of my head, I do not think that ForumThread is a registered DWR Bean to be serialized.  Instead of making 3 separate calls for values on a single object, I would return  a bean populated with this information in a single hit.

                         

                        Also, you are crossing DWR/Freemarker syntaxes which will not work.  I would talk to Jesse and have him walk through the DWR example, I know I've walked through it once with him before and he seemed to have a decent grasp on it.

                         

                        Hope this helps,

                        RR

                          • Re: access java method from ftl
                            amyc

                            I emailed Jesse about it today so in the mean while I tried to trial and error with the codes and create 2 classes : DeployProjectWidgetResponse.java and DeployProjectWidgetBean.java

                            Inside the bean I make a few changes:

                             <dwr:configuration>
                              <dwr:convert class="com.jivesoftware.sbs.plugins.deploywidget.remote.DeployProjectWidgetResponse" type="bean">
                              <dwr:include method="priority"/>
                              <dwr:include method="severity"/>
                              <dwr:include method="status"/>
                                 <dwr:include method="requestStatus"/>
                                 <dwr:include method="statusMessage"/>
                              </dwr:convert>
                              <dwr:convert class="com.jivesoftware.sbs.plugins.deploywidget.remote. DeployProjectWidgetBean" type="bean">
                              <dwr:include method="threadID"/>
                              <dwr:include method="threadName"/>
                              <dwr:include method="priority"/>
                              <dwr:include method="severity"/>
                              <dwr:include method="status"/>
                                 <dwr:include method="url"/>
                              </dwr:convert>
                              </dwr:configuration>
                            

                             

                            DeployProjectWidgetResponse.java

                             

                            public class DeployProjectWidgetResponse extends BaseRemoteResponse {
                            
                            
                              private static final long serialVersionUID = 1768713990992640933L;
                            private String m_severity = null;
                              private String m_priority = null;
                              private String m_status = null;
                            
                              public DeployProjectWidgetResponse() {
                              super();
                              } // end constructor
                            
                              public String getPriority() { return m_priority; }
                              public void setPriority(String priority) {
                              m_priority = priority;
                              } // end get/setState
                            
                              public String getSeverity() { return m_severity; }
                              public void setSeverity(String severity) {
                              m_severity = severity;
                              } // end get/setState
                            
                            
                              public String getStatus() { return m_status; }
                              public void setStatus(String status) {
                              m_status = status;
                              } // end get/setStatus
                            }
                            

                             

                            DeployProjectWidgetBean.

                            public class DeployProjectWidgetBean implements Serializable  {
                            
                            
                              private static final long serialVersionUID = 54387796948612L;
                              private long m_threadID = -1;
                              private String m_threadName = nul
                              private String m_url= null;
                              private String m_severity = null;
                            
                            
                              private String m_priority = null;
                              private String m_status = null;
                            
                            
                            
                              public DeployProjectWidgetBean(HttpServletRequest request, User user, ForumThread thread) {
                              m_threadID = thread.getID();
                              m_threadName = thread.getName();
                              m_url = JiveResourceResolver.getJiveObjectURL(thread,false);
                              m_priority = thread.getProperties().get("priority");
                              m_severity = thread.getProperties().get("severity");
                              m_status = thread.getProperties().get("status");
                            
                              } // end constructor
                            
                            
                              public long getThreadID() { return m_threadID; }
                              public void setThreadID(long threadID) {
                              m_threadID = threadID;
                              } // end get/setThreadID
                            
                            
                              public String getThreadName() { return m_threadName; }
                              public void setThreadName(String threadName) {
                              m_threadName = threadName;
                              } // end get/setThreadName
                            
                            
                            
                              public String getPriority() { return m_priority; }
                              public void setPriority(String priority) {
                              m_priority = priority;
                              } // end get/setState
                            
                              public String getSeverity() { return m_severity; }
                              public void setSeverity(String severity) {
                              m_severity = severity;
                              } // end get/setState
                            
                            
                              public String getStatus() { return m_status; }
                              public void setStatus(String status) {
                              m_status = status;
                              } // end get/setStatus
                            
                            
                              public String getUrl() { return m_url; }
                              public void setUrl(String url) {
                              m_url = url;
                              } // end get/setURL
                            
                            
                            
                            }
                            
                  • Re: access java method from ftl
                    Jesse Booth

                    Do you have an init() method for your deployWidgetUtils?

                     

                    Also, are you sure thread being passed in javascript is not null? Also, try passing the thread ID rather than the thread object.

                     

                    In your deploy-widget.ftl, there needs to be a <@resource.javascript> after the <@resource.dwr file="DeployWidgetUtils"/>  where you can define a method, such as the following:

                     

                    var deployWidget = new Object();

                    deployWidget.getPriority = function(threadID) {

                         if (threadID != null) {

                              return DeployWidgetUtils.getPriority(threadID);

                         }

                    }

                      • Re: access java method from ftl
                        Jesse Booth

                        Also, you do not need DeployWidgetConstants deploy = new DeployWidgetConstants();  and this instantiate won't work.

                         

                        Just go with the generic Object(); for a javascript variable like above.

                          • Re: access java method from ftl
                            Jesse Booth

                            Also, in your FTL, it might be better to define your DWR in the <head></head>, for example:

                             

                            <head>

                            <@resource.dwr file="DeployWidgetUtils"/>

                            <@resource.javascript>

                                 define javascript functions here for validating parameters to be passed into DWR methods, then return results.

                            </@resource.javascript>

                            </head>

                            <body>

                            ...

                            call javascript functions here

                            ...

                            </body>

                          • Re: access java method from ftl
                            amyc

                            Not sure what you mean by init() method but nope, I don't have it in my class. Can you give me a snippet code perhaps?

                              • Re: access java method from ftl
                                Jesse Booth

                                In your DWR java class, be sure to have:

                                 

                                public void init() {

                                 

                                  } // end init - SPRING MANAGED

                                 

                                public void destroy() {

                                 

                                  } // end destroy - SPRING MANAGED

                                 

                                Not sure if this is actually your problem though.

                                 

                                You might also want to verify that in you are implementing the DWR calls correctly in the FTL by following the above example.

                                 

                                For example, this won't work:

                                1. <@resource.javascript> 
                                2.      DeployWidgetConstants deploy = new DeployWidgetConstants(); 
                                3. </@resource.javascript>