java - 如何使用 Builder 类绑定(bind) config.jelly 上的复选框?

标签 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

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

最佳答案

你好像有两个复选框??或者您正在尝试在果冻复选框旁边制作自己的复选框。

整个部分都在 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 中给出的名称。

关于java - 如何使用 Builder 类绑定(bind) config.jelly 上的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23147180/

相关文章:

java - 如何识别实现未使用?

java - 我如何在 Java 应用程序中知道我的 UNIX 操作系统是否具有 GUI 层,或者它只是命令行?

java - Android 将字符串转换为类

jenkins - Jenkins:在全局环境部分中使用withCredentials

Jenkins 管道: No such DSL method

java - 是 JavaCE 中的 RESTful Web 服务器

java - 在 Jenkins 中创建构建作业时如何调用项目

git - EC2/AWS、 Jenkins 、Git、SSH

java - Jenkins的依赖性检查间歇性失败

plugins - 如何使用 Jenkins 插件 Email-ext 将构建日志附加到电子邮件通知?