aspose file tools*
The moose likes Struts and the fly likes Display error message using validate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Display error message using validate?" Watch "Display error message using validate?" New topic
Author

Display error message using validate?

Timotius Pamungkas
Greenhorn

Joined: Feb 26, 2005
Posts: 28
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.
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
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

Joined: Feb 26, 2005
Posts: 28
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display error message using validate?
 
Similar Threads
ActionErrors with html:errors tag not displaying errors :-(
validation error messages
String value substitution
html:options horrible default settings
ActionMessages?????????