cancel
Showing results for 
Search instead for 
Did you mean: 

Java Backed Webscript: no model object in ftl template

mapubox
Champ in-the-making
Champ in-the-making
Hi,
I'm a newbie with Alfresco and webscript and I cant figure what I make wrong in my implementation.
After much seeking in the wiki and in the forum, you're my last hope Smiley Happy
I wrote a webscript with a java controller which returns a "model" object but my ftl template can't access it and returns "undefined expression".
I'm sure, through some logging, that the webscript executes and the java controller works, but the model (declared as: Map<String, Object> model = new HashMap<String, Object>() and returned by the executeImpl method of a class which extends org.alfresco.web.scripts.DeclarativeWebScript )  doesn't seem to be transmitted to the ftl template.
Any idea?

Here is some snapshots of my code:
constrval.get.desc.xml in alfresco/WEB-INF/classes/alfresco/templates/webscripts/mydomaintree/myapp/:
<?xml version="1.0" encoding="UTF-8"?>
<webscript>
   <shortname>Constrain Values</shortname>
   <description>Constraint Values</description>
   <url>/mydomain/myapp/constrval?prop={prop}</url>
   <url>/mydomain/myapp/constrval.xml?prop={prop}</url>
   <format default="html">any</format>
   <authentication>user</authentication>
   <transaction>requiresnew</transaction>
</webscript>
constrval.get.html.ftl in alfresco/WEB-INF/classes/alfresco/templates/webscripts/mydomaintree/myapp/:
<html>
   <body>
      <h1>Constraint values:</h1>
      <p>
         <#if model.validValues??>
            <#list model.validValues as value>
               ${value} <br/>
            </#list>
         </#if>
                </p>
                ${model.status}
   </body>
</html>
mydomain-scripts-context.xml in tomcat/shared/classes/alfresco/extension/:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
    <bean id="webscript.mydomaintree.myapp.constrval.get" class="mydomaintree.myapp.scripts.ConstraintValuesList" parent="webscript">
       <property name="dictionaryService">
          <ref bean="DictionaryService" />       
        </property>
    </bean>
</beans>
ConstraintValuesList.class (snapshot) in alfresco/WEB-INF/classes/mydomaintree/myapp/scripts/:
public class ListaValoriValidi extends DeclarativeWebScript {
   private DictionaryService dictionaryService;
   
   @Override
   protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
      
      Logger logger = Logger.getLogger(ConstraintValuesList.class);
      Map<String, Object> model = new HashMap<String, Object>();
      logger.debug("model created");
       String propName = req.getParameter("prop");
      
      if (propName==null) {
         model.put("status","Error");
         logger.debug("Proprierty null");
         return model;
      }
      logger.debug("proprietà corretta");
      QName theProperty;
      try {
         theProperty = QName.createQName("mydomain",propName);
      } catch (org.alfresco.service.namespace.InvalidQNameException e) {
         logger.debug("qname creation error");
         return model;
      }
      logger.debug("qname correct");
      ListOfValuesConstraint lovConstraint = null;
       PropertyDefinition propertyDef = null;
      
       // get the property definition for the item
       propertyDef = dictionaryService.getProperty(theProperty);

       if (propertyDef != null) {
           List<ConstraintDefinition> constraints = propertyDef
                   .getConstraints();

           for (ConstraintDefinition constraintDef : constraints) {
               Constraint constraint = constraintDef.getConstraint();

               if (constraint instanceof ListOfValuesConstraint) {
                   lovConstraint = (ListOfValuesConstraint) constraint;
                   break;
               }
           }
       } else {
          logger.debug("Proprierty doesn't exist");
          model.put("status","Error");
          return model;
       }
       if (lovConstraint!=null) {
          model.put("status", "Success");
          model.put("validValues", lovConstraint.getAllowedValues());
          logger.debug("Constraint Values returned");
       } else {
          model.put("status","Error");
          logger.debug("No constraint");
       }
       return model;
   }
2 REPLIES 2

lyamamot
Champ in-the-making
Champ in-the-making
I haven't worked with Java-backed web scripts (only JavaScript-backed web scripts) but typically in an FTL, you don't need to specify the "model" name. All the values put into the model are directly accessible. In your example FTL, you would use "<#if validValues??>" and so on.

mapubox
Champ in-the-making
Champ in-the-making
Now, I'm ashamed! What a  stupid mistake!..
I'haven't tried yet, but I'm sure the problem is there.
Anyway, thank you very much..