Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Validator Framework question.

Hi,

I can check validation on properties of beans with the Validator Framework.
In the login page,
I check if the user enters his user-ID and password.
I put ser-ID and password as required,
that works greats, if he don't enter login, message is displayed, also for password.

Now I am checking if the user-ID and the associated password are valid in my Database,
to see if he is a valid registered user.

When he submits the form (user-ID and password fields), I go check in DB,
how should I handle the errors now and the display of them ?

Thank u.
0
matthew016
Asked:
matthew016
  • 12
  • 11
  • 4
2 Solutions
 
dasmaerCommented:
Hi

I catch errors with a simple try{}catch(Exception e){}. If the DB fails then you're covered. When talking about the validator, you'll see that the validator classes will handily return booleans.  Without seeing what your code looks like its a but tricky, but try something like the following:

if (validator.control.name(username,password)){
  //create some session variables.
}
else{
  //set conditions to re-prompt for authentication details.
}

Post up the offending bit of code so I can see if it sheds any more light.
0
 
matthew016Author Commented:
I do the validations in XML files.
with Validator Framework.
0
 
dasmaerCommented:
Okay, at some point however a JSP or Java class must be getting the results from your validation? and then querying the DB? Is that right or do you have the Validator Framework doing a DB connection?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
matthew016Author Commented:
Yes, I have a method that checks in DB,
and return User object, or null if it was not valid.
0
 
dasmaerCommented:
Okay

If it returns null then you want to know how to handle the error? In the JSP or class, when you check to see if you have been given a null user object:

boolean authenticated = false;
try{
   /*
     your code that gets the User Object
  */
   if(user.obj!=null){
      //get on with life
     authenticated = true;
     ... //more code if you have other stuff you fancy doing
   }
} //try
catch (Exception e){
   //any logging you have put here
}

if (!authenticated){
  response.sendRedirect("login.jsp")
}

What this will do is set up a boolean then get a user object. if it can;t get one, or it gets one and its null, then authenticated is still false... so send them back to the login page (or indeed wherever you like).

It is hard to help without seeing the code you're working with, as yet its not been posted. However I hope I'm on the right lines here.
0
 
matthew016Author Commented:
yes ok I see, just a try catch,

but something confuses me,

I do nothing in the XML files ?
0
 
matthew016Author Commented:
Ok, I know what is confusing me :

I'll still need to put errors in ActionMessages,
but I don't have any validate() method since I use the Validator Framework.
Where do I put the ActionErrors ?
0
 
dasmaerCommented:
Where do you want them? Do you mean you are getting errors from the framework and don't know where to put them (in which case there should be some javascript handlers built in - see: http://www.roseindia.net/struts/struts_validator_framework.shtml)? 
0
 
jaggernatCommented:
>>>Where do I put the ActionErrors ?
two things to take care of.
validation using validation framework
and db validation.

if  user  gets through the validation framework, get the values in action class, and check if the values  exist in database.
If it does not, forward user to an error page, and show errors.

If username/ password doesnt get through validation framework , user is blocked on the client side and values dont reach action class.



0
 
matthew016Author Commented:
So,
when the user enters user-ID and password,
the validator framework will automatically check the validity of the user-ID and password defined in the dynaformbean.

If it is ok,
*where* do I check in DB ?

I want :

if form is not valid (checked by validator framework) => go to page that is defined in "input" tag in the struts-config.xml

if db error => put errors in in ActionErrors, !!go to "input" tag also!!, and  display errors.
0
 
jaggernatCommented:
>>>>  if db error => put errors in in ActionErrors, !!go to "input" tag also!!, and  display errors.
No

Input tag will come into the picture ONLY if validation framework detects errors.

In your ACTION class , if db error occurs i.e username and password are missing in the database, then use ActionMessages to display errors.
 use this template



public ActionForward checkUsernamePassword(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception
{
ActionMessages messages = new ActionMessages();
      if ( condition)
//username and password do not exist


{
//add error.   Cannot log you in buddy

messages.add(Globals.ERROR_KEY,new ActionMessage("errors.reportQuantityinchekcout"));
saveMessages(request, messages);
}

return mapping.findForward("jsp which will show error messages");
}



 public void saveMessages(
        HttpServletRequest request,
        ActionMessages messages) {

                if ((messages == null) || messages.isEmpty()) {
            request.removeAttribute(Globals.MESSAGE_KEY);
            return;
        }

        // Save the messages we need
        request.setAttribute(Globals.MESSAGE_KEY, messages);
    }


 public static final String MESSAGE_KEY =
      "org.apache.struts.action.ACTION_MESSAGE";
0
 
matthew016Author Commented:
Thank u jaggernat,

What if I have more than one error possible in my execute function,
and that I want to be able to display them individually in my JSP page ?
0
 
jaggernatCommented:
Make  your Actionclass extend DispatchAction. that way you can have custom methods each forwarding to a different error page or tile.

Dont use execute function . use DispatchAction

go through this sample

http://javaboutique.internet.com/tutorials/strictly_struts/index-2.html
0
 
matthew016Author Commented:
I don't need to forward to different error pages.
Just forward to one page.

But I mean, if I have more than one "type" of error possible and I use the key : Globals.ERROR_KEY,
I can't identify each error when I will desire to display them in the jsp, u follow me ?

Can I do this ?

if(user == null) {
      ActionErrors errors = new ActionErrors();
      errors.add("invalid_user", new ActionMessage("errors.invalid_user")); <--- use a String as key to identify each error
      saveErrors( request, errors );
      return mapping.findForward((String) session.getAttribute("page"));
}
0
 
jaggernatCommented:
If you have multiple error messages it would be something like

messages.add(Globals.ERROR_KEY, new ActionMessage("errors1.message"));
messages.add(Globals.ERROR_KEY, new ActionMessage("errors2.message"));
messages.add(Globals.ERROR_KEY, new ActionMessage("errors3.message"));
and so on..
0
 
matthew016Author Commented:
Ok jaggernat,

but like I said how can I display the global errors in the jsp ... ?
=>

<html:form method="post" action="/Login.do" onsubmit="return valider();">
                        
login :
<br/>
<html:text property="login" size="10"/>

<html:errors property="login"/>
<html:errors property=    <-- HERE the GLOBAL for the first error
                                               (error if password and user invalid)

[some HTML code ...]

<html:errors property=    <-- HERE the GLOBAL for the second error
                                                      (error if for IOException)

etcetera...


If I use "Globals.ERROR_KEY" I can't *identify* the errors.

For example the errors with the bean properties, login and password, I can identify them,
because they have a key :

<html:errors property="login"/>
<html:errors property="password"/>

Per consequent I can put them where I want in my JSP.

Do u understand ?
0
 
jaggernatCommented:
use this code from my project

create a jsp and put this code in it call it error.jsp


<%@ taglib uri="/WEB-INF/c-rt.tld" prefix="c_rt" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ page import="org.apache.struts.Globals"%>
<%@ page import="org.apache.struts.util.RequestUtils"%>
<head>
</head>
 
 
<c_rt:set var="actionErrors" value="<%=RequestUtils.getActionMessages(pageContext,Globals.ERROR_KEY)%>"/>
<jsp:useBean id="actionErrors" type="org.apache.struts.action.ActionMessages"/>
      
<c_rt:if test="<%=actionErrors.isEmpty()==false%>">
<html:errors/>
</c_rt:if>



in your jsp where you want to display errors  include this
<jsp:include page="/includes/error.jsp"  flush="true"/>  
0
 
jaggernatCommented:
if i do it using jstl (i.e  reusing the single table code multiple times), i dont think the tables will be aligned/placed side by side.
I am not even sure if that approach will work. i tried the above code today and i was getting some errors.

0
 
jaggernatCommented:
ignore above post matthew :) . cross-post busted :)

0
 
jaggernatCommented:
does the above code work matthew?
0
 
matthew016Author Commented:
I'll be back on this this evening jaggernat, I had some issues that had first to fix in my web application.
0
 
matthew016Author Commented:
jaggernat,
sory for bothering, but u didn't understand my question,
seems like I have communication problems in this forum, but it seems that I am clear ;-)

I don't want to have "<html:errors/>" in my code,
as I said above,
I want to be able to identify each error, being able to display each error where I want,
If I use <html:errors/>, they will *all* display at the same place,
do u see what I mean now ?
0
 
jaggernatCommented:
ok. I dont think you can do that. If you are using the validation framework you have to use
<html:errors/>   or <html:messages/>  to display errors/messages.

I thought you wanted something like
>>
messages.add(Globals.ERROR_KEY, new ActionMessage("errors1.message"));
messages.add(Globals.ERROR_KEY, new ActionMessage("errors2.message"));
messages.add(Globals.ERROR_KEY, new ActionMessage("errors3.message"));


and then use <html:errors/> to dispaly the above messages.

But if you want to avoid <html:errors/>, you have to think of someother way of handling errors.

0
 
matthew016Author Commented:
if instead of using Globals.ERROR_KEY I use a String like "user_not_in_db" ,
then I can display the message :

<html:errors property="user_not_in_db"/>
0
 
jaggernatCommented:
hmm, it might work, not sure. just try it and see what happens.


0
 
matthew016Author Commented:
yes it works
I wonder though if it is not a bad programming
0
 
jaggernatCommented:

what part of it do you think is "bad programming" ?

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 11
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now