File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes doubt in scriplets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "doubt in scriplets" Watch "doubt in scriplets" New topic
Author

doubt in scriplets

Div Raj
Greenhorn

Joined: Nov 04, 2008
Posts: 22
Hi

I've a doubt in writing scriplet code in jsp.

I like to get display as, when we are selecting and giving identity 1, then the next drop down SMNO should display options 1 to 7. And 1 to 11 for giving identity 2, like that...


<td class="bodyblue" align="left">Identity<br /></td>
<td class="bodyboldblack" valign="middle" width="1"></td>
<td><html:select property="identity"
style="WIDTH: 78px; HEIGHT: 16px">
<OPTION value="" selected>--Select--</OPTION>
<OPTION value="1">1</OPTION>
<OPTION value="2">2</OPTION>
</html:select><b></b> <br /> </td>
</tr>

<tr>
<td class="bodyblue" align="left">SMNO<span
class="mandatory"></span>  </td>
<td class="bodyboldblack" valign="middle" width="1"></td>
<td><html:select property="smno"
style="WIDTH: 78px; HEIGHT: 16px" >
<OPTION value="" selected>--Select--</OPTION>

<OPTION value="1">1</OPTION>
<OPTION value="2">2</OPTION>
<OPTION value="3">3</OPTION>
<OPTION value="4">4</OPTION>
<OPTION value="5">5</OPTION>
<OPTION value="6">6</OPTION>
<OPTION value="7">7</OPTION>
<OPTION value="8">8</OPTION>
<OPTION value="9">9</OPTION>
<OPTION value="10">10</OPTION>
<OPTION value="11">11</OPTION>
</html:select> </td>
</tr>



Help me please.

--------
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

"Divya K", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
If I'm understanding your problem correctly I can see two options to do what you want, but neither involves writting a scriptlet.

1. Submit the html form back to the server every time a change is made to the value of identity. Then in your jsp, use logic:equal tags to display the correct set of options based on the value of identity.

2. When the value of identity is changed, use javascript to modify which values are available in smno.

I'd lean towards option 2 on this, running client side code carries some risks but the user experience might be less pleasant if they have to wait for a response from the server every time the change the value of the first drop down.
Div Raj
Greenhorn

Joined: Nov 04, 2008
Posts: 22
Thankyou Tom.
Since I'm a beginner, could you please explain?

I thought it'll be easy to give the values in scriplets using <html ptions> tag. But I've to learn to write using JavaScript.Below is a code I've seen in a file.
Could you please explain how to write values using script?
Or in action we've to call a method doing that?

...
<script language="javascript">
function fetchCounterTypeBycounter(){
if(document.forms[0].counterId.value!=""){
document.forms[0].action= "<bean:message key="crtrs.homeurl" bundle="alias"/>/CreatecounterlabelAction.do?tid=14&method=fetchgroupBycounterId&counterId="+document.forms[0].counterId.value;
document.forms[0].submit();
}
else{
document.forms[0].countergroupText.value = "";
document.forms[0].description.value = "";
document.forms[0].counterNumber.value = "";

}
}
...

Please help me.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I agree that the easiest way to do this would be to use the htmlptions tag but when using struts you want to avoid using scriptlets and use the struts tags or other custom tags instead. Struts has tags to allow you to do logical branching, take a look at the logic:equal and logic:notEqual tags. They can be used to control which options are displayed. Keep in mind that struts code and only be run on the server, so if you use this method you'll need to put some javascript into your page to submit the form to the application server if the value of the first list changes.

The second option is to write the updates to the second drop down list in java script. This is, in my opinion, more tedious to write and maintain but it may provide a better experience for your users. To do this, look into using document.createElement to create the option tags and appendChild to associate them with the select tag.
Div Raj
Greenhorn

Joined: Nov 04, 2008
Posts: 22
Ok. so if I use logic:equal tags, as you told it wont be comfortable at the user end. right?
so for using JavaScript, I've to write some methods which do that task.isn't it?

But, I'm sorry. I couldn't understand the second option you told. Could you please explain?
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
The first option will cause the processing to occur on the application server, which will likely take a little longer. This first method will require the use of some javascript (but not as much as the second option) since you'll need to submit your html form when the value of the first checkbox changes (probably onchange event in the select tag).

For the second option, you can use javascript to rewrite some of the html for the screen. When I have questions on javascript I usually use microsoft's reference site:

microsoft html/dhtml reference

to create new options for a drop down you'll need to create new option elements, probably using the document.createElement function and then you'll need to associate these new elements with your drop down. So you'll need to get a reference to your drop down element and call appendChild passing the new option element you created. I assume there are also methods for deleting elements if you want to reduce the options in your second drop down. Javascript really isn't one of my strong points so there may be better ways to do this, this is just the way that I've used before. If you use this option try writting the javascript and posting the specific problems you are running into.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

"Divya K", My request that you change your display name to adhere to JavaRanch standards was not a suggestion. Valid display names are mandatory for participation on the Ranch. Please change your display name as instructed prior to your next post.

Be aware that accounts with invalid display names are disabled.

bear
JavaRanch Sheriff
Div Raj
Greenhorn

Joined: Nov 04, 2008
Posts: 22
ok.
Could you please tell me what does "appendChild" meant by?
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Its a javascript function that works for all of the html elements that I know about, it nests an html element inside of another. So if you have a reference to a select element and you call appendChild on it and pass it a reference to an option element then the option will be added to your drop down list. Check the link I posted as a reference and search around for online tutorials.
Div Raj
Greenhorn

Joined: Nov 04, 2008
Posts: 22
ok.
Thankyou Tom
 
 
subject: doubt in scriplets
 
Similar Threads
Combobox is not working in FireFox but it is working in IE.
Struts: Getting a blank page instead of the form
Error comes whilke compiling the jsp page
logic:forward problem
Why this problem???