JSF Validator Problem HTTP Status 500

wbott@freenet.d

Aktives Mitglied
Hallo,
habe da ein Anfängerproblem, dass ich nicht verstehe.
Eine einfache JSP enthält drei Eingabefelder und eine Validatorklasse.

Scheinbar habe ich diese Klasse aber an falscher Stelle registriert/konfiguriert:
SCHWERWIEGEND: Servlet.service() for servlet FacesServlet threw exception
javax.servlet.jsp.JspException: Not nested in a tag of proper type. Error for tag with handler class:com.sun.faces.taglib.jsf_core.ValidatorTag
at javax.faces.webapp.ValidatorELTag.doStartTag(ValidatorELTag.java:111)

Hier der Quellcode der JSP (Zeile 31) :

Java:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
 
<html>
<body>
<f:view>
   <h:form>
      <%-- Einlesen des Resource-Bundles und binden an die lokale Variable msg --%>
      <f:loadBundle basename="resources.application" var="msg"/> 
      <fieldset>
         <%-- Ausgabe eines Labels aus dem Ressource-Bundle --%>
         <h3><h:outputText value="#{msg.detail_heading}"/></h3>
         <%-- Deklaration eines Grids, welches nach jeweils 3 Elementen eine neue Zeile beginnt.--%>
         <h:panelGrid columns="3">
            <%-- Ausgabe eines Labels aus den Ressourcen --%> 
            <h:outputLabel for="firstName" value="#{msg.address_firstName}"/>
            <h:inputText id="firstName" required="true"
                         value="#{addressController.currentAddress.firstName}"/>
            <h:message for="firstName"/>

            <h:outputLabel for="name" value="#{msg.address_name}"/>
            <h:inputText id="name" required="true"
                         value="#{addressController.currentAddress.name}"/>
            <h:message for="name"/>

            <h:outputLabel for="mail" value="#{msg.address_eMail}"/>
            <h:inputText id="mail" required="true" 
                     value="#{addressController.currentAddress.mail}"/>
                     <%-- Validator registrieren (f=alias für jsf-core) --%> 
                     <f:validator validatorId="eMailValidator" />             
            <h:message for="mail"/>
         </h:panelGrid>

         <h:commandButton action="#{addressController.cancel}"
                          value="#{msg.detail_cancel}"
                          immediate="true"/>
         <h:commandButton action="#{addressController.save}"
                          value="#{msg.detail_save}"/>
      </fieldset>
   </h:form>
</f:view>
</body>
</html>

Die Konfiguration (faces-config.xml):

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">

<!-- Einbinden eines eigenen Validators -->
<validator>
<validator-id>eMailValidator</validator-id>
<validator-class>jsf.EmailValidator</validator-class>
</validator>


<!-- Definition der unterstützten Locales -->
<application>
<locale-config>
<default-locale>de</default-locale>
<supported-locale>en</supported-locale>
</locale-config>
<resource-bundle>
<base-name>resources.application</base-name>
<var>msg</var>
</resource-bundle>
<message-bundle>resources.application</message-bundle>
</application>

<!-- Definition des Workflows in Form von Navigationsregeln -->
<navigation-rule>
<navigation-case>
<from-outcome>listAddresses</from-outcome>
<to-view-id>/jsp/addressList.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>editAddress</from-outcome>
<to-view-id>/jsp/addressDetail.jsp</to-view-id>
</navigation-case>
</navigation-rule>

<!-- Einbinden des Adress-Controllers -->
<managed-bean>
<managed-bean-name>addressController</managed-bean-name>
<managed-bean-class>jsf.AddressController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

</faces-config>

und die Validatorklasse:

Java:
package jsf;
import  java.util.regex.Pattern; 
import  java.util.regex.Matcher; 
import  java.util.ResourceBundle;
import  javax.faces.context.FacesContext;
import  javax.faces.component.UIComponent;
import  javax.faces.application.FacesMessage;
import  javax.faces.validator.Validator; 
import  javax.faces.validator.ValidatorException; 

/** Validiert eMail-Adressen auf syntaktische Korrektheit */
public class [COLOR="red"]EmailValidator [/COLOR]implements Validator  {

   /** Validiert den Wert des übergebenen Objekt 'obj' */
   public void validate (FacesContext ctx, UIComponent component, Object obj) 
          throws ValidatorException  {

      // Ressourcen-Bundle laden um die Fehlermeldung auszulesen
      ResourceBundle bundle = ResourceBundle.getBundle("resources.application",
                              ctx.getViewRoot().getLocale());
      // Fehlermeldung auslesen
      String errorKey = "errors_email";
      String errorMsg = bundle.getString(errorKey);

      // Fehlermeldung im Kontext speichern, damit diese ggf. auch in der JSP zur Verfügung steht
      FacesMessage msg = new FacesMessage(errorMsg);

      // 'null' ist ebenfalls ein ungültiger Wert
      if (obj == null) {
          throw new ValidatorException(msg);
      }

      // Muster gültiger eMail-Adressen als Regular Expression
      String pattern = "^[\\w-]+(?:\\.[\\w-]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7}$";

      // Pattern-Instanz erzeugen und auf Gültigkeit überprüfen
      Pattern p = Pattern.compile(pattern);
      Matcher m = p.matcher(obj.toString());
      if (!m.matches()) {
         throw new ValidatorException(msg);         // Wert passt nicht auf den Ausdruck
      }
   }
}

Ich verstehe nicht wo hier das eigentliche Problem liegt.
Kann mir jemand helfen?

Gruß Willy
 
Kenne mich mit JSF nicht aus, aber ich könnte mir vorstellen, dass der Validator-Tag ein Kindelement des zugehörigen Input-Feldes sein muss. D.h. Du darfst den Tag für das Input nicht direkt schließen. Aus Deinem Code:
[XML]
<h:inputText id="mail" required="true" value="#{addressController.currentAddress.mail}"/>
<%-- Validator registrieren (f=alias für jsf-core) --%>
<f:validator validatorId="eMailValidator" />
[/XML]
Mache mal das hier:
[XML]
<h:inputText id="mail" required="true" value="#{addressController.currentAddress.mail}">
<%-- Validator registrieren (f=alias für jsf-core) --%>
<f:validator validatorId="eMailValidator" />
</h:inputText>
[/XML]
 

Neue Themen


Zurück
Oben