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 pass <bean:write> to a function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "pass <bean:write> to a function" Watch "pass <bean:write> to a function" New topic
Author

pass <bean:write> to a function

Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Hi,
I want to pass <bean:write> to a javascript function. But when i run this code i get an error saying 'EQUALS SYMBOL EXPECTED'.

Can't we use <bean:write> as an argument to pass it to the function?

<logic:iterate id="Products" name="Products">
<tr>

<td >
<%=i %>.
</td>

<td >
<bean:write name = "Products" property="products"/>
</td>
<td >
<html:text name = "Products" property="nos_BL" size="7" onchange="calcBBSeries('<bean:write name = "Products" property="products"/>');" ></html:text>
</td>
</tr>
</logic:iterate>
function calcBBSeries(product)
{

alert(product);
document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries";
document.forms[0].submit();
}

Please help.
Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The issue has nothing to do with JavaScript; you're nesting custom tags, which isn't legal.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
David Newton wrote:The issue has nothing to do with JavaScript; you're nesting custom tags, which isn't legal.


So how do i pass the value to the function?
Any other way to do it?

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

JSP EL if you're on a JSP 2+ container, or the Struts 1 EL tags if not, I guess.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
David Newton wrote:JSP EL if you're on a JSP 2+ container, or the Struts 1 EL tags if not, I guess.


Hi,
I tried the following:

1) Kept struts-el.jar in my proj folder.
2) <%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %> included in my jsp
3)<td >
<html-el:text name = "Products" property="nos_BL" size="7" onchange="calcBBSeries('${Products.products}');"></html-el:text>
</td>

function calcBBSeries(product)
{

alert(product);

}

But when i change the value in the text field i am not getting any alert. What am i doing wrong?

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

Because you're setting the string passed to JavaScript at page rendering time--you're not passing the current value of the text box.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
David Newton wrote:...

Because you're setting the string passed to JavaScript at page rendering time--you're not passing the current value of the text box.


I did not get you. So you mean to say $(Products.products) is not the current value of the text box?

I am confused.

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, ${Products.product} is not the current value of the text box. It's the value of ${Products.products} when the page is rendered.

JSP EL and custom tags execute on the *server* side, not on the *client* side (the browser).
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
David Newton wrote:No, ${Products.product} is not the current value of the text box. It's the value of ${Products.products} when the page is rendered.

JSP EL and custom tags execute on the *server* side, not on the *client* side (the browser).


So, now what do i do in such a case. There has to be some alternative to this.
Please help. Cant go further without this.

Thanks
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Deeps Mistry wrote:
David Newton wrote:No, ${Products.product} is not the current value of the text box. It's the value of ${Products.products} when the page is rendered.

JSP EL and custom tags execute on the *server* side, not on the *client* side (the browser).


So, now what do i do in such a case. There has to be some alternative to this.
Please help. Cant go further without this.

Thanks

Okay, see what i have done is this:
This is how my jsp gets displayed

S/No. Products Nos. of B/L(text box) Boxes/Loose(Drop down) BB Series Last Box No
1. Drawer Select
2. Standard Units Select


In Nos. of B/L column we need to type in a number and soon as you are done, you get a BB series printed under BB Series column.
So what i have done in my jsp is as follows:

<table width="515" height="172" border="0" cellpadding="0" cellspacing="0" bgcolor="#FAFAFA" >

<tr>

<td width="37"><div ><strong>S/No. </strong></div></td>

<td width="100"><strong> Products</strong></td>
<td width="100"><strong> Nos. of B/L</strong></td>
<td width="115" ><strong>Boxes/Loose</strong></td>
<td width="120"><div><strong>BB Series</strong></div></td>
<td width="120"><div><strong>Last Box No.</strong></div></td>
</tr>
<% int i = 1; %>
<logic:iterate id="Products" name="Products">
<tr>

<td width="37">
<%=i %>.
</td>

<td width="100">

<bean:write name = "Products" property="products"/>
</td>
<td width="100" align="center">

<html:text name = "Products" property="nos_BL" size="7" onchange="calcBBSeries();"></html:text>

</td>
<td width="97" align="center">
<html:select name="Products" property="BL">
<html:option value="Select"></html:option>
<html:option value="Boxes"></html:option>
<html:option value="Loose"></html:option>
</html:select>
</td>
<td width="47">
<bean:write name = "Products" property="bbSeries" />

</td>
<td width="47">
<bean:write name = "Products" property="lastboxno" />
</td>
</tr>
<% i++; %>
</logic:iterate>

</table>

function calcBBSeries()
{
document.forms[0].action = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries";
document.forms[0].submit();
}

Now as soon as i enter a value in the text box, my action class is called which calculates the BB series. But the problem is i want the product name to calculate BB series.
I am not able to send the product name against which the text has been entered.
How do i achieve this?
Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh; if you want to do that without refreshing the entire page you'll need to use Ajax in some way, either manually or with Ajax custom tags.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
David Newton wrote:Oh; if you want to do that without refreshing the entire page you'll need to use Ajax in some way, either manually or with Ajax custom tags.


Hi,
As per your suggestion, i read about Ajax + struts examples.

In my case, the problem i am facing is that i am not able to pass the product value against which the nos_bl field text is changed. Even if i use Ajax, i will have to pass the product value to the function.
So in either case, the problem would be the same: of passing product value.

So now how do i go about it?

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

That would depend on how you're implementing the Ajax functionality. If the value is in a text box then get the value out of the text box (that's a JavaScript, not Struts, issue) and use it as one of the parameters in the Ajax call.
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
David Newton wrote:...

That would depend on how you're implementing the Ajax functionality. If the value is in a text box then get the value out of the text box (that's a JavaScript, not Struts, issue) and use it as one of the parameters in the Ajax call.


Hi,

I have implemented the AJAX functionality. But my action class is not being called :(

Here is my code:
function ajaxFunction()
{
//Do the Ajax call

url = "http://localhost:8080/struts-blank/challan.do?method=calcBBSeries";


if (window.XMLHttpRequest) { // Non-IE browsers
req = new XMLHttpRequest();
req.onreadystatechange = processStateChange;
try {
req.open("GET", url, true); //was get
alert('open')
} catch (e) {
alert("Problem Communicating with Server\n"+e);
}
req.send(null);
} else if (window.ActiveXObject) { // IE

req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = processStateChange;
req.open("GET", url, true);
req.send();
}
}
}

function processStateChange() {

if (req.readyState == 4) { // Complete
if (req.status == 200) { // OK response
alert('ready')
//alert("Ajax response:"+req.responseText);
document.forms[0].bbseries.value = req.responseText;

} else {
alert("Problem with server response:\n " + req.responseText);
}
}
}


<html:text name="Challan" property="perbox" styleId='<%="pb" + ids%>' disabled="true" size="5" onchange="ajaxFunction();"></html:text>


Please please help.

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

*Please* use code tags when posting code.

This is really outside the realm of Struts... you can use tools like Firebug, proxies, etc. to see if the request is being made correctly, examine return codes, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass <bean:write> to a function
 
Similar Threads
How do I iterate the object which is in Session (in a JSP)?
pass a variable in the URl
enable/disable textbox
javascript function not being called
document.form.action problem