cancel
Showing results for 
Search instead for 
Did you mean: 

Java Web Script first steps

nm_santos
Champ in-the-making
Champ in-the-making
Hi everyone,

I've managed to complete the tutorial regarding java-based web scripts.

Now, I tried modifying the Javadir class so I could try out some things.

Here's how it looks now:


public class JavaDir extends DeclarativeWebScript
{
   
   protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
   {
      String user = null;
      
      user = req.getParameter("username");
      
            
      Map<String, Object> model = new HashMap<String, Object>();
      model.put("username", user);
      
      return model;
      
   }
}

Here are my descriptors and template files:

<webscript>
  <shortname>Folder Listing Utility</shortname>
  <description>Java-backed implementation of listing folder contents
  </description>
  <url>/javadir/username={username}</url>
  <authentication>user</authentication>
</webscript>

<html>
<head>
  <title>Folder java funciona</title>
  </head>
      ${username} funciona
</body>
</html>
All I want now, is to display the username received. (the URI is ttp://localhost:8080/alfresco/service/javadir/{username}


however, when I run the script, I get the following error:





500 Description:    An error inside the HTTP server which prevented it from fulfilling the request.

Message:   05060023 Wrapped Exception (with status template): 05060003 Error during processing of the template 'Expression username is undefined on line 5, column 19 in org/example/javadir.get.html.ftl.'. Please contact your system administrator.

Exception:   freemarker.core.InvalidReferenceException - Expression username is undefined on line 5, column 19 in org/example/javadir.get.html.ftl.


Which is in regards to ${username}.

What am I doing wrong?

Regards,
Nuno.
5 REPLIES 5

abarisone
Star Contributor
Star Contributor
Hi,
in your descriptor it's better to write
<webscript>
  <shortname>Folder Listing Utility</shortname>
  <description>Java-backed implementation of listing folder contents
  </description>
  <url>/javadir/username</url>
  <authentication>user</authentication>
</webscript>
and try to call the webscript with
http://localhost:8080/alfresco/service/javadir/username?username=Nuno
You also missed the <body> opening tag.

Regards

nm_santos
Champ in-the-making
Champ in-the-making
Hi,
in your descriptor it's better to write
<webscript>
  <shortname>Folder Listing Utility</shortname>
  <description>Java-backed implementation of listing folder contents
  </description>
  <url>/javadir/username</url>
  <authentication>user</authentication>
</webscript>
and try to call the webscript with
http://localhost:8080/alfresco/service/javadir/username?username=Nuno
You also missed the <body> opening tag.

Regards

Hi!

Thanks for your help Smiley Happy I managed to get that to work!

Now, I've tried changing some stuff again, namely using a POST Method.


Now, in my Java Script, I've got this:

protected Map<String, Object> executeImpl(WebScriptRequest req,
         Status status, Cache cache)
         {

      String content="testing";
      try {
         content = req.getContent().getContent();
         
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      Map<String, Object> model = new HashMap<String, Object>();
      model.put("content", content);

      return model;
         }

How can I output my variable content? I just want to make sure that it read the content correctly.

When I run the POST method in my application, I will get the same error I showed you earlier, that is:

———-
freemarker.core.InvalidReferenceException: Expression content is undefined on line 6, column 30 in javadir.post.html.ftl.

Here are my files:

response template:
<html>
<head>
  <title>Utilizador bomba </title>
  </head>
<body>
   o conteudo e o seguinte ${content}
</body>
</html>

descriptor:
<webscript>
  <shortname>Folder Listing Utility</shortname>
  <description>Java-backed implementation of listing folder contents
  </description>
  <url>/javadir</url>
  <authentication>user</authentication>
</webscript>

context file:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN 2.0//EN'
  'http://www.springframework.org/dtd/spring-beans-2.0.dtd'>
<beans>
  <bean id="webscript.org.example.javadir.post"
    class="org.example.JavaDir" parent="webscript">
  </bean>
</beans>
Am I not using the correct approach?

I don't understand how to work with freemarker files 😕

abarisone
Star Contributor
Star Contributor
Hi Nuno,
you'd better first take a look at these links in order to get acquainted with freemarker templates…
http://wiki.alfresco.com/wiki/FreeMarker_Template_Cookbook
http://wiki.alfresco.com/wiki/Template_Guide
http://freemarker.sourceforge.net/docs/index.html

Regards

nm_santos
Champ in-the-making
Champ in-the-making
Hi,

Having looked at the pages you gave me, I still find it not recognizing the variables.

In my Java class, when I do the following:

Map<String, Object> model = new HashMap<String, Object>();
      model.put("username", usss);
      model.put("testing", test);
      return model;

Shouldn't the freemarker template recognize ${username} and ${testing} ?

In this guide http://docs.alfresco.com/4.0/topic/com.alfresco.enterprise.doc/tasks/ws-folderListing-Java-scripting...

He does model.put("verbose",verbose") and verbose is recognized!

Regards,
Nuno.

abarisone
Star Contributor
Star Contributor
Hi Nuno,
I think that the problem resides into the fact that you are calling your POST script issuing a GET call.
Namely, when you call the link referring your script, the controller considers it as a HTTP GET and, in fact, it correctly passes the variable verbose that is specified in the url.
Alfresco gives you the opportunity of calling scripts with HTTP methods other than GET.
http://docs.alfresco.com/4.0/index.jsp?topic=%2Fcom.alfresco.enterprise.doc%2Fconcepts%2Fws-tunnelin...
So try to add to the querystring the fragment
&alf_method=POST
and look at the results.

Hope this helps.
Regards,
Andrea