如何将config.jelly上的复选框与Builder类绑定?

时间:2014-04-18 04:03:41

标签: java jenkins jenkins-plugins jelly

这是基于HelloWorldBuilder的构建器类。

public class LogInfoBuilder extends Builder {
    private final TimerSettings settings = new TimerSettings();

    private final List<String> infoCollection = new ArrayList<String>();

    // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
    @DataBoundConstructor
    public LogInfoBuilder(String key, boolean isStart) {
        settings.setKey(key);
        settings.setIsStart(isStart);
    }

    /**
     * We'll use this from the <tt>config.jelly</tt>.
     */
    public String getKey() {
        return settings.getKey();
    }

    public boolean isStart()
    {
        return settings.getIsStart();
    }

    ...

这是config.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">
  <!--
    This jelly script is used for per-project configuration.

    See global.jelly for a general discussion about jelly script.
  -->

  <!--
    Creates a text field that shows the value of the "name" property.
    When submitted, it will be passed to the corresponding constructor parameter.
  -->
  <f:entry title="Key" field="key">
    <f:textbox />
  </f:entry>

  <!--
  <f:entry title="Start?" field="isstart">
    <select name="isStart">
      <option value="true" selected="${it.isstart}">Yes</option>
      <option value="false" selected="${!it.isstart}">No!</option>
    </select>
  </f:entry>
  -->

  <f:entry title="Starting point?" description="If checked, this will be the starting point.">
    <f:checkbox name="start" checked="${it.start}"/>
  </f:entry>
</j:jelly>

该复选框显示在作业配置页面上但我无法设置它的值,我的意思是选中或取消选中该页面上的复选框不会影响构建器类中的值。

这是配置页面,ui正确呈现。 enter image description here

但是输出不是我所期望的,即使我选中复选框,也总是falseenter image description here

我在构建器和/或果冻文件中做错了什么?

2 个答案:

答案 0 :(得分:3)

你似乎有两个复选框?或者您正试图在果冻复选框旁边创建自己的复选框。

您在config.jelly

中有这整个部分
<f:entry title="Start?" field="isstart">
<select name="isStart">
  <option value="true" selected="${it.isstart}">Yes</option>
  <option value="false" selected="${!it.isstart}">No!</option>
</select>
</f:entry>

但这甚至不会出现在用户界面上。


这应该适合您:将config.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">
<!--
   This jelly script is used for per-project configuration.

   See global.jelly for a general discussion about jelly script.
-->

<!--
   Creates a text field that shows the value of the "name" property.
   When submitted, it will be passed to the corresponding constructor parameter.
-->
<f:entry title="Key" field="key">
  <f:textbox />
</f:entry>

<f:entry title="Starting point?" field="start" description="If checked, this will be the starting point.">
  <f:checkbox/>
</f:entry>
</j:jelly>

如您所见,我删除了中间的无用部分,并更改了您定义复选框的方式。

然后将LogInfoBuilder更改为:

public class LogInfoBuilder extends Builder {
    private final TimerSettings settings = new TimerSettings();

    private final List<String> infoCollection = new ArrayList<String>();

    // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
    @DataBoundConstructor
    public LogInfoBuilder(String key, boolean start) {
        settings.setKey(key);
        settings.setIsStart(start);
    }

    /**
     * We'll use this from the <tt>config.jelly</tt>.
     */
    public String getKey() {
        return settings.getKey();
    }

    public boolean isStart()
    {
        return settings.getIsStart();
    }

    ...

这些更改只包括更改变量的名称以适合config.jelly中给出的名称。

答案 1 :(得分:0)

关键是使用“field”属性。在&lt; f:entry&gt;中或&lt; f:复选框&gt;

<f:entry title="Starting point?" description="...">
  <f:checkbox field="start" />
</f:entry>

or
<f:entry title="Starting point?" field="start" description="...">
  <f:checkbox />
</f:entry>