Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8554885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:59:18+00:00 2026-06-11T14:59:18+00:00

I would like to do a register form which works fine: <h:form id="register_form"> <h:panelGrid

  • 0

I would like to do a register form which works fine:

<h:form id="register_form">
            <h:panelGrid columns="3">
                <h:outputLabel for="email" value="E-mail:" />
                <h:inputText id="email" value="#{userc.userb.user.email}">
                    <f:ajax event="blur" render="m_email" />
                </h:inputText>
                <h:message id="m_email" for="email" ajaxRendered="false" />

                <h:outputLabel for="password" value="Passoword" />
                <h:inputSecret id="password" value="#{userc.userb.user.password}">
                    <f:validator validatorId="confirmPasswordValidator" />
                    <f:attribute name="confirm" value="#{confirmPassword.submittedValue}" />
                </h:inputSecret>
                <h:message id="m_password" for="password" />

                <h:commandButton value="Registerr" action="#{userc.register}">
                    <f:ajax execute="@form" render="@form" />
                </h:commandButton>
                
                <h:messages globalOnly="true" layout="table" />
                
            </h:panelGrid>
        </h:form>

And this is my controller:

public void register(){
    FacesMessage message;
    try {
        userb.encryptPassword();
        userEAO.create(userb.getUser());
        
        message = new FacesMessage("Register successfully!");
        FacesContext.getCurrentInstance().addMessage(null, message);
    } catch (Exception e) {
        // 1062 : duplicate entry
        if (e.getMessage().contains("Duplicate")){
            message = new FacesMessage("User already registered, please try another email");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }else{
            message = new FacesMessage("An error occured, try again please");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }
    
}

So, for example, if some user try to register himself twice, I return this message, saying that’s duplicate, but I wonder if this is the best approach.

How to treat these kinds of exceptions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:59:19+00:00Added an answer on June 11, 2026 at 2:59 pm

    Those validations should be handled by your Business Logic Layer object, in this case, it looks like it is the userEAO object. You can decide if you should handle this error messages in the flavor of Exceptions (which IMHO won’t use).

    In my case, I prefer to use a Message class that basically contains an integer code and a message holding the results of the operation. For my case, I usually return 0 or more to indicate the operation was successful, and use negative codes to declare errors. This is a pretty basic example:

    public class Message {
        private int code;
        private String message;
        //constructor...
        //getters and setters...
    }
    
    public class UserBL {
    
        public Message create(User user) {
            //do some fancy validations
            if (!verifyDuplicatedUser(user)) {
                return new Message(-1000, "Duplicated user");
            }
            //and the operations...
            saveUser(user);
            return new Message(0, "Register successfully!");
        }
    
    }
    

    Now your controller will be forward to what it must really do (this means, no heavy business logic validations):

    public void register() {
        FacesMessage fmessage;
        userb.encryptPassword();
        Message message = userEAO.create(userb.getUser());
        fmessage = new FacesMessage(message.getMessage());
        FacesContext.getCurrentInstance().addMessage(null, fmessage);
    }
    

    This approach can even help you when you should validate a set of data, like validating the results of an Excel sheet and you must show a list of errors (because showing a single error lot of times is very annoying!).

    You can improve the way you create the messages, like retrieving them from a table with the messages for your system, or using properties files to handling internationalization (that depends on your functional requirements, of course).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a registration form which creates subdomains (yet on localhost),
I have php scripts that do things like register, login, upload. I would like
I would like to make register button, but when people click on it. A
I would like to convert this fluent approach to xml: container.Register( AllTypes.FromAssemblyNamed(Company.DataAccess) .BasedOn(typeof(IReadDao<>)).WithService.FromInterface(), AllTypes.FromAssemblyNamed(Framework.DataAccess.NHibernateProvider)
I've a website where users can register. I would like to prevent all special
Using MVVM's SimpleIoc, I would like to register an implementation for a given interface,
I would like to create an HTML form with one field for user name,
I would like to create an event signup form using Google Docs. The user
I would like to have in the registration form in my site an interface.
I am trying to make a form which would check if an username is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.