我正在努力要求在JSF服务器端验证后突出显示红色失败的字段。没有javascript可用于验证。有没有一种方法可以将服务器端验证与css样式更改联系起来?
答案 0 :(得分:6)
您可以使用托管bean执行此操作:
public class ValidBean {
private UIComponent myComponent;
public UIComponent getMyComponent() {
return myComponent;
}
public void setMyComponent(UIComponent myComponent) {
this.myComponent = myComponent;
}
public String getErrorStyle() {
FacesContext context = FacesContext
.getCurrentInstance();
String clientId = myComponent.getClientId(context);
Iterator<FacesMessage> messages = context
.getMessages(clientId);
while (messages.hasNext()) {
if (messages.next().getSeverity().compareTo(
FacesMessage.SEVERITY_ERROR) >= 0) {
return "background-color: red";
}
}
return null;
}
}
请求范围变量:
<managed-bean>
<managed-bean-name>validBean</managed-bean-name>
<managed-bean-class>stylevalid.ValidBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
示例视图:
<f:view>
<h:form>
<h:inputText binding="#{validBean.myComponent}" styleClass="foo"
style="#{validBean.errorStyle}">
<f:validateLength minimum="6" />
</h:inputText>
<h:commandButton />
<h:messages />
</h:form>
</f:view>
组件绑定到辅助bean。如果已为组件排队错误消息,则它将使用其style属性覆盖其CSS类设置。
答案 1 :(得分:1)