String Input

The string input component asks a user for simple text input, optionally masking values if the content contains something sensitive. The input can also be required (at least 1 char).
The following listing shows an example:spring-doc.cn

public class ComponentCommands {

	@Command(name = "component string", description = "String input", group = "Components")
	public String stringInput(boolean mask) {
		StringInput component = new StringInput(getTerminal(), "Enter value", "myvalue");
		ResourceLoader resourceLoader = null; // getResourceLoader();
		TemplateExecutor templateExecutor = null; // getTemplateExecutor();
		component.setResourceLoader(resourceLoader);
		component.setTemplateExecutor(templateExecutor);
		if (mask) {
			component.setMaskCharacter('*');
		}
		StringInputContext context = component.run(StringInputContext.empty());
		return "Got value " + context.getResultValue();
	}

}

The following screencast shows typical output from a string input component:spring-doc.cn

The context object is StringInputContext. The following table lists its context variables:spring-doc.cn

Table 1. StringInputContext Template Variables
Key Description

defaultValuespring-doc.cn

The default value, if set. Otherwise, null.spring-doc.cn

maskedInputspring-doc.cn

The masked input valuespring-doc.cn

maskedResultValuespring-doc.cn

The masked result valuespring-doc.cn

maskCharacterspring-doc.cn

The mask character, if set. Otherwise, null.spring-doc.cn

hasMaskCharacterspring-doc.cn

true if a mask character is set. Otherwise, false.spring-doc.cn

requiredspring-doc.cn

true if the input is required. Otherwise, false.spring-doc.cn

modelspring-doc.cn

The parent context variables (see TextComponentContext Template Variables).spring-doc.cn