........ ............ ........... ............ .... ............
我的应用程序没有显示验证数据,我不知道为什么。 在其他情况下它可以工作,但不是这次,我真的不知道是什么问题。
JSP:
(...)
<div id="contentarea">
<c:if test="${userBean.isOnline}">
<h2>
<h:outputLabel value="#{msg.vehicleRegistration}" />
</h2>
<fieldset>
<h:form>
<h:outputLabel value="#{msg.brandLabel}" />
<h:inputText id="brand" value="#{carBean.brand}">
<f:validator validatorId="TextValidator" />
</h:inputText>
<h:message for="brand" style="color:red" />
<br />
<h:outputLabel value="#{msg.surnameLabel}" />
<h:inputText id="surname" value="#{carBean.surname}">
<f:validator validatorId="TextValidator" />
</h:inputText>
<h:message for="surname" style="color:red" />
<br />
<h:outputLabel value="#{msg.addresLabel}" />
<h:inputText id="address" value="#{carBean.adress}">
<f:validator validatorId="TextValidator" />
</h:inputText>
<br />
<h:message for="address" style="color:red" />
<br />
<h:outputLabel value="#{msg.yearLabel}" />
<h:inputText id="year" value="#{carBean.year}">
<f:validator validatorId="NumberValidator" />
</h:inputText>
<h:message for="year" style="color:red" />
<br />
<br />
<h:commandButton value="#{msg.addButtonLabel}" onclick="alert('#{msg.vehicleAdded}')"
action="#{carList.addToList(carBean)}" styleClass="myButton" />
</h:form>
</fieldset>
</c:if>
<c:if test="${!userBean.isOnline}">
<h2>
<h:outputLabel value="#{msg.accessDenied}" />
</h2>
<b><a href="/Project"> <h:outputText
value="#{msg.accessDeniedLabel}" />
</a> </b>
</c:if>
</div>
(...)
</html>
TextValidator.java
package my.jsf.code;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
public class TextValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
FacesContext facesContext = FacesContext.getCurrentInstance();
String localePL = facesContext.getViewRoot().getLocale()
.toLanguageTag();
String text = localePL.equals("pl") ? "Pole nie może być puste!"
: "Field cannot be empty!";
String val = (String) value;
if (val == null) {
FacesMessage msg = new FacesMessage(text);
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
}