Connect jelly and java

68 views
Skip to first unread message

jenkin...@gmail.com

unread,
Sep 3, 2014, 4:45:00 PM9/3/14
to jenkin...@googlegroups.com
Hi all,

I am currently developing my first Jenkins plugin.
I need to have a dropdown menu on the job page that is filled via a java method, but the jelly and the java file don't seem to work properly together: There is just an empty dropdown list.
What could be wrong?

jobMain.jelly

<j:jelly
 
xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define"
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

    <f:entry field="selection" title="Choose">
        <f:select />
    </f:entry>
</j:jelly>


JavaClass.java

public class JavaClass implements Action {


private AbstractProject ap;


public JavaClass(AbstractProject ap) {

    this.ap = ap;

}


public String getIconFileName() {

    return null;

}


public String getDisplayName() {

    return "";

}


public String getUrlName() {

    return "something";

}


@Extension

public static final class DescriptorImpl extends TransientProjectActionFactory {


    String selection;


    public DescriptorImpl() throws IOException {

    }


    @DataBoundConstructor

    public DescriptorImpl(String selection) {

        this.selection = selection;

    }


    public ListBoxModel doFillSelectionItems() throws IOException {

        ListBoxModel model = new ListBoxModel();

        model.add("test");

        return model;

    }


    @Override

    public Collection<? extends Action> createFor(AbstractProject target) {

        return Arrays.asList(new JavaClass(target));

    }

  }

}        


Ulli Hafner

unread,
Sep 4, 2014, 6:39:35 AM9/4/14
to jenkin...@googlegroups.com
This field should be in your JavaClass class



    public DescriptorImpl() throws IOException {

    }



No constructor required

    @DataBoundConstructor

    public DescriptorImpl(String selection) {

        this.selection = selection;

    }


This DataBoundConstructor constructor should be in your JavaClass class


    public ListBoxModel doFillSelectionItems() throws IOException {

        ListBoxModel model = new ListBoxModel();

        model.add("test");

        return model;

    }


    @Override

    public Collection<? extends Action> createFor(AbstractProject target) {

        return Arrays.asList(new JavaClass(target));

    }

  }

}        



--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

jenkin...@gmail.com

unread,
Sep 4, 2014, 2:26:20 PM9/4/14
to jenkin...@googlegroups.com
Thanks for replying!
So now the code looks like this, right?

public class JavaClass implements Action {
 
private AbstractProject ap;
public String selection;

 
public JavaClass(AbstractProject ap) {
   
this.ap = ap;
}
 
@DataBoundConstructor
public JavaClass(String selection) {
       
this.selection = selection;
}
 
public String getIconFileName() {
   
return null;
}
 
public String getDisplayName() {
   
return "";
}
 
public String getUrlName() {
   
return "something";
}
 
@Extension
public static final class DescriptorImpl extends TransientProjectActionFactory {

 
   
public ListBoxModel doFillSelectionItems() throws IOException {

       
ListBoxModel model = new ListBoxModel();
        model
.add("test");
       
return model;
   
}
 
   
@Override
   
public Collection<? extends Action> createFor(AbstractProject target) {
       
return Arrays.asList(new JavaClass(target));
   
}
 
}
}


However the dropdown list is still empty. The doFill method is never called.
Do you have another hint?

Mads Nielsen

unread,
Sep 4, 2014, 2:42:19 PM9/4/14
to jenkin...@googlegroups.com

I dont think an action is a describable object? So i dont think tour approach will work.

Jenkins User

unread,
Sep 5, 2014, 5:51:31 AM9/5/14
to jenkin...@googlegroups.com
Ah, ok.. does that mean my class has to implement the Describable interface?
I'm really not yet familiar with all those Jenkins classes and interfaces :/
This "implements Action" and "extends TransientProjectActionFactory" combination is something I copied from another plugin because I knew it would accept the jobMain.jelly for the job page. Is there a solution that can fill the dropdown as well?
> You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/L8EO8GkZoaE/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.

Robert Sandell

unread,
Sep 5, 2014, 10:52:25 AM9/5/14
to jenkin...@googlegroups.com
Well the project main page is not a place that is prepared to do any form bindings for you since its not a config page.

You should just need to create a normal html select in your jobMain.jelly and fill it in whatever "static" way you like.

<select name="mySelect">
 <option value="test">Test</option>
</select>

The "it" var in jelly is a reference to your Action instance, so if you implement a method in that you could do something like the following:

<select name="mySelect">
 <j:forEach var="v" items="${it.getMyData()}">
   <option value="${v.value}">${v.name}</option>
 </j:forEach>
</select>

Just like we did all those years ago in jsp and php ;)

/B
Robert Sandell
Software Engineer
CloudBees Inc.
Reply all
Reply to author
Forward
0 new messages