Solved

Struts and Javascript

Posted on 2003-11-26
7
2,160 Views
Last Modified: 2010-04-01
Hi experts,

Is there a way for struts to first call a javascript function (to check some value) and if it returns a certain value, continue on with the action, or if returns another value, simply display a javascript alert window and not enter the action anymore?

I have a form and a submit button (an image button), but I wanted to try and perform a validation check first using javascript and inform the user using an alert window if the value = false. If value = true, continue with the action.

Thanks!
0
Comment
Question by:rusco
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 25 total points
Comment Utility
Struts is a server-side technology, and javascript is a client side technology, so there is no way for the two to communicate directly...

You can do things like "check a form is correctly filled in" using javascript

http://javascript.about.com/library/scripts/blformvalidate.htm

But you cannot pass action variables to the javascript and back again without multiple reloads and posts of the page

Tim
0
 
LVL 2

Expert Comment

by:DidierD
Comment Utility
You can always call some javascript functions before you submit.

  For example:

  <script>
    function myFunction(){
     //do your validation here
     if (yourValidationFunction()){
       document.yourForm.action="nextpage.do";
       document.submit();
     } else{
       alert("wrong input");
     }
    }
  </script>

 
  <input type="button" onClick="myfunction()">

 

But there is a Struts Validator for this. I used it a while ago and it works perfectly.

There are already some basic validators available. The mask can be very usefull. But you can also add your own (Javascript) to the validator-rules.xml.

For more information about this see http://jakarta.apache.org/struts/userGuide/dev_validator.html. On the bottom of this page you can see a ref to http://www.manning-source.com/books/husted/husted_ch12.pdf. Look for client side validation.

Greetz,
Didier
0
 
LVL 2

Expert Comment

by:DidierD
Comment Utility
Should be

document.yourForm.submit();
0
 

Author Comment

by:rusco
Comment Utility
Hi Didier!

I tried out what you said but I run into some problems with javascript. It seems that when my code goes to my javascript, it only reads the first line of code:

ex:

my function {
    alert("Hello");
    alert("Test");
}

what will be returned to me is just the alert "Hello", the next alert will not be shown anymore. Below is my actual jsp code to do this (I forgot that I will be opening a new popup for this one):

 <html:image src="/ADM/images/edit_button.gif" border="0" onclick="return false"/>

<script language="javascript"><!--begin
   
    function getConfigItemKey()
    {  

        var count = parent.configItemsListForm.counter.value;
        var newWindow;
        alert(count);

        if (count.value == 1) {
            if (newWindow) {
                newWindow.close();
                newWindow = null;
            }
           
            var test = 1;
            window.open('/ADM/SetupEditConfigItem.do?
                                    configItemKey=test','confirm','width=400,height=150');
           
        }
       
        if (count.value == 0) {
            alert("Please select a config item to edit/view");
        }
       
        if (count.value > 1) {
            alert("Please select only one config item to edit/view");
        }
    }

\\ end-->
</script>

Thanks!
0
 
LVL 2

Assisted Solution

by:DidierD
DidierD earned 25 total points
Comment Utility
your function is wrong.

It should be something like

<script language="javascript">
function myfunction(){
  alert("Hello");
  alert("Test");
}
</script>

<html:image src="/ADM/images/edit_button.gif" border="0" onclick="myfunction()"/>


Look at my first example again. The yourValidate function returns true or false. If it returns true you do the submit or open the window by calling getConfigItemKey().

I think you want to do something like this.

<script>

   function yourValidationFunction(){
     // do your validation and return true or false
      return true;
   }

    function myFunction(){
     if (yourValidationFunction()){
        getConfigItemKey()
     } else{
       alert("wrong input");
     }
    }
  </script>

<html:image src="/ADM/images/edit_button.gif" border="0" onclick="myFunction()"/>

Hmmm Maybe i start to understand your problem. Do you mean that the html:image is doing the onclick and the submit (form action). You don't want the form action to happen.

if you use a normal HTML img the form action will not be submitted.
<img src="/ADM/images/edit_button.gif" onclick="myfunction()"> then in the javascript function you can do the submit. Like the example in my previous comment.

Greetz,
Didier
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

HOW TO: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
In this step by step tutorial with screenshots, we will show you HOW TO: Enable SSH Remote Access on a VMware vSphere Hypervisor 6.5 (ESXi 6.5). This is important if you need to enable SSH remote access for additional troubleshooting of the ESXi hos…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now