我的应用程序中有一个自定义表单,我想将值加载到其字段中。
为此,我有一个从ItemListener扩展的类,并覆盖onUpdated方法以读取更新的配置文件,并通过创建Action对象将值设置到文本框字段,将该操作对象添加到项目并保存项目。出于某种原因,这不起作用。代码片段如下!
行动类:
public class MyAction<T extends AbstractItem> implements Action {
final private T item;
public String gitRepo;
public String gitBranch;
public String mavenGoals;
public String mavenPOM;
public String eMailRecipient;
@DataBoundConstructor
public MyAction(T item, String gitRepo, String gitBranch, String mavenGoals, String mavenPOM, String eMailRecipient) {
this.item = item;
this.gitRepo = gitRepo;
this.gitBranch = gitBranch;
this.mavenGoals = mavenGoals;
this.mavenPOM = mavenPOM;
this.eMailRecipient = eMailRecipient;
}
//Getter methods follow...
ItemListener类:
@Extension
public class MyActionListener extends ItemListener{
@Override
public void onUpdated(Item item){
if(item instanceof AbstractItem)
{
for(Action action : Jenkins.getInstance().getActions())
if(action.getClass()==MyAction.class)
{
AbstractProject proj=(AbstractProject) item;
XmlFile projConfig=proj.getConfigFile();
String config=projConfig.toString();
String gitRepo=config.substring(config.indexOf("<url>")+5, config.indexOf("</url>"));
String branch=config.substring(config.indexOf("<name>")+6,config.indexOf("</name>"));
String mavengoals=config.substring(config.indexOf("<target>")+8,config.indexOf("<target>"));
String mavenPom=config.substring(config.indexOf("<pom>")+5,config.indexOf("</pom>"));
String eMailReceipient=config.substring(config.indexOf("<recipientList>")+15,config.indexOf("<recipientList>"));
Action newAction=new MyAction((AbstractItem) item,gitRepo,branch,mavengoals,mavenPom,eMailReceipient);
proj.addAction(newAction);
try {
proj.save();
} catch (IOException ex) {
Logger.getLogger(MyActionListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
果冻形式:
<f:form method="post" name="config" action="configSubmit">
<f:block>
<f:section title="${%Job Configuration}">
<f:entry title="${%Job name}">
<f:textbox id="name" name="name" default="${it.ItemName}"/>
</f:entry>
</f:section>
<f:section title="${%Source Code Management}">
<f:entry title="${%Git Repo}">
<f:textbox id="repo" name="repo" default="git@github.scm.corp.mycompany.com:{Enter your git repo here}" value="${it.GitRepo}"/>
</f:entry>
<f:entry title="${%Branch}">
<f:textbox id="branch" name="branch" default="master" value="${it.GitBranch}"/>
</f:entry>
</f:section>
<f:section title="${%Maven Targets}">
<f:entry title="${%Goals}">
<f:textbox id="goals" name="goals" default="-U clean install" value="${it.MavenGoals}"/>
</f:entry>
<f:entry title="${%POM}">
<f:textbox id="pom" name="pom" default="pom.xml" value="${it.MavenPOM}"/>
</f:entry>
</f:section>
<f:section title="${%E-Mail Notification}">
<f:entry title="${%Recipient}">
<f:textbox id="emailrecipient" name="emailrecipient" default="QE DL For Notifying" value="${it.eMailRecipient}"/>
</f:entry>
</f:section>
<f:section title="${%Assembler Postbuild}">
<f:entry title="${%Assembler Target POM.xml{relative path}}">
<f:textbox id="assemblerpom" name="assemblerpom"/>
</f:entry>
</f:section>
</f:block>
<f:bottomButtonBar>
<f:submit value="${%Save}" />
</f:bottomButtonBar>
</f:form>