Read a property of an arbitrary CmsResource

(Difference between revisions)
Jump to: navigation, search
(fixed "requestContext cannot be resolved")
Line 2: Line 2:
  
 
<source lang="java">
 
<source lang="java">
   <%@page import="org.opencms.file.CmsProperty,org.opencms.file.CmsObject,org.opencms.jsp.CmsJspActionElement"%>
+
   <%@page import="org.opencms.file.CmsProperty"%>
 
   <%
 
   <%
 
       CmsJspActionElement cmsAction    = new CmsJspActionElement (pageContext, request, response);
 
       CmsJspActionElement cmsAction    = new CmsJspActionElement (pageContext, request, response);
Line 8: Line 8:
 
        
 
        
 
       // Try to find the "prop_name" property of the current file and, if it is not found there, try to find it on all parent folders:
 
       // Try to find the "prop_name" property of the current file and, if it is not found there, try to find it on all parent folders:
      // the first parameter of readPropertyObject must be a CmsRecource
 
 
       boolean search = true;
 
       boolean search = true;
       CmsProperty prop = cmsObject.readPropertyObject("/sites/mysite/my_path/file.ext", "prop_name", search);       
+
       CmsProperty prop = cms.readPropertyObject("/sites/mysite/my_path/file.ext", "prop_name", search);       
 
       String propValue = (prop != null) ? prop.getValue() : null;
 
       String propValue = (prop != null) ? prop.getValue() : null;
  
 
     // ... or get the title propery ...
 
     // ... or get the title propery ...
     CmsProperty titleProp = cmsObject.readPropertyObject (requestContext.getUri(), "Title", false);
+
     CmsProperty titleProp = cmsObject.readPropertyObject (cmsAction.getRequestContext.getUri(), "Title", false);
 
     String title = (titleProp != null) ? titleProp.getValue() : null;
 
     String title = (titleProp != null) ? titleProp.getValue() : null;
 
   %>
 
   %>

Revision as of 17:59, 9 April 2015

With scriptlets, you can read the properties of any opencms resource.

   <%@page import="org.opencms.file.CmsProperty"%>
   <%
      CmsJspActionElement cmsAction    = new CmsJspActionElement (pageContext, request, response);
      CmsObject cmsObject              = cmsAction.getCmsObject();
 
      // Try to find the "prop_name" property of the current file and, if it is not found there, try to find it on all parent folders:
      boolean search = true;
      CmsProperty prop = cms.readPropertyObject("/sites/mysite/my_path/file.ext", "prop_name", search);      
      String propValue = (prop != null) ? prop.getValue() : null;
 
     // ... or get the title propery ...
     CmsProperty titleProp = cmsObject.readPropertyObject (cmsAction.getRequestContext.getUri(), "Title", false);
     String title = (titleProp != null) ? titleProp.getValue() : null;
   %>
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox