• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Display error message using validate?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I'm quite new in Struts, and I'd like to know how to display error message. I found that ActionForm.validate(ActionMapping, HttpServletRequest) can be used for validation purpose, but I don't know in which part to show my error message. My validate() looks like this :

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

ActionErrors errors = new ActionErrors();
int errorIndex = 0;

if (request.getParameter("myParam") == null || request.getParameter("myParam").length == 0) {
errors.add(String.valueOf(++errorIndex), new ActionMessage("Zero-length param encountered"));

}

return errors;
}


I've already debugged my ActionForm, and it's true that errors contains ActionError, but the message never displayed.

Another question, in case I'd like to display a "yes/no" message, such as "Zero-length param encountered. Continue process?", what should I do? If the user chooses "Yes", system will forward user into another window, otherwise, system will reload page.

Thanks in advance.
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to achieve two things:
- Server side validation
- Client side validation (Javascript)

For server side error message to be displayed on the form input page you need to have "<html: errors/>" in the form page.

You may have to explain about


Another question, in case I'd like to display a "yes/no" message, such as "Zero-length param encountered. Continue process?", what should I do? If the user chooses "Yes", system will forward user into another window, otherwise, system will reload page.




I believe there is no direct means to achieve this using validator-plugin. Either you can store the javascript generated from validator into a static script file and modify the validation function or you need to write your own javascript function to do this. I am not sure what you meant by forward to another window. Did you mean opening new window or just submit the form and refresh the content on the page?

The below URL contains simple tutorials on struts validator framework. It might be useful. http://www.roseindia.net/struts/
[ November 03, 2006: Message edited by: Purushothaman Thambu ]
 
Timotius Pamungkas
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
I believe there is no direct means to achieve this using validator-plugin. Either you can store the javascript generated from validator into a static script file and modify the validation function or you need to write your own javascript function to do this. I am not sure what you meant by forward to another window. Did you mean opening new window or just submit the form and refresh the content on the page?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

I mean, when user click "Yes", it will replace current page (same window) but when user clicks "No", it will bring user back to current page(without any changes on already-inputted data)

The actual problem is, I need to validate whether someone's name already exists in database (CMIIW - server-side-validation). If it is exists, the system will ask user whether user wants to continue with current person (that already exists in database). What should I do? I thought I can use following flow :

1. put validation in jsp (call by something like isExists = validator.checkPersonExistence(name, townOfBirth, dateOfBirth) ). In this case, isExists may vary (I have 6 different values based on validation).
2. based on isExists, if it is not 0, display appropriate confirmation window. In this window, if user answers yes, replace page with second page. If user answers no, rreload current page (just close the confirmation window). Now, I don't know how to do it using JSP. I can retrieve isExists correctly, but I stuck here. So I think maybe Struts can answer my problem rather than using only pure JSP and JS. JSP can get isExists, but cannot open confirmation window while JS do vice versa.

Any suggestion?
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This situation would be a good place to use AJAX. You could make an AJAX call to determine if the record exists or not, and then use JavaScript to display the confirm box and take action accordingly.

This thread contains a simple AJAX/Struts example that you could use as a starting point.
 
reply
    Bookmark Topic Watch Topic
  • New Topic