*
The moose likes Struts and the fly likes Struts 1.1 problem with form validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 1.1 problem with form validation" Watch "Struts 1.1 problem with form validation" New topic
Author

Struts 1.1 problem with form validation

Anthony D'Souz
Ranch Hand

Joined: Oct 13, 2011
Posts: 63
Hi all,

I am trying to validate my struts login page to check if some value is entered for the username and password or not. If not then error message should be displayed. But when i debug my application by setting a break point in thee validate method, then the validate method is getting executed but the user is getting redirected to a blank page rather than displaying the error message in the login page itself. Please have a look at what i did so far:











Please help!!!

Thanks,
Butny
Chandan Sarma
Greenhorn

Joined: Oct 04, 2011
Posts: 14
You can do one more things.the validation you can do in action itself. I am attaching some code here. just change your action mapping some what it would work


import javax.servlet.http.*;
import org.apache.struts.action.*;

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

/* forward name="success" path="" */
private static final String SUCCESS = "success";
/* failure name="failure" path="" */
private static final String FAILURE = "failure";

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//Add error into incoming HTTP request
ActionMessages errors = new ActionMessages();
// By making instance of DynaActionForm class,
DynaActionForm actionForm = (DynaActionForm) form;
//Value store into DynaForm is call here:
String first = (String) actionForm.get("first");
String last = (String) actionForm.get("last");
int age = (Integer) actionForm.get("age");
int number = (Integer) actionForm.get("number");

//Check the valiadation condition into incomming HTTP request form DynaForm
if (first == null || first.length() < 1) {
errors.add("first", new ActionMessage("error.first.required"));
}
if (last == null || last.length() < 1) {
errors.add("last", new ActionMessage("error.last.required"));
}
if (age == 0) {
errors.add("age", new ActionMessage("error.age.required"));
}
if (number < 1) {
errors.add("number", new ActionMessage("error.number.required"));
}
//Save Error/request and dispaly it anywhere within this application
saveErrors(request, errors);
if (errors.isEmpty()) {
return mapping.findForward(SUCCESS);
} else {
return mapping.findForward(FAILURE);
}
}
}
Tagore Noo
Greenhorn

Joined: Nov 29, 2011
Posts: 4
<html:form action="/login">
change this to <html:form action="login.do"> and try.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 1.1 problem with form validation
 
Similar Threads
ServletException in:/pages/logon.jsp] Cannot retrieve definition for form bean null'
javax.servlet.jsp.JspException: No getter method for property: "id" of bean: "Login.LoginForm"
help with login program
Validation framework is not working
How is a request handled within struts framework