Jira and Velocity - form field

b March 28, 2012

Hi,

I'm newbie in JIRA topic, I trying to create simple form.

I have following class

public class ManageTemplates extends JiraWebActionSupport {

	public ManageTemplates(FieldScreenRendererFactory fieldScreenRendererFactory,
			JiraAuthenticationContext jiraAuthenticationContext) {
		
		String issueId = ((String[])ActionContext.getParameters().get("id"))[0];
		MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject(Long.valueOf(issueId));
		
		
	}
}

Class is connected with .vm file via atlassian-plugin.xml config file. Here's my .vm

<html>
<head>
	<meta name="decorator" content="atl.admin"/>
    <title>Create X</title>
</head>
<body class="nl">
	<div class="content intform">
	</div>
</body>
</html>

How could I insert user picker to form? Any code examples?

1 answer

1 accepted

4 votes
Answer accepted
Radek Kantor
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 28, 2012

Hi,

I do not knowv how to reuse standard user picker - you must check source code, but why you read parameter 'id' in so complicated way. You should define property id + getter/setter in action class and set it in request as some hidden form field or directly in url.

private String issueId;

public String getIssueId() {
  return issueId;
}
	
public void setIssueId(String issueId) {
  this.issueId = issueId;
}

Also you can define other methods in action (not only overwrite execute), eg. to list all JIRA users

public Collection<User> getAllUsers() {
  return UserUtils.getAllUsers();
}

Than iterate throught results in vm tempate and create eg. select element.

<select id="jiraUser" name="jiraUser">
#foreach ($user in $action.getAllUsers())
  <option value="$user.name">$user.displayName</option>
#end
</select>
Bahar Çağlar October 11, 2012

Hi Radek,

I wanna ask a question to you. action is the class name or not? I guess it is not :) If so, how can I set a class as action?

Thanks

Radek Kantor
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 13, 2012

It is not class name. You can use notation as $action.someMethodCall() in any vm template, where action is mapped into the class defined for your webwork in atlassian-plugin.xml

Example:

1. atlassian-plugin.xml

<webwork1 key="action-webwork" name="Test Action module" class="java.lang.Object">
		<description key="test.action.webwork.desc">Action used to demonstrate test example.</description>
		<actions>
			<action name="com.mycompany.jira.plugin.test.web.actions.TestAction"
				alias="TestAction">
				<view name="error">/template/plugin/webwork/testaction.vm</view>
				<view name="input">/template/plugin/webwork/testaction.vm</view>
			</action>
		</actions>
	</webwork1>

2. Some parts of vm template "testaction.vm" mapped to action class TestAction.java

#set($names = $action.getNames())
#set($counter = $names.size())
#if($counter > 0)
	#set( $i = 1 )
	#foreach( $name in $names )
		<tr>
			<td class="fieldLabelArea">
				$i18n.getText("name.action.label")
				<span class="icon icon-required"><span>$i18n.getText("general.field.required")</span></span>
			</td>
			<td class="fieldValueArea">
				<input type="text" id="name_$i" value="$!name">
			</td>  
		</tr>
		#set($i = $i + 1)
	#end
#end

3. TestAction.java

public class TestAction extends JiraWebActionSupport {

	private String nameList;
	private TestService testService;
	
	public TestAction(TestService testService) {
		this.testService = testService;
	}

	@Override
	protected void doValidation() {
		super.doValidation();
		// Some other TestAction validation
	}

	@Override
	@RequiresXsrfCheck
	protected String doExecute() throws Exception {
		if (hasAnyErrors())return ERROR;
		// Some execute TestAction code
		return INPUT;
	}

	public String getNameList() {
		return nameList;
	}
	
	public void setNameList(String nameList) {
		this.nameList = nameList;
	}
	
	public List<String> getNames() {
		return testService.getHelper().tokenize(nameList, "@@");
	}

}

Suggest an answer

Log in or Sign up to answer