PDA

View Full Version : Problems including javascript


jamin
Feb 10th, 2006, 09:37 AM
Hi,

Im tryng to learn spring and am implementing a e shop framework, i have all my service and data acess working but am having trouble with the web stuff.

I have a jsp called home.jsp that im trying to show a category tree on. Im trying to use the javascript from this site http://www.destroydrop.com/javascripts/tree/
but when i deploy the webapp the home.jsp cant load the dtree.js.

Heres my setup.....

Directory layout

web
index.jsp
images
js
dtree.js
WEB-INF
classes
jsp
home.jsp

My web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<context-param>
<param-name>contextConfigLocations</param-name>
<param-value>
/WEB-INF/shop-data.xml,/WEB-INF/applicationContext.xml, /WEB-INF/shop-servlet.xml
</param-value>
</context-param>

<servlet>
<servlet-name>shop</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>shop</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/fmt.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
<taglib-location>/WEB-INF/sql.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
<taglib-location>/WEB-INF/x.tld</taglib-location>
</taglib>

</web-app>

My shop-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyP laceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/jdbc.properties</value>
</list>
</property>
</bean>



<!-- TransactionInterceptor -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.Transa ctionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
</props>
</property>
</bean>


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTran sactionManager">
<property name="dataSource"><ref local="dataSource"/></property>
</bean>

<!-- DAO -->
<bean id="dao" class="com.benshort.dao.jdbc.JdbcCatalogDao">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>

<!-- Business Objects -->
<bean id="catalogTarget" class="com.benshort.service.CatalogImp">
<property name="catalogDao">
<ref bean="dao"/>
</property>
</bean>

<bean name="/home.html" class="com.benshort.web.HomePageController" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default" >

<property name="catalog">
<ref bean="catalogTarget"/>
</property>

</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
</bean>
</beans>

My index.jsp
<jsp:forward page="home.html" />

My home.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>


<html>
<head>
<link rel="stylesheet" type="text/css" media="print" href="<c:url value='/style/dtree.css'/>"/>

<script type="text/javascript" src="<c:url value='/js/dtree.js'/>"></script>
</head>

<body>

<div class="dtree">

<p><a href="javascript: d.openAll();">open all</a> | <a href="javascript: d.closeAll();">close all</a></p>

<script type="text/javascript">


d = new dTree('d');

d.add(0, -1, 'My example tree');
d.add(1, 0, 'Node 1', 'example01.html');
d.add(2, 0, 'Node 2', 'example01.html');
d.add(3, 1, 'Node 1.1', 'example01.html');
d.add(4, 0, 'Node 3', 'example01.html');
d.add(5, 3, 'Node 1.1.1', 'example01.html');
d.add(6, 5, 'Node 1.1.1.1', 'example01.html');
d.add(7, 0, 'Node 4', 'example01.html');
d.add(8, 1, 'Node 1.2', 'example01.html');
d.add(9, 0, 'My Pictures', 'example01.html', 'Pictures I\'ve taken over the years', '', '', 'img/imgfolder.gif');
d.add(10, 9, 'The trip to Iceland', 'example01.html', 'Pictures of Gullfoss and Geysir');
d.add(11, 9, 'Mom\'s birthday', 'example01.html');
d.add(12, 0, 'Recycle Bin', 'example01.html', '', '', 'img/trash.gif');

document.write(d);


</script>

</div></body>

</html>

jamin
Feb 10th, 2006, 09:38 AM
My controller

package com.benshort.web;

import org.springframework.web.servlet.mvc.AbstractContro ller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.benshort.service.ICatalog;

import java.util.Map;
import java.util.HashMap;

public class HomePageController extends AbstractController
{
private ICatalog mCatalog = null;

protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception
{


Map model = new HashMap();
model.put("categoryTree", mCatalog.getCategoryTree());


return new ModelAndView("home", model);
}

public void setCatalog(ICatalog catalog)
{
mCatalog = catalog;
}


}

EndlessWinter
Feb 10th, 2006, 10:21 AM
your dtree.js file is located in the root, not in js folder

jamin
Feb 10th, 2006, 10:27 AM
No its in the js directory... the formating got screwed up.

Directory layout

web
index.jsp
images
js
dtree.js
WEB-INF
jsp
home.jsp

EndlessWinter
Feb 10th, 2006, 10:49 AM
1) Have you checked the result html for
<script type="text/javascript" src="<c:url value='/js/dtree.js'/>"></script>
expression from your jsp?
2) Can you access the script by direct link (filling in the address bar in the browser with URL of the script [localhost:xxx/web/js/dtree.js])?

jamin
Feb 10th, 2006, 10:55 AM
EndlessWinter,

1. It resolves to the following

<html>
<head>
<link rel="stylesheet" type="text/css" media="print" href="/shop/style/dtree.css"/>

<script type="text/javascript" src="/shop/js/dtree.js"></script>
</head>

2. And i get the following when trying to access it directly

HTTP Status 404 - /shop/js.dtree.js

type Status report

message /shop/js/dtree.js

description The requested resource (/shop/js/dtree.js) is not available.
Apache Tomcat/5.5.12


shop is the name of the deployed webapp

EndlessWinter
Feb 10th, 2006, 11:06 AM
then, no more ideas. Sorry. 404 error says, that there are no such file, and we can do noting, except checking the web-app structure twice .....

jamin
Feb 10th, 2006, 11:22 AM
EndlessWinter,

Many thanks for you help.

I wondered if the directory listing was disabled, so i checked tomcats web.xml.

While looking through I remember I had been fiddling around with it to try and get another webapp deployed as the ROOT webapp. So i changed the servlet mapping back to the following and it all works!!

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>