简体   繁体   English

我如何在wicket框架中使用html5颜色输入类型

[英]How do I use html5 Color input type in wicket framework

In the wicket application, I want to apply some style and color in my form. 在检票口应用程序中,我想在表单中使用一些样式和颜色。 So for that I want to use < input wicket:id="fillColorField" class="input" type="color"/> instead of input wicket:id="fillColorField" class="input" type="text"/> in .html file. 因此,我想使用< input wicket:id="fillColorField" class="input" type="color"/>代替input wicket:id="fillColorField" class="input" type="text"/>在.html文件中。

Is there any attribute or element for html5 Color input type in the wicket framework like for type= "text" we have TextField<~> ? wicket框架中是否有html5颜色输入类型的属性或元素,例如type= "text"我们有TextField<~>吗?

If there is no such attribute then please suggest me the alternative way to do this but it should be html5 color input type, not others like javascript color picker, etc. 如果没有这样的属性,那么请建议我执行此操作的另一种方法,但是它应该是html5颜色输入类型,而不是javascript颜色选择器等其他类型。

Color as Hex-String 颜色为十六进制字符串

The simplest way is to use a normal TextField<String> and overwrite the getInputTypes method which tells wicket to accept your type="color" in your HTML for the input. 最简单的方法是使用普通的TextField<String>并覆盖getInputTypes方法,该方法告诉wicket在HTML中接受您的type="color"作为输入。

IModel<String> colorModel = new Model<>();
queue(new TextField<String>("colorpicker", colorModel) {
            @Override
            protected String[] getInputTypes() {
                return new String[] {"color"};
            }
        });
queue(new Label("colorlabel", colorModel));

With this you will have the string representation of the color in your model, like the browser sends it, so for example #FF33AA . 这样,您将在模型中具有颜色的字符串表示形式,就像浏览器发送颜色一样,例如#FF33AA

You might also want to add a validator or some other safeguard to the TextField which checks the format, as it's not guaranteed that the submitted value will always be a nicely formatted color hex-string (browsers not supporting type="color" , a malicious user sending arbitrary values, etc.)! 您可能还想向TextField添加一个验证器或其他保护措施,以检查格式,因为不能保证提交的值始终是格式正确的彩色十六进制字符串(浏览器不支持type="color" ,这是恶意的用户发送任意值等)!


Color as Java Object 颜色作为Java对象

Additionally you could also register a custom IConverter in your wicket Application that converts the String value to something more useful, for example a java.awt.Color object, which would give you direct access to the RGB values. 此外,您还可以在检票口Application中注册一个自定义IConverter ,它将String值转换为更有用的内容,例如java.awt.Color对象,该对象将使您可以直接访问RGB值。

For this add a new converter for the Color class in your wicket Application class by overwriting the Application#newConverterLocator() method: 为此,通过重写Application#newConverterLocator()方法,在您的wicket Application类中为Color类添加一个新的转换器:

@Override
protected IConverterLocator newConverterLocator() {
    ConverterLocator defaultLocator = new ConverterLocator();

    defaultLocator.set(Color.class, new IConverter<Color>() { 
        @Override
        public Color convertToObject(String value, Locale locale) throws ConversionException {
            try {
                return Color.decode(value);
            } catch (NumberFormatException e) {
                throw new ConversionException("Illegal color format", e);
            }
        }

        @Override
        public String convertToString(Color value, Locale locale) {
            return String.format("#%02x%02x%02x", value.getRed(), value.getGreen(), value.getBlue());
        }
    });

    return defaultLocator;
}

And then use a Model with the Color class instead of String. 然后使用带有Color类而不是String的Model。

IModel<Color> colorModel = new Model<>();
queue(new TextField<Color>("colorpicker", colorModel, Color.class) {
            @Override
            protected String[] getInputTypes() {
                return new String[] {"color"};
            }
        });
queue(new Label("red", colorModel.map(Color::getRed)));
queue(new Label("green", colorModel.map(Color::getGreen)));
queue(new Label("blue", colorModel.map(Color::getBlue)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM