my dog learned polymorphism*
The moose likes Struts and the fly likes To call and load javascript in struts jsp. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "To call and load javascript in struts jsp." Watch "To call and load javascript in struts jsp." New topic
Author

To call and load javascript in struts jsp.

Manishhh Srivastva
Greenhorn

Joined: Aug 12, 2010
Posts: 2
Hi,
Is there anyway to load javascript in stuts application JSP.i am using the below javascript stuff.It works well with normal JSP and html, but in struts JSP its not getting called.Is the anyway to achieve in struts.Even body onload function not works with struts.
<script type="text/javascript">
var pager = new Pager('results', 2);
pager.init();
pager.showPageNav('pager', 'pageNavPosition');
pager.showPage(1);
</script>

Thnx in advance.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Welcome to JavaRanch!

What you're saying makes no sense: all the client sees is HTML; if the HTML being generated is correct, it will work. Something else is clearly happening, because countless people use JavaScript in their Struts JSPs all the time. You'll have to provide more details.
Prakash Mani - Attur
Ranch Hand

Joined: Oct 08, 2009
Posts: 100
Hi Manishhh,
Try to avoid onload in all the situations because onload gets loaded only when the page is completely downloaded.
Also if the library files you are using has already used onload you cannot use that again. I prefer you jQuery ready event instead onload. There are very good reasons,
i. it will load when the DOM is ready, will not wait for the page to get downloaded completely.
ii. you can use it as many times you want.
Manishhh Srivastva
Greenhorn

Joined: Aug 12, 2010
Posts: 2
below is my JSP.I am using ajax to post request.All javascript functions gets called on click.but
<script type="text/javascript">
alert('1111');
var pager = new Pager('results', 2);
pager.init();
pager.showPageNav('pager', 'pageNavPosition');
pager.showPage(1);
</script>
is not wriiten on click of any event.It never gets executes directly.even i dont get the alert...the same code if i keep as a functions and call it with onClick it gets called, but i cant use onClick..i want to load the stuff with JSP itself....It works well with normal JSP....Any ideas will be highly appreciated...I need to implement paging and its needed on JSP page....

Thnx in advance.



<%@ page import="java.util.List "%>
<%@ page import="com.fairpoint.omweb.web.form.CollocatedForm"%>
<%@ page import="com.fairpoint.omweb.persistence.CollocatedList"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>


<html:form styleId="collocatedForm" >
<% System.out.println("111111"); %>
<table>
<tr valign="top">
<td colspan="2"><logic:messagesPresent message="true">
<html:messages id="msg" message="true">
<font color="red"><b><bean:write name="msg" /></b></font>
<br />
</html:messages>
</logic:messagesPresent></td>
</tr>
<% System.out.println("222222222"); %>
<tr>
<td></td>
<td align="center"><b><font color="red">Collocated ACTLs</font></b></td>
<td></td>
</tr>
</table>
<hr width="100%" color="#99CCFF">
<% System.out.println("333333"); %>
<input type="hidden" name="indexCount" id="indexCount" value="0" />
<table id='addtab' border="1">
<tr bgcolor="#5CB3FF">
<!-- <td valign="top" align="left" width="5%"><input type="checkbox" name="checkboxValue" id="checkboxValue" onclick="selectAll();" /></td> -->
<td valign="top" align="left" width="5%">SELECT</td>
<td valign="top" align="left" width="10%"><b>CLLI CODE</b></td>
<td valign="top" align="left" width="10%"><b>CLLI TYPE</b></td>
<td valign="top" align="left" width="10%"><b>CLLI DECODE</b></td>
<td valign="top" align="left" width="10%"><b>ACNA</b></td>
<td valign="top" align="left" width="10%"><b>Last Modified UserName</b></td>
<td valign="top" align="left" width="15%"><b>Last Modified Date</b></td>

</tr>
<% System.out.println("444444"); %>
<bean:define id="collocationListSize" name="collocatedForm"
type="java.util.ArrayList" property="collocationList"></bean:define>
<% System.out.println("Size is:::" +collocationListSize.size());
int colSize = collocationListSize.size();
System.out.println("Size is:::" +colSize); %>
<input type="hidden" name="colSizeId" id="colSizeId" value="<%= colSize%>" />
<% System.out.println("usocListUtilList test : " + collocationListSize.getClass()); %>
<% CollocatedForm form = (CollocatedForm)request.getAttribute("collocatedForm");
System.out.println("1111122222");
List list = form.getCollocationList();
System.out.println("size of list is::::" +list.size());
CollocatedList listofColl =null;
%>
<tr>
<!--<td valign="top" align="left" width="13%"><input type="checkbox" name="selectedList" id="selectedList"/></td>-->

<% System.out.println("01"); %>
<td valign="top" align="left" width="10%">aaaaaaaa</td>
<td valign="top" align="left" width="5%">cccccccc</td>
<td valign="top" align="left" width="10%">ffffffffff</td>
<td valign="top" align="left" width="10%">hhhhhhhhh</td>
<td valign="top" align="left" width="5%">3333333333</td>
<td valign="top" align="left" width="10%">uuuuuuuuuuuuu</td>
<td valign="top" align="left" width="5%">33333</td>

<% System.out.println("02"); %>

</tr>
<tr>
<!--<td valign="top" align="left" width="13%"><input type="checkbox" name="selectedList" id="selectedList"/></td>-->

<% System.out.println("01"); %>
<td valign="top" align="left" width="10%">aaaaaaaa</td>
<td valign="top" align="left" width="5%">cccccccc</td>
<td valign="top" align="left" width="10%">ffffffffff</td>
<td valign="top" align="left" width="10%">hhhhhhhhh</td>
<td valign="top" align="left" width="5%">3333333333</td>
<td valign="top" align="left" width="10%">uuuuuuuuuuuuu</td>
<td valign="top" align="left" width="5%">33333</td>

<% System.out.println("02"); %>

</tr>
<tr>
<!--<td valign="top" align="left" width="13%"><input type="checkbox" name="selectedList" id="selectedList"/></td>-->

<% System.out.println("01"); %>
<td valign="top" align="left" width="10%">aaaaaaaa</td>
<td valign="top" align="left" width="5%">cccccccc</td>
<td valign="top" align="left" width="10%">ffffffffff</td>
<td valign="top" align="left" width="10%">hhhhhhhhh</td>
<td valign="top" align="left" width="5%">3333333333</td>
<td valign="top" align="left" width="10%">uuuuuuuuuuuuu</td>
<td valign="top" align="left" width="5%">33333</td>

<% System.out.println("02"); %>

</tr>
<tr>
<!--<td valign="top" align="left" width="13%"><input type="checkbox" name="selectedList" id="selectedList"/></td>-->

<% System.out.println("01"); %>
<td valign="top" align="left" width="10%">aaaaaaaa</td>
<td valign="top" align="left" width="5%">cccccccc</td>
<td valign="top" align="left" width="10%">ffffffffff</td>
<td valign="top" align="left" width="10%">hhhhhhhhh</td>
<td valign="top" align="left" width="5%">3333333333</td>
<td valign="top" align="left" width="10%">uuuuuuuuuuuuu</td>
<td valign="top" align="left" width="5%">33333</td>

<% System.out.println("02"); %>

</tr>
<tr>
<!--<td valign="top" align="left" width="13%"><input type="checkbox" name="selectedList" id="selectedList"/></td>-->

<% System.out.println("01"); %>
<td valign="top" align="left" width="10%">wwwwwwwwwwww</td>
<td valign="top" align="left" width="5%">cccccccc</td>
<td valign="top" align="left" width="10%">ffffffffff</td>
<td valign="top" align="left" width="10%">hhhhhhhhh</td>
<td valign="top" align="left" width="5%">3333333333</td>
<td valign="top" align="left" width="10%">uuuuuuuuuuuuu</td>
<td valign="top" align="left" width="5%">33333</td>

<% System.out.println("02"); %>

</tr>
<tr>
<!--<td valign="top" align="left" width="13%"><input type="checkbox" name="selectedList" id="selectedList"/></td>-->

<% System.out.println("01"); %>
<td valign="top" align="left" width="10%">hhhhhh</td>
<td valign="top" align="left" width="5%">cccccccc</td>
<td valign="top" align="left" width="10%">ffffffffff</td>
<td valign="top" align="left" width="10%">hhhhhhhhh</td>
<td valign="top" align="left" width="5%">3333333333</td>
<td valign="top" align="left" width="10%">uuuuuuuuuuuuu</td>
<td valign="top" align="left" width="5%">33333</td>

<% System.out.println("02"); %>

</tr>
<tr>
<!--<td valign="top" align="left" width="13%"><input type="checkbox" name="selectedList" id="selectedList"/></td>-->

<% System.out.println("01"); %>
<td valign="top" align="left" width="10%">00000000000</td>
<td valign="top" align="left" width="5%">cccccccc</td>
<td valign="top" align="left" width="10%">ffffffffff</td>
<td valign="top" align="left" width="10%">hhhhhhhhh</td>
<td valign="top" align="left" width="5%">3333333333</td>
<td valign="top" align="left" width="10%">uuuuuuuuuuuuu</td>
<td valign="top" align="left" width="5%">33333</td>

<% System.out.println("02"); %>

</tr>
<tr>
<!--<td valign="top" align="left" width="13%"><input type="checkbox" name="selectedList" id="selectedList"/></td>-->

<% System.out.println("01"); %>
<td valign="top" align="left" width="10%">11111</td>
<td valign="top" align="left" width="5%">22222</td>
<td valign="top" align="left" width="10%">ffffffffff</td>
<td valign="top" align="left" width="10%">hhhhhhhhh</td>
<td valign="top" align="left" width="5%">3333333333</td>
<td valign="top" align="left" width="10%">uuuuuuuuuuuuu</td>
<td valign="top" align="left" width="5%">33333</td>

<% System.out.println("02"); %>

</tr>
<% System.out.println("777777777"); %>
</table>
<div id="pageNavPosition"></div>

<script type="text/javascript">
var pager = new Pager('results', 2);
pager.init();
pager.showPageNav('pager', 'pageNavPosition');
pager.showPage(1);
</script>



<% System.out.println("8888888888888"); %>
<% List validFunction = (List)request.getSession().getAttribute("validUser");%>
<% if(validFunction != null){
if(validFunction.contains("COLLOCATED_ACTLS")){ %>
<br>
<input type='button' name='Add' value='Add Row' onclick="addRow('addtab')" />   
<input type='button' name='DeleteRow' value='Delete Row' onclick="deleteCollocatedRow('addtab')" />   
<input type='button' name='Delete' value='Delete' onClick="deleteCollocatedActls();"/>   
<input type="button" id="btnContinue" value="Save/Update" onClick="saveCollocatedActls();" />
<% }} %>
</html:form>
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.

Also, please try to only post relevant snippets--most of what you've posted is not directly related to the question at hand.

You'll still need to be more specific about how the JSP is being loaded, how you load the JavaScript, configuration details, etc. People use JavaScript in Struts-based JSP pages all the time without issue.
Prakash Mani - Attur
Ranch Hand

Joined: Oct 08, 2009
Posts: 100
Hi Manishhh
As i told you before, use jQuery ready funtion to load all the scripts you want to load when the page gets loaded..
Use the following into your script tag..

In addition to the above, download the jquery library file and import it in your page.. use the below link to download jquery library file jQuery lib file
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To call and load javascript in struts jsp.
 
Similar Threads
Implementing Paging
Comparing two string variables
Javascript does not executed for Struts File
html : form
Referencing Javascript