JSF中基于两个组件的组合的验证/转换 [英] Validation/conversion for two component based composite in JSF

查看:111
本文介绍了JSF中基于两个组件的组合的验证/转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个JSF Web应用程序,我需要在其中使用周期性作为数据结构.这里有我使用的Java类:

I'm developing a JSF web application where I need to use periodicities as data structure. Here there are the Java classes I work with:

public class Periodicity implements Serializable {

    private Integer value = 0;

    private PeriodicityType type;

    //Getter and setters

}

public enum PeriodicityType {
    DAY, WEEK, MONTH, YEAR
}

这样,我可以为我的任务指定不同的周期性,可以将值与PeriodicityType组合在一起.

That way I can specify different periodicities for my tasks, which can combine values with PeriodicityType.

我还创建了一个名为 periodicityInput.xhtml 的输入复合元素,我可以使用它以可重用的方式以不同的形式提供该数据类型:

I also have created an input composite element called periodicityInput.xhtml, which I can use to provide that data type in different forms in a reusable way:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:composite="http://java.sun.com/jsf/composite"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<h:body>
    <composite:interface>
        <composite:attribute name="value" required="true" />
        <composite:attribute name="disabled" required="false" default="false" />
    </composite:interface>

    <composite:implementation>
        <h:panelGrid columns="2" id="#{cc.id}">
            <p:spinner value="#{cc.attrs.value.value}" min="1"
                id="value_spinner" disabled="#{cc.attrs.disabled}" />
            <p:selectOneMenu value="#{cc.attrs.value.type}" style="width:200px"
                disabled="#{cc.attrs.disabled}">
                <f:selectItem noSelectionOption="true"
                    itemLabel="#{windowsMessages.NOT_ASSIGNED}" />
                <f:selectItems value="#{viewUtils.periodicityTypes}" />
            </p:selectOneMenu>
        </h:panelGrid>
    </composite:implementation>
</h:body>
</html>

基本上,我有一个spinner元素和一个selectOneMenu,以便为周期性选择一个值和一个类型.还有机会不选择任何类型,在这种情况下,输入的数值必须为零.或者,甚至更好的是,如果未选择任何周期性,则必须将输入转换为null/null元组.

Basically I have an spinner element and a selectOneMenu, in order to select a value and a type for the periodicity. There's also the chance not to select any type, in this case the input numeric value must be zero. Or, even better, if no periodicity selected the input must be converted into a null/null tuple.

正如我在某些网站中所阅读的可以一次通过验证方法中的id来验证多个JSF组件:

As I read in some sites there's the chance to validate multiple JSF components at once accesing the id in the validation method:

UIInput confirmComponent =(UIInput)component.getAttributes().get("confirm");

UIInput confirmComponent = (UIInput) component.getAttributes().get("confirm");

问题是,如何对其进行修改以便在没有固定ID的复合组件中使用?

Question is, how to adapt it in order to use in a composite component which doesn't have a fixed id?

推荐答案

一种方法是创建一个扩展UIInput的后备组件,并在UIInput#getSubmittedValue()中完成该工作.子输入仅由findComponent()直接可用.

One way would be to create a backing component extending UIInput and do the job in UIInput#getSubmittedValue(). The child inputs are just directly available by findComponent().

<cc:interface componentType="inputPeriodicity">
    ...
</cc:interface>
<cc:implementation>
    ...
    <p:spinner id="value_spinner" ... />
    <p:selectOneMenu id="type_menu" ... />
    ...
</cc:implementation>

使用

@FacesComponent("inputPeriodicity")
public class InputPeriodicity extends UIInput implements NamingContainer {

    @Override
    public String getFamily() {
        return UINamingContainer.COMPONENT_FAMILY;
    }

    @Override
    public Object getSubmittedValue() {
        UIInput typeMenu = (UIInput) findComponent("type_menu");
        String type = (String) typeMenu.getSubmittedValue();

        if (type == null || type.isEmpty()) {
            UIInput valueSpinner = (UIInput) findComponent("value_spinner");
            valueSpinner.setSubmittedValue("0"); // Don't use int/Integer here!
        }

        return super.getSubmittedValue();
    }

}

另请参见:

  • 拆分Java. util.Date超过两个h:inputText字段,它们分别用f:convertDateTime表示小时和分钟
  • See also:

    • Split java.util.Date over two h:inputText fields representing hour and minute with f:convertDateTime
    • 无关与具体问题无关,<h:panelGrid id="#{cc.id}">确实不正确.如果确实需要,请给它一个固定的ID.

      Unrelated to the concrete problem, the <h:panelGrid id="#{cc.id}"> really isn't right. Give it a fixed ID instead if you really need to.

      这篇关于JSF中基于两个组件的组合的验证/转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆