我有一个由 ui:repeat 提供的数据表。因为我希望用户能够逐行更改数据,所以每一行都包含在一个 h:form 中。最后,每个 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提交表单和表单并显示结果?