文字列入力

文字列入力コンポーネントは、ユーザーに簡単なテキスト入力を求めます。コンテンツに機密情報が含まれている場合は、オプションで値をマスクします。次のリストは例を示しています。

@ShellComponent
public class ComponentCommands extends AbstractShellComponent {

	@ShellMethod(key = "component string", value = "String input", group = "Components")
	public String stringInput(boolean mask) {
		StringInput component = new StringInput(getTerminal(), "Enter value", "myvalue");
		component.setResourceLoader(getResourceLoader());
		component.setTemplateExecutor(getTemplateExecutor());
		if (mask) {
			component.setMaskCharacter('*');
		}
		StringInputContext context = component.run(StringInputContext.empty());
		return "Got value " + context.getResultValue();
	}
}

次のスクリーンキャストは、文字列入力コンポーネントからの一般的な出力を示しています。

コンテキストオブジェクトは StringInputContext です。次の表に、そのコンテキスト変数を示します。

表 1: StringInputContext テンプレート変数
キー 説明

defaultValue

設定されている場合のデフォルト値。それ以外の場合は null。

maskedInput

マスクされた入力値

maskedResultValue

マスクされた結果値

maskCharacter

マスク文字 (設定されている場合)。それ以外の場合は null。

hasMaskCharacter

true if a mask character is set. Otherwise, false.

model

親コンテキスト変数 ( TextComponentContext テンプレート変数を参照)。