aspose file tools*
The moose likes Struts and the fly likes Where should I perform validation with database connectivity? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Where should I perform validation with database connectivity?" Watch "Where should I perform validation with database connectivity?" New topic
Author

Where should I perform validation with database connectivity?

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi, I have an Action class and an ActionForm class. I also have a separate class called DBHelper that handles database connectivity... Now I wanted to validate user input (username and password). Where should I place the validation with database connectivity? Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It is generally considered to be "best practice" to do validations that involve access to the database in the Action class rather than in the validate() method of the ActionForm. The reason for this is that in the Action class you have greater control over what to do if the database is completely down, or if there are other errors related to database connectivity. It is also considered best practice not to include database access logic directly in your Action class.

Hence, the answer to your question is that you should put the logic to validate the user namd and password against the database in some other class (Sounds like DBHelper might be a good candidate, although I favor objects that follow the Data Access Object pattern). Then put a call to that method in your action class. If the validation fails, add an entry to the ActionErrors object and redisplay the JSP. The JSP should have a <html:errors/> tag to display the error.


Merrill
Consultant, Sima Solutions
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Thanks! That certainly boosted my confidence!
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi, I'd just like to clarify... By adding entries to the ActionErrors object you mean making a method in my action class similar to the validate method of ActionForm class?


DBHelper class




and in my action class




then I just have to make a call to <html:errors/> in my jsp page? Did I get it right? Thanks!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Unlike ActionForm, there is no method on an Action class that is automatically called to do validation. You have to do validation yourself as part of the execute() method. Here is some sample code to get you started:


If you're using Struts 1.2 and above, use "new ActionMessage()" instead of "new ActionError()" as the ActionError class is deprecated.
[ January 10, 2006: Message edited by: Merrill Higginson ]
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi, please take a look at my code...




The errors are not displayed in my error page... I have ofcourse used <html:errors/>... Also... Eclipse says that saveErrors() is a deprecated method... Is there any alternative to this?


if you need to see my Action mappings here it is...




Please help... Thanks!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Since you appear to be using struts 1.2.x, change the type of the errors field to ActionMessages and that will fix the "deprecated method" message you're getting.

The <html:errors> works one of two ways:

1. It displays all errors created with the ActionMessages.GLOBAL_MESSAGE option.
2. It displays specific messages when you specify a "property" attribute

so, you can either change the code in your Action to:

errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("login.invalid"));


or change your <html:errors/> tag to <html:errors property="usernamepassword" />
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi again, nothing's really happening... I tried to change the deprecated classes to the more updated ones.


package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;

import beans.UserLoginForm;
import dao.DBHelper;

public class LoginUserAction extends org.apache.struts.action.Action
{

// Global Forwards
public static final String GLOBAL_FORWARD_getUser = "getUser";

// Local Forwards
public static final String FORWARD_success = "success";
public static final String FORWARD_failed = "failed";


public LoginUserAction()
{
}

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
UserLoginForm ulf = (UserLoginForm)form;
ActionErrors errors = new ActionErrors();
DBHelper dao = new DBHelper();

//HttpSession session = request.getSession();
System.out.println("Loggin in...");

System.out.println("Username: " + ulf.getUsername());
System.out.println("Password: " + ulf.getPassword());


if (dao.isExisting(ulf.getUsername(), ulf.getPassword()) == true)
{
return (mapping.findForward(FORWARD_success));
}
else
{
errors.add("usernamepassword", new ActionMessage("login.invalid"));

//super.saveErrors(request, errors);
super.saveMessages(request, errors);
return (mapping.findForward(FORWARD_failed));
}

//return (mapping.findForward(FORWARD_failed));

}

}


I was wondering if redirecting affected the transaction? If you remember in my action mapping I put...




hmmm... I was also wondering about the arguments in super.saveMessages(response, errors). There's another saveMessages() method that accepts a HttpSession argument... If you also need to know... Here's my JSP source code..


Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The 'redirect="true"' is definitely a problem. You need to remove it in both forwards.

The problem is that when you specify redirect="true", you lose any information saved in either the request or session contexts, and that's where the errors have been saved.

Also, you may have misunderstood me before, but to get rid of the deprecated message, you need to change

ActionErrors errors = new ActionErrors();

to

ActionMessages errors = new ActionMessages();
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
It still won't work... Maybe I should just leave this for a while and concentrate on other parts of my application. It would be nice to know the problem now though... Here's my source anyway... Thanks for your help Merrill and sorry to have bothered you...


LoginUserAction.java



failed.jsp


struts-config.xml
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
since you are now producing a global error, you need to remove property="usernamepassword" from your <html:errors> tag.

Also, you should still call super.saveErrors(), not super.saveMessages().
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
WHOAH! IT WORKED! IT WORKED! Thanks Merrill! Mind if I give you a hug? > <

Here's the source!

LoginUserAction.java




failed.jsp




Thanks! Thanks! Thanks!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Whew! Glad it finally worked.
Cheers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Where should I perform validation with database connectivity?