Display error messages in JSF [ h:message ] – part 2

  • Topic : Java Server Faces (JSF)
  • Environment : J2EE 5.0, MyFaces 1.1.5

fieldErrorMessage.jsp

also read:

<!--
   Source : www.javabeat.net
-->

<html>
    <body>
          <f:view>
              <h:form id="error">
                   <h:inputText id="name" value="#{fieldErrorMessageBean.name}" >
                       <f:validateLength maximum="5"/>
                   </h:inputText>
                   <h:message for="name"/>
                   <h:commandButton value="Submit" action="#{fieldErrorMessageBean.submit}"/>
              </h:form>
          </f:view>
    </body>
</html>

FieldErrorMessageBean.java


/**
 * Source : www.javabeat.net
 * */
package net.javabeat.myfaces.error;

public class FieldErrorMessageBean {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String submit(){
        return "fieldErrorMessageResult";
    }
}

faces-config.xml


   <managed-bean>
        <managed-bean-name>
            fieldErrorMessageBean
        </managed-bean-name>
        <managed-bean-class>
            net.javabeat.myfaces.error.FieldErrorMessageBean
        </managed-bean-class>
        <managed-bean-scope>
            request
        </managed-bean-scope>
    </managed-bean>
    <navigation-rule>
        <navigation-case>
            <from-outcome>
                fieldErrorMessageResult
            </from-outcome>
            <to-view-id>
                /pages/error/fieldErrorMessageResult.jsp
            </to-view-id>
        </navigation-case>
    </navigation-rule>

fieldErrorMessageResult.jsp


<!--
   Source : www.javabeat.net
-->

<html>
    <body>
          <f:view>
              <h:form id="error">
                  <h:outputText value="Validation Success"/>
              </h:form>
          </f:view>
    </body>
</html>

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*