aspose file tools*
The moose likes Struts and the fly likes INSERT THE VALUE OF A FORM FIELD INTO A JAVA STRING (without page refresh) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "INSERT THE VALUE OF A FORM FIELD INTO A JAVA STRING (without page refresh)" Watch "INSERT THE VALUE OF A FORM FIELD INTO A JAVA STRING (without page refresh)" New topic
Author

INSERT THE VALUE OF A FORM FIELD INTO A JAVA STRING (without page refresh)

Giulietta Blumarine
Ranch Hand

Joined: Dec 03, 2004
Posts: 59
Hi!
I have a html hidden field, whose value is setted dinamically by a javascript function (called by "onChange" on another field).
I have to compare this setted value with a java String, without refreshing the page, if possible.

The code is this.

When I select a value in the combo below,I call the javascrit function.

---------------------------------------------------------------
<html:select property="editore" onchange="filtraFamiglie()" name="SearchTab" >
<html ption value=" " />
<html ptions collection="listaEdi" property="editoreD"
labelProperty="descriEditoreD" />
</html:select>
-------------------------------------------------------------------

Javascript function set the selected value into the hidden field.


-----------------------------------------------------------------------
<script>
function filtraFamiglie(){
document.SearchTab.artifizio.value = document.SearchTab.editore.value;
}
</script>


<html:hidden property="artifizio" name="SearchTab" />
------------------------------------------------------------


ok,how can I compare the value setted by Javascript into "artifizio" with a String java?

String a = "hello";
if (a == ???) then .....

THANK YOU
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4742
    
    7

"Giulietta"

Welcome to JavaRanch! Just a couple of things before we get to your question:

1. Please change

your profile so that your publicly displayed name complies with the JavaRanch Naming Policy.

2. Posting in ALL CAPS on the Internet is meant to convey "shouting". Please use your "inside voice" in the forums. Please, make sure your keyboard "Caps Lock" is off.

Thanks for your cooperation.

As for your question, in JavaScript, you'd simply write

if (document.SearchTab.artifizio.value == "hello") {
...
}

If the value is something that you set in your Java code, then you could put it in the request context and use a scriptlet.



The best thing to do in this case though would probably be to create another hidden field and use straight JavaScript to compare the two.
[ December 15, 2004: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Giulietta Blumarine
Ranch Hand

Joined: Dec 03, 2004
Posts: 59
THANK YOU FOR ALERTS!
I DIDN'T READ NAMING POLICY BECAUSE OF TIME LACKING!
OK ALSO FOR "CAPS", IT'S NOT A PROBLEM.

I'M TRYING NOW YOUR SOLUTION.
THANK YOU
SEE YOU AGAIN HERE

Giu
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4742
    
    7

Originally posted by Giulietta Blumarine:
OK ALSO FOR "CAPS", IT'S NOT A PROBLEM.


Thanks for changing your name but you're still shouting... Please use normal case instead of all uppercase. Thanks.
Giulietta Blumarine
Ranch Hand

Joined: Dec 03, 2004
Posts: 59
Hi!
The problem is this:

I have crete the javascript comparison, according to your alert:

/* in JSP
<script> ... if (document.SearchTab.artifizio.value ==
"<%= request.getAttribute("aStr") %>") { ... }
</script>

/*

But inside my comparison I have to insert java logic.
I know that stirring java logic with javascript tag could give some problems, because javascript one runs on client side,java one runs on server side.
In fact my compiler gives no error, but the run time can't recognize the if - else condition (both of condition are executed).

Below my code in order to explain better
(I click on the first combo, js function is called, the second combo must be valued according to the js comparison).

Thanks for discussing

Giu
-------------------------
<script language="javascript">

function filtraFamiglie(){

alert(document.SearchTab.editore.value);
document.SearchTab.artifizio.value = document.SearchTab.editore.value;

}
</script>

-------------------------------

<html:hidden property="artifizio" name="SearchTab" value=""/>

---------------------------------------------


<script language="javascript">
if(document.SearchTab.artifizio.value != ""){ </scrip>
<%Collection col = (Collection) session.getAttribute("listaFam");%>

<html:select property="famiglia" <html ption value="" />

<% if(col.iterator().hasNext() ){

FamigliaDTO dto = (FamigliaDTO) col.iterator().next();
String idFamdto = dto.getIdFamiglia();
String desFam = dto.getDescrifam();

session.setAttribute("valore",idFamdto);
%>


<script language="javascript">
if (document.SearchTab.artifizio.value ==
"<%= session.getAttribute("valore") %>" ) {
</script>

<html ption value="<%= desFam %>" />

<script language="javascript">}</script>

<% } %>

<% } %>
Giulietta Blumarine
Ranch Hand

Joined: Dec 03, 2004
Posts: 59
Post scriptum:

Sorry change the last three rows into these:

-----------------------
<script language="javascript">}</script>
<% } %>
</html:select>
<script language="javascript"> } </script>
----------------------------
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4742
    
    7

Giulietta,

As you have already seen, what you wrote won't work because JavaScript gets executed on the client side while the JSP Scriptlets get executed on the server side when the page is rendered. You will still need to mix Scriptlets in with the JavaScript code but you have to get it clear in your head when each part is executed and what is the result.

Here's an example that will hopefully set you on track: http://webdeveloper.earthweb.com/webjs/jsform/item.php/106591_viewit

The JavaScript may be a bit hard to follow if you are not very familiar with dealing with arrays and "low-level" access to form elements as JavaScript objects.

Start with the static JavaScript:



Now, you need to make the population of the JavaScript array dynamic, based on the contents of some collection. So, you have to mix in Scriptlets (or if you want to get more "up to current specs", use JSTL. Remember, to the JSP engine, the JavaScript shown above is just plain old text that it spits out, as is.



I have shown three different versions of the dynamic portion above: JSP 2.0 JSTL/EL, JSP 1.2 and prior JSTL/EL, and Scriptlets.

Apply the technique I show above to the sample JavaScript I have linked to. I'll leave the details to you as an exercise.

HTH
[ December 16, 2004: Message edited by: Junilu Lacar ]
Giulietta Blumarine
Ranch Hand

Joined: Dec 03, 2004
Posts: 59
Hi!
I didn't know JSTL/EL, but I have understood the concept.

I try.
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: INSERT THE VALUE OF A FORM FIELD INTO A JAVA STRING (without page refresh)