Solved

how to write an error message on a jsp by calling a js function from jsp

Posted on 2006-07-07
10
368 Views
Last Modified: 2010-04-01
hi ,
i am facing the problem of showing an error message on a jsp page once the user clicks a link.

The error message is to be shown on the same page where the link is shown.

the code is as below

(<html:link href="#" onclick="document.write(indexEnableInMatch('pmformforindexing'))">
                                                                  <bean:message bundle="POIMatches" key="pmr.details.link" />
                                                                       </html:link>)


not abel to understand what code should i write on the the event onClick?

kindly help me

regards,
jatinder

0
Comment
Question by:ramareddyj
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 5

Expert Comment

by:jpolin1
ID: 17058877
Looks like you are using struts, why not use its built in error checking?

If you want it to display an error message w/o a page refresh(another request-responce cycle),
then you need some DHTML, like swapping layers in and out via javascript.

Let me know which way you are trying to go.
0
 

Author Comment

by:ramareddyj
ID: 17058987
i am using struts framework. and i have decide to show the error message in a textbox which will be disabled all the time

what i am trying to do now is on execution of following line
onclick="indexEnableInMatch(<%='pmformforindexing'%>)">

my indexEnableInMatch() will be called and inside this function i am going to set the value of a text box to my error message

function indexEnableInMatch(FormName)
{
      alert(FormName);
      FormName.hiddenLabel.value="my error emssage ";

}

now i am facing problem in passing  the parameter "pmformforindexing" which is nothing but my action form.

any way to resolve this??

regards,
jatinder
0
 
LVL 5

Accepted Solution

by:
jpolin1 earned 125 total points
ID: 17059194
Ideally you should use struts built in form-validation.
Are you familar with this at all? There are a couple of different ways to go with it.
It will be easier to setup and easier to maintain down the road.
If the data you want to validate is already on form beans associated with your actions, then it wouldn't be too much work to get the wiring together to make this happen.

Basically all you do is override the validate method in your form beans, perform your error check there.
If you have errors you create a collection of them and return them. Then all you have to do is create a resource bundle for the actual messages and put 1 new tag on your jsp.

The other good thing is that the validation is handled on the server side, rather than client side like you are trying to do with javascript. If you do your validation on the server side, you are guarenteed to be getting good data, javascript can always be disabled or hacked around.

The latest way of doing validation within struts uses the validation framework. There is a ton of stuff out there on this. I haven't used this approach yet.
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:ramareddyj
ID: 17063728
can i make the text color "RED" for a disabled text box?????
0
 
LVL 4

Assisted Solution

by:Meritor
Meritor earned 125 total points
ID: 17064595
If you are using IE then its hard to set color for disabled text box but in Mozilla/Netscape you can write the following code:
input[disabled] {color:red!important;}

As in:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title> New Document </title>
<style type="text/css">
input {
color: green;
}
input[disabled] {color:red!important;}
</style>

</head>

<body>
<form>
<input type="text" disabled value="disabled"></input><br>
<input type="text" value="normal"></input>
</form>
</body>
</html>
0
 
LVL 5

Assisted Solution

by:muktajindal
muktajindal earned 125 total points
ID: 17071822
You should use server-side validation provision by Struts to solve this. Are you aware of Struts Form validation using "validate" method in your Form classes? Just try that and pass errors from there to your jsp files and display there.

For textbox color change, you can define two separate classes for active and disabled textboxes in your CSS file. Create textboxes with the active class. Whenever you want to disable a textbox, just change the class associated with that textbox. You should get the desired results.
0
 
LVL 10

Assisted Solution

by:jaggernat
jaggernat earned 125 total points
ID: 17349532
You should use the "Struts validation framework". Its very simple to use and very powerful.

Struts validation framework comprises primarily of two files, validation.xml and validation-rules.xml(provided by struts framework).

lets say you want to validate a field "xyz" in your jsp,
In your validation.xml file , you have to write

<form name="nameoftheformbean">
<field property="xyz" depends="required">
<arg0 key="showerrormessage"/>
</field>
</form>


<field property="xyz" depends="required">  //reuired is a rule defined in validation-rules.xml file which means that the field "xyz" cannot be null.
<arg0 key="showerrormessage"/>  //showerrormessage is defined in ApplicationResources.properties file like
showerrormessage=Please enter a value in the field xyz

and thats it . Its that simple.


In your struts-config , you have to put
validate="true" and
input="NameOfJSP"

Basically this means perform the validation and if there are any errors show them on NameOfJSP.jsp

Like "required" is a rule, there are other rules.

Take a look at this excellent article from Orielley

http://www.onjava.com/pub/a/onjava/2002/12/11/jakartastruts.html

hope that helps
J
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17349555
>>>>>   input="NameOfJSP"

I meant    input="NameOfJSP.jsp"



0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
library class in java 1 93
if statement not resolving in my code 5 55
equalIsNot  challenge 43 174
struts spring hibernate example 12 132
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
Building a successful professional career is a long and difficult journey, especially in case if your decisions are not chosen carefully. For example, if you think that you can get to the desired position without experience and apply for it, your ch…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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