Javascript inside struts

  • Thread starter Jean-François Michaud
  • Start date
J

Jean-François Michaud

Hello,

I've been trying to figure something out for the past few days and I
can't seem to pinpoint what I'm doing wrong.

I've been trying to call javascript from inside a <logic:equal>, but
it doesn't seem to work like I want.

I've tried calling an alert('hello'); to test out if my call worked
correctly, and it does, but when I call my own javascript functions
that I create in the html/head, they don't seem to get executed. Any
idea why (except disbaleCif() which I call through onload)?

Here's a snippet of code:

<html>
<head>
<link REL="stylesheet" HREF="css/bear.css" TYPE="text/
css">
<script type="text/javascript">
function disableCif()
{

document.ManageKQADocumentForm.cifType1.disabled="true";

document.ManageKQADocumentForm.cifType2.disabled="true";

document.ManageKQADocumentForm.cifType3.disabled="true";
}

function enableCifType1()
{

document.ManageKQADocumentForm.cifType1.disabled="false";
}

function enableCifType2()
{

document.ManageKQADocumentForm.cifType2.disabled="false";
}

function enableCifType3()
{

document.ManageKQADocumentForm.cifType3.disabled="false";
}
</script>
</head>
<body class="MainBody" onload="javascript:disableCif();">
<logic:iterate id="cifType"
property="checkItemFormTypes"
name="manageKQADocumentTO">
<logic:equal value="1" name="cifType">
<script> alert('hello'); </script>
</logic:equal>
<logic:equal value="2" name="cifType">
<script> enableCifType2(); </script>
</logic:equal>
<logic:equal value="3" name="cifType">
<script> enableCifType3(); </script>
</logic:equal>
</logic:iterate>

Regards
Jean-Francois Michaud
 
V

VK

I've tried calling an alert('hello'); to test out if my call worked
correctly, and it does, but when I call my own javascript functions
that I create in the html/head, they don't seem to get executed. Any
idea why (except disbaleCif() which I call through onload)?

Firefox reports "document.ManageKQADocumentForm has no properties"
which is correct because neither HTML nor XHTML have element
ManageKQADocumentForm so UA doesn't have any DOM interface for it.

I am not clear what markup is that? It is not HTML nor XHTML for sure,
at the max it is a malformed invalid XML fragment - if served as text/
html then treated as broken HTML tag soup. In either case
ManageKQADocumentForm element doesn't appear by some magic.

It would help to understand what do you mean by "struts" in
application to (X)HTML then a workable solution could be suggested.
 
T

Tim Slattery

VK said:
I am not clear what markup is that? It is not HTML nor XHTML for sure,
at the max it is a malformed invalid XML fragment - if served as text/
html then treated as broken HTML tag soup. In either case
ManageKQADocumentForm element doesn't appear by some magic.

As he said, it's Struts (http://struts.apache.org), a Java-based web
application framework. The <logic> tags enable the page to be put
together conditionally on the server side. The javascript is all
client side, and I'd say your diagnosis is correct: the element he's
trying to enabled doesn't exist, at least not in the code he showed
us. And, even in Struts, that element won't appear by magic.
 
J

Jean-François Michaud

I'm sorry, I should have written down a complete section of code.

Here goes. ManageKQADocumentForm is a Form I have set up for the
current jsp page (ManageKQADocumentForm.java). The Form logic work. I
can transfer data around without problems to and from the Form and a
Transfer Object is used to communicate with a Database (DAO object).
My problem is not with the underlying strut logic. It's with the call
to a javascript function inside a <logic:equal> (the functions are in
the <head> tags). If I call an "alert('hello world');" inside the
<logic:equal>, it works, If I call my own function, it doesnt.

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<bean:define id="manageKQADocumentTO" name="ManageKQADocumentTO"></
bean:define>
<jsp:include page="/jsp/common/header.jsp" flush="true" />
<jsp:include page="/jsp/common/menu.jsp" flush="true" />
<jsp:include page="/jsp/common/errorpage.jsp" flush="true" />

<html>
<head>
<link REL="stylesheet" HREF="css/bear.css" TYPE="text/css">
<script type="text/javascript">
function disableCif()
{
document.ManageKQADocumentForm.cifType1.disabled="true";
document.ManageKQADocumentForm.cifType2.disabled="true";
document.ManageKQADocumentForm.cifType3.disabled="true";
}

function enableCifType1()
{
document.ManageKQADocumentForm.cifType1.disabled="false";
}

function enableCifType2()
{
document.ManageKQADocumentForm.cifType2.disabled="false";
}

function enableCifType3()
{
document.ManageKQADocumentForm.cifType3.disabled="false";
}
</script>
</head>
<body class="MainBody" onload="javascript:disableCif();">
<logic:iterate id="cifType" property="checkItemFormTypes"
name="manageKQADocumentTO">
<logic:equal value="1" name="cifType">
<script> alert('hello'); </script>
</logic:equal>
<logic:equal value="2" name="cifType">
<script> enableCifType2(); </script>
</logic:equal>
<logic:equal value="3" name="cifType">
<script> enableCifType3(); </script>
</logic:equal>
</logic:iterate>
<form name="ManageKQADocumentForm">
<table border="0" width="100%" class="TableBody" cellspacing="0"
cellpadding="0">
<tr>
<td WIDTH="100%" CLASS="PageHeading">KQA Edit Management</td>
</tr>
</table>
<br>
<table border="0" width="100%" class="TableBody" cellspacing="0"
cellpadding="0">
<tr>
&nbsp;&nbsp;&nbsp;&nbsp;Enter a Service Bulletin
number.&nbsp;&nbsp;&nbsp;&nbsp;<input type="button"
name="sbnumbersubmit" value="Submit"
onclick="javascript:sbnumberCheck()"/>
</td>
</tr>
</table>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td valign="middle" align="left">
<input type="radio" name="cifType1" setGroup="group1"
value="1"/>&nbsp;&nbsp;&nbsp;&nbsp;
<% int count = 0; %>
<logic:iterate id="cifTypeLookup"
property="checkItemFormTypesLookup" name="manageKQADocumentTO">
<% if(count==0){ %>
<bean:write name="cifTypeLookup"/>
<% } count++; %>
</logic:iterate>
</td>
</tr>
<tr>
<td valign="middle" align="left">
<input type="radio" name="cifType2" setGroup="group1"
value="2"/>&nbsp;&nbsp;&nbsp;&nbsp;
<% count = 0; %>
<logic:iterate id="cifTypeLookup"
property="checkItemFormTypesLookup" name="manageKQADocumentTO">
<% if(count==1){ %>
<bean:write name="cifTypeLookup"/>
<% } count++; %>
</logic:iterate>
</td>
</tr>
<tr>
<td valign="middle" align="left">
<input type="radio" name="cifType3" setGroup="group1"
value="3"/>&nbsp;&nbsp;&nbsp;&nbsp;
<% count = 0; %>
<logic:iterate id="cifTypeLookup"
property="checkItemFormTypesLookup" name="manageKQADocumentTO">
<% if(count==2){ %>
<bean:write name="cifTypeLookup"/>
<% } count++; %>
</logic:iterate>
</td>
</tr>
</table>
</form>
</body>
<script language="javascript">
function sbnumberCheck()
{
if(document.ManageKQADocumentForm.sbnumber.value=="")
{
alert('Please enter valid Service Bulletin number.');
}
else
{
document.ManageKQADocumentForm.action="UpdateKQAViewPage.do";
document.body.style.cursor = "wait";
document.ManageKQADocumentForm.submit();
}
}
</script>
</html>

Regards
Jean-Francois Michaud
 
M

Martin Honnen

Jean-François Michaud said:
function disableCif()
{
document.ManageKQADocumentForm.cifType1.disabled="true";
document.ManageKQADocumentForm.cifType2.disabled="true";
document.ManageKQADocumentForm.cifType3.disabled="true";

JavaScript has boolean values true and false while "true" is a string
and not a boolean value. You should assign true and not "true"
therefore. The same for the other functions.
<body class="MainBody" onload="javascript:disableCif();">

This should work as it is called after the page has been loaded.


<script> enableCifType2(); </script>

This can't work as the form you are trying to script has not yet been
parsed.

<script> enableCifType3(); </script>

Same here.
 
J

Jean-François Michaud

and not a boolean value. You should assign true and not "true"
therefore. The same for the other functions.

Ahhhh!! Thats the problem then. The form hasn't been parsed yet!
Alright, I'll have to find another way of doing this then. Thanks ;-).

Regards
Jean-Francois Michaud
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top