@DataBoundConstructor with object in constructor

158 views
Skip to first unread message

Ludovic Roucoux

unread,
Apr 8, 2015, 6:21:48 AM4/8/15
to jenkin...@googlegroups.com
Hi,

I develop a build step plugin and I try to bind an object composed of an other object. Here it's what i have :

public class ZAProxyBuilder extends Builder {
private final ZAProxy zaproxy;
private final boolean startZAPFirst;

// Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
@DataBoundConstructor
public ZAProxyBuilder(boolean startZAPFirst, ZAProxy zaproxy) {
this.startZAPFirst = startZAPFirst;
this.zaproxy = zaproxy;
}
 
public boolean getStartZAPFirst() {
return startZAPFirst;
}
public ZAProxy getZaproxy() {
return zaproxy;
}  
}

public class ZAProxy {
private final String targetURL;
private final boolean spiderURL;

@DataBoundConstructor
public ZAProxy(String targetURL, boolean spiderURL) {
this.targetURL = targetURL;
this.spiderURL = spiderURL;
}
 
public String getTargetURL() {
return targetURL;
}
public boolean getSpiderURL() {
return spiderURL;
}
}

And my config.jelly in src/main/resources/ZAProxyBuilder
<?jelly escape-by-default='true'?>
<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:optionalBlock title="${%Start ZAProxy in a pre-build step}" field="startZAPFirst" inline="true" />

<f:entry title="${%Target URL}" >
<f:textbox name="zaproxy.targetURL" value="${instance.zaproxy.targetURL}" clazz="required" />
</f:entry>
 
<f:optionalBlock title="${%Spider URL}" field="${instance.zaproxy.spiderURL}" inline="true" />
</j:jelly> 


The value of startZAPFirst is saved but not the values of the ZAProxy class. The constrcutor of ZAProxy is never called.
 I also try to write a second config.jelly in src/main/resources/ZAProxy and include this file to the config.jelly in src/main/resources/ZAProxyBuilder like this :
<st:include page="config.jelly" class="${ZAProxy}"/>
but without success.

Anyone can help me ?

Regards,
Ludovic.

Vojtech Juranek

unread,
Apr 8, 2015, 8:37:07 AM4/8/15
to jenkin...@googlegroups.com
On Wednesday 08 April 2015 03:21:48 Ludovic Roucoux wrote:
> *<st:include page="config.jelly" class="${ZAProxy}"/>*
> but without success.

have you tried fully-qualified class name, e.g.

<st:include page="config.jelly" class="org.jenkinsci.plugins.zap.ZAProxy"/>

? This IMHO should work.

HTH
Vojta
signature.asc

Stephen Connolly

unread,
Apr 8, 2015, 9:13:01 AM4/8/15
to jenkin...@googlegroups.com
You typically want the class you are instatiating to also be a Describable with a Descriptor, so what you want is

public class ZAProxy extends AbstractDescribableImpl<ZAProxy> {

...

  @Extension
  public static class DescriptorImpl extends Descriptor<ZAProxy> {
    public String getDisplayName() { return null; }
  }
}

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/9135d836-28e0-4e79-93d9-9c4ecea4fdcb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ludovic Roucoux

unread,
Apr 8, 2015, 9:16:57 AM4/8/15
to jenkin...@googlegroups.com
Oh yes, I have already tested that also, but my ZAProxy constructor is never called and so my values are never saved. In the second config.jelly (in src/main/resources/ZAProxy), here what I have :

<?jelly escape-by-default='true'?>
<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 title="${%Target URL}" field="targetURL">
<f:textbox clazz="required" />
</f:entry>

<f:optionalBlock title="${%Spider URL}" field="spiderURL" inline="true" />

</j:jelly>

Is it the good way (field value) to save values into my ZAProxy class ?

Ludovic Roucoux

unread,
Apr 8, 2015, 9:26:02 AM4/8/15
to jenkin...@googlegroups.com
Stephen, I tested your code but without more success. The ZAProxyBuilder constructor is called correctly but not the ZAProxy constructor, so my zaproxy in ZAProxyBuilder is always null.

Ludovic Roucoux

unread,
Apr 8, 2015, 5:00:44 PM4/8/15
to jenkin...@googlegroups.com
I don't understand why a ZAProxy instance is not created. Anyone can explain me the @DataBoundConstructor working ?

Ludovic Roucoux

unread,
Apr 9, 2015, 10:55:47 AM4/9/15
to jenkin...@googlegroups.com
Finally, I found a solution with the code of Stephen and some modifications in jelly files.

So, I have two class ZAProxyBuilder and ZAProxy with two config.jelly respectively.

In the first config.jelly file (i.e in the ZAProxyBuilder package), I include the other file with the following code :

<f:property field="zaproxy" />

where zaproxy is the name of my member in ZAProxyBuilder class. And, in this second config.jelly, I fill the field normally (i.e with the name of member in ZAProxy class).

It works perfectly but I don't understand why the following code :

<st:include page="config.jelly" class="fr.novia.zapjenkinsplugin.ZAProxy"/>

doesn't work. This code includes correctly the second config.jelly file but it doesn't create a new instance of ZAProxy class. If anyone have an answer of this problem, i'm interested ;)

Thank you guys for your help.

Regards,
Ludovic.

Jesse Glick

unread,
Apr 9, 2015, 11:23:16 AM4/9/15
to Jenkins Dev
On Thu, Apr 9, 2015 at 10:55 AM, Ludovic Roucoux
<ludovic...@gmail.com> wrote:
> Finally, I found a solution with the code of Stephen

For the future go straight to ui-samples plugin, which contains
complete working examples of these kinds of things. Not f:property,
yet; file a PR to add such a sample.

> I don't understand why the following code :
>
> <st:include page="config.jelly" class="fr.novia.zapjenkinsplugin.ZAProxy"/>
>
> doesn't work. This code includes correctly the second config.jelly file but
> it doesn't create a new instance of ZAProxy class.

Under the hood f:property would be doing additional stuff, in
particular setting the `descriptor` and `instance` variables.

Christian McHugh

unread,
Oct 29, 2016, 7:52:27 PM10/29/16
to Jenkins Developers
Okey dokie, I think I'm nearly out of the woods. I can now create a pipeline job. Unfortunately, I also have a problem with a descriptor constructor. With this new code, we are now seeing
errors calling the new subclass descripters. This output is shown in:

If anyone is able to help with this problem, it would be appreciated. 


Thank you very much!
Reply all
Reply to author
Forward
0 new messages