<h:form>里面的<f:ajax>的奇怪行为<ui:repeat> </ui:repeat> </h:form> </f:ajax>

时间:2012-05-13 09:32:07

标签: ajax jsf-2 jboss-weld hibernate-validator uirepeat

我有一个ui表示的数据表:重复。因为我希望用户能够基于每行更改数据,所以每行都包含在h:表单中。最后每个h:form都有一个带有f:ajax标签的按钮。我的行为非常不稳定。

<ui:repeat value="#{importManager.items}" var="item" varStatus="status">
    <h:form>
        <tr>
            <td>
                <h:outputText value="#{status.index}"/>
            </td>
            <td>
                <h:inputText id="title" value="#{item.title}" styleClass="#{item.validTitle ? 'valid' : 'invalid'}"/>
            </td>
            <td>
                <h:inputText id="artist" value="#{item.artist}" styleClass="#{item.validArtist ? 'valid' : 'invalid'}"/>
            </td>
            <td>
                <h:commandButton value="#{importMessages.submit}">
                    <f:ajax execute="@form" render="@all" listener="#{importManager.publish(item)}"/>
                </h:commandButton>
            </td>                           
        </tr>
    </h:form>
</ui:repeat>

上述工作但显然在带宽上并不便宜。

如果我将render =“@ all”更改为render =“@ form”,Firebug会显示正在发送的部分响应,但我的浏览器(Firefox)神秘地不会显示它。所以我猜它(浏览器)找不到要更新的元素?

如果我将execute =“@ form”改为execute =“@ all”,我会得到非常奇怪的行为,即数据丢失,受影响的字段变为空白。

支持bean非常简单:

    public void publish(final Item item)
    {
        Set<ConstraintViolation<Item>> violations = item.validate();

        if (violations.isEmpty())
        {
            temporaryRegistry.deleteItem(item);
            registry.storeItem(item);
        }
        else
        {
            // Display error messages
        }
    }

模特:

@Entity
public class Item implements Cloneable
{
    @Id @GeneratedValue
    private long identifier;

    @NotNull(groups={Warning.class})
    @Length(min=1, max=80, groups={Warning.class})
    private String title;

    @NotNull(groups={Warning.class})
    @Length(min=1, max=80, groups={Warning.class})  
    private String artist;

    @NotNull(groups={Warning.class})
    @Length(min=1, max=10, groups={Warning.class})
    private String media;

    @NotNull(groups={Warning.class})
    @Length(min=1, max=5, groups={Warning.class})
    @Column(name = "_condition")
    private String condition;

// Setters and Getters

    public boolean isValidTitle()
    {
        final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        final Validator validator = factory.getValidator();

        final Set<ConstraintViolation<Item>> violations = validator.validateProperty(this, "title", Warning.class);

        return violations.isEmpty();        
    }

    public boolean isValidCondition()
    {
        final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        final Validator validator = factory.getValidator();

        final Set<ConstraintViolation<Item>> violations = validator.validateProperty(this, "condition", Warning.class);

        return violations.isEmpty();        
    }

    public boolean isValidArtist()
    {
        final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        final Validator validator = factory.getValidator();

        final Set<ConstraintViolation<Item>> violations = validator.validateProperty(this, "artist", Warning.class);

        return violations.isEmpty();
    }

    @Override
    public boolean equals(final Object object)
    {   
        return (object instanceof Item) && (object != null) && (((Item) object).getIdentifier() == identifier);
    }

    @Override
    public int hashCode()
    {
        return Long.valueOf(identifier).hashCode();
    }

    public Set<ConstraintViolation<Item>> validate()
    {
        final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        final Validator validator = factory.getValidator();

        return validator.validate(this, Warning.class);
    }
}

任何人都可以解释这个,有没有人有办法只通过ajax提交表单和表单并显示结果?

1 个答案:

答案 0 :(得分:1)

您的HTML最终会在每个<form>周围<tr>。这是illegal HTML语法,因此未指定浏览器行为。

您需要将<h:form>放在<table>周围。如果您需要围绕单个“行”的表单,那么您可能希望将单个<table>重新设计为具有固定列宽或一组<table>的多个<div>。< / p>