File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts 1.1 problem with form validation

 
Anthony D'Souz
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic