Solved

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

Posted on 2006-07-07
10
365 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
In this increasingly digital world, security hacks are no longer just a threat, but a reality. As we've witnessed with Target's big identity hack 2013, Heartbleed in 2015, and now Cloudbleed, companies and their leaders need to prepare for the unthi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

831 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