Results 1 to 5 of 5

Thread: Username Validation

  1. #1
    Join Date
    Mar 2009
    Posts
    10

    Default Username Validation

    Hi guys,

    I'm new to Spring and Hibernate, looking for some help on Validation...

    In my application, I have a registration form with two fields 'username' and 'password' and a corresponding controller that is used to save it to the database.

    My validator checks if the field entries are not empty..etc
    Fine till now.

    I want my validator to check if the username entered already exists in the database and if so show a corresponding msg in the formView...

    But the problem is I dont see how I can access my db from the validator...
    ?? (help me here plz..)
    ----

    I access the db from my controller in this way:

    Code:
    WebApplicationContext context;
     ...
    context=WebApplicationContextUtils.getRequiredWebApplicationContext(req.getSession().getServletContext());
    -----


    Thanks,
    Vishwa
    Last edited by Vishwanath; Mar 24th, 2009 at 02:57 PM.

  2. #2
    Join Date
    Mar 2009
    Location
    New Jersey
    Posts
    44

    Default

    Not sure if it's possible. However, It's better to validate user info in controller/DAO using database stored procedure. With this approach, one database call will check if the user exists, if it does, return 'user already exists' error otherwise save new user details.

  3. #3
    Join Date
    Mar 2009
    Posts
    10

    Default

    Thanks... I'll try that...

  4. #4
    Join Date
    Dec 2006
    Posts
    311

    Default

    why wouldnt you be able to access the dB from a validator? just register the validator like a controller or service object then inject or better yet autowire a service method that checks the db for dups.

  5. #5
    Join Date
    Mar 2009
    Posts
    10

    Default

    Hi..

    Please correct me if I'm making a fundamental mistake... (I guess im making one...??? )

    I understand that through DI, I can inject values...

    But in the code there must be a context.getBean(theBeanNameWhereDependencyInjectio nIsConfigured)

    Somewhere or the other in my code I would need a getBean() method invocation...

    (Please tell me if im right till here...)

    Looking at the API I find that it can be done using,

    Code:
    WebApplicationContext context;
     ...
    context=WebApplicationContextUtils.getRequiredWebApplicationContext(theServletContext);
    
    AccountSave accountSave=(AccountSave)context.getBean("AccountSaveDao");
    But don see how I can get theServletContext in my validator..

    In one other code I used

    Code:
    WebApplicationContext context;
     ...
    context=WebApplicationContextUtils.getRequiredWebApplicationContext(req.getSession().getServletContext());
    thus getting theServletContext thro' req object...
    But here the validate method has no request object to use...
    Last edited by Vishwanath; Mar 25th, 2009 at 06:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •