Date: Mon, 12 Jul 2010 12:57:47 -0400
From: jcafar...@gmail.com
To: user@struts.apache.org
Subject: Javascript issues
I'm trying to incorporate some ajax stuff with struts2 but I'm running
into problems.
I have this action:
public class AjaxUserBrowser extends ActionSupport
{
private List<User> users;
public String execute()
{
System.out.println("AJAX USER BROWSER");
setUsers(getPortfolioService().getUsers());
return SUCCESS;
}
public PortfolioService getPortfolioService( )
{
return new PortfolioService();
}
public void setUsers(List<User> users)
{
this.users = users;
}
public List<User> getUsers()
{
return users;
}
}
It's very simple. It doesn't really do anything.
Then I have this resulting jsp page:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script src="ajax-user-browser.js" type="text/javascript" />
<title>Title</title>
</head>
<body>
<h5>Artist Browser Control</h5>
<s:form action="AjaxRetrieveUser">
<s:select name="username" list='users' listKey="username"
listValue="username" label="Select an artist" value="defaultUsername"
onchange="fetchUser();"/>
</s:form>
<hr/>
<h5>Artist Information</h5>
<div id='console'>
<p>Name: <s:property value="defaultUser.firstName"/> <s:property
value="defaultUser.lastName"/></p>
<s:iterator value="defaultUser.portfolios">
<p>PortfolioName: <s:property value="value.name" /></p>
</s:iterator>
</div>
</body>
</html>
When I click the link to the action, nothing appears on the resulting
page. The action fires (print statement is printed) but no resulting
page is shown. If I view the source, the html is right, and if I take
out the <script> line, the page is shown but doesn't do anything obviously
Here is the javascript file:
var req=null;
var console=null;
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;
function sendRequest ( url, params, HttpMethod ) {
if ( !HttpMethod ){
HttpMethod="GET";
}
req=initXMLHTTPRequest();
if ( req ) {
req.onreadystatechange=onReadyState;
req.open(HttpMethod, url, true );
req.setRequestHeader ( "Content-Type",
"application/x-www-form-urlencoded");
req.send (params);
}
}
function initXMLHTTPRequest(){
var xRequest=null;
if (window.XMLHttpRequest) {
xRequest=new XMLHttpRequest();
} else if ( window.ActiveXObject ){
xRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
return xRequest;
}
function onReadyState() {
var ready=req.readyState;
var jsonObject=null;
if ( ready == READY_STATE_COMPLETE ){
jsonObject=eval( "("+ req.responseText +")" );
toFinalConsole ( jsonObject );
}
}
function toFinalConsole(jsonObject){
if (console!=null){
removeAllChildren ( console );
var div = document.createElement("p");
var txt=document.createTextNode("Name: " +
jsonObject.artist.firstName + " " + jsonObject.artist.lastName );
div.appendChild ( txt );
console.appendChild(div);
//mess of Javascript references because we didn't mediate the JSON
interpretation of our maps, etc.
var portfolios = jsonObject.artist.portfolios.entry;
var portfolioCount = portfolios.length;
for ( var index = 0; index < portfolioCount; index++ ) {
var portfolio = portfolios[index];
txt=document.createTextNode("Portfolio Name: " +
portfolio['string'] );
div = document.createElement("p");
div.appendChild ( txt );
console.appendChild(div);
}
}
}
function removeAllChildren( node ){
var childCount = node.childNodes.length;
for ( var count = 1; count <= childCount; count++) {
node.removeChild ( node.childNodes[0] );
}
}
function fetchUser()
{
console=document.getElementById('console');
var selectBox = document.getElementById('AjaxRetrieveUser_username');
var selectedIndex = selectBox.selectedIndex;
var selectedValue = selectBox.options[selectedIndex].value
sendRequest("AjaxRetrieveUser.action", "username=" + selectedValue ,
"POST");
}
Maybe there's an error in the javascript? Is there a debugger or something?
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org