Solved

Validator Framework question.

Posted on 2006-11-28
27
285 Views
Last Modified: 2010-04-01
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
Comment
Question by:matthew016
  • 12
  • 11
  • 4
27 Comments
 
LVL 5

Expert Comment

by:dasmaer
ID: 18031747
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
 
LVL 9

Author Comment

by:matthew016
ID: 18031790
I do the validations in XML files.
with Validator Framework.
0
 
LVL 5

Expert Comment

by:dasmaer
ID: 18031856
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
 
LVL 9

Author Comment

by:matthew016
ID: 18031888
Yes, I have a method that checks in DB,
and return User object, or null if it was not valid.
0
 
LVL 5

Assisted Solution

by:dasmaer
dasmaer earned 200 total points
ID: 18032098
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
 
LVL 9

Author Comment

by:matthew016
ID: 18032158
yes ok I see, just a try catch,

but something confuses me,

I do nothing in the XML files ?
0
 
LVL 9

Author Comment

by:matthew016
ID: 18032197
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
 
LVL 5

Expert Comment

by:dasmaer
ID: 18032367
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
 
LVL 10

Expert Comment

by:jaggernat
ID: 18032924
>>>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
 
LVL 9

Author Comment

by:matthew016
ID: 18038397
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
 
LVL 10

Expert Comment

by:jaggernat
ID: 18041502
>>>>  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
 
LVL 9

Author Comment

by:matthew016
ID: 18041526
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
 
LVL 10

Expert Comment

by:jaggernat
ID: 18041613
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Author Comment

by:matthew016
ID: 18047606
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
 
LVL 10

Expert Comment

by:jaggernat
ID: 18049660
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
 
LVL 9

Author Comment

by:matthew016
ID: 18049699
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
 
LVL 10

Expert Comment

by:jaggernat
ID: 18049733
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
 
LVL 10

Expert Comment

by:jaggernat
ID: 18050741
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
 
LVL 10

Expert Comment

by:jaggernat
ID: 18050754
ignore above post matthew :) . cross-post busted :)

0
 
LVL 10

Expert Comment

by:jaggernat
ID: 18073716
does the above code work matthew?
0
 
LVL 9

Author Comment

by:matthew016
ID: 18074743
I'll be back on this this evening jaggernat, I had some issues that had first to fix in my web application.
0
 
LVL 9

Author Comment

by:matthew016
ID: 18078691
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
 
LVL 10

Expert Comment

by:jaggernat
ID: 18081585
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
 
LVL 9

Author Comment

by:matthew016
ID: 18083450
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
 
LVL 10

Accepted Solution

by:
jaggernat earned 300 total points
ID: 18089807
hmm, it might work, not sure. just try it and see what happens.


0
 
LVL 9

Author Comment

by:matthew016
ID: 18091207
yes it works
I wonder though if it is not a bad programming
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 18098522

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

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
netstat -a in AIX unix 8 26
Spring Tuorial 1 107
report generation frameworks 1 58
I get error: useBean: Duplicate bean name: {0} 1 87
In this step by step tutorial with screenshots, we will show you HOW TO: Enable SSH Remote Access on a VMware vSphere Hypervisor 6.5 (ESXi 6.5). This is important if you need to enable SSH remote access for additional troubleshooting of the ESXi hos…
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now