kamil.rak
kamil.rak

Reputation: 1378

Spring MVC - Tiles, JSP Page include js/jquery file does not work

I am trying to develop a Java App that is based on Spring MVC and Apache Tiles. Basically everything is working fine, until I am trying to add some javascript to the page. Than the page is not displayed. Maybe you have any Idea, why it is not working?

Since it is working fine without js I think it is ok, if I wont copy the Java classes, but only the jsps and config files...

So the Spring-servlet is:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
 http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>

<context:property-placeholder location="classpath:resources/database.properties" />
<context:component-scan base-package="com.kamirru" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />


<context:annotation-config />



<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.tiles2.TilesView">
        <!-- <value> </value> -->
    </property>
</bean>
<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/config/tiles.xml</value>
        </list>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.kamirru.model.Rez</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        </props>
    </property>
</bean>

<bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>


<mvc:resources location="/resources/*" mapping="/resources/**"/>
<mvc:annotation-driven />

Tiles config file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
   "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>

<!-- Widok: Header, body, footer -->

<definition name="nomenu" template="/WEB-INF/views/welcomeTemplate.jsp">
    <put-attribute name="title" value=""></put-attribute>
    <put-attribute name="header" value="/WEB-INF/views/header.jsp"></put-attribute>
    <put-attribute name="body" value=""></put-attribute>
    <put-attribute name="footer" value="/WEB-INF/views/footer.jsp"></put-attribute>
</definition>

<definition name="welcome" extends="nomenu">
    <put-attribute name="body" value="/WEB-INF/views/welcome.jsp"></put-attribute>
</definition>

</tiles-definitions>

The web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
    <servlet-name>sdnext</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/sdnext-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<!--    <servlet-mapping>
    <servlet-name>statCont</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping> -->

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



</web-app>

And finally the welcome.jsp:

<?xml version="1.0" encoding="ISO-8859-2" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:spring="http://www.springframework.org/tags"
xmlns:h="http://java.sun.com/jsf/html" version="2.0">
<jsp:directive.page language="java"
    contentType="text/html; charset=ISO-8859-2" pageEncoding="ISO-8859-2" />
<jsp:text>
    <![CDATA[ <?xml version="1.0" encoding="ISO-8859-2" ?> ]]>
</jsp:text>
<jsp:text>
    <![CDATA[ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ]]>
</jsp:text>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>



<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js_main/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/resources/js_slides/jquery.cycle2.js"></script> 



<!-- 
 <script type="text/javascript" src="<c:url value="/resources/js_main/jquery.min.js" />"></script>
 <script type="text/javascript" src="<c:url value="/resources/js_slides/jquery.cycle2.js" />"></script>
 --> 



 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2" />
<title>Insert title here</title>
</head>
<body>
<center>

   <div class="cycle-slideshow"
    data-cycle-fx="scrollHorz"
  data-cycle-pause-on-hover="false"
  data-cycle-speed="2000"
>
<img src="${pageContext.request.contextPath}/resources/img/1.jpg"></img>
<img src="${pageContext.request.contextPath}/resources/img/2.jpg"></img>
</div>
</center>
</body>
</html>
</jsp:root>

As you can see the

<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js_main/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/resources/js_slides/jquery.cycle2.js"> </script> 

are not commented. If I comment it, the img`s will be displayed on the webpage, but when I am trying to include the javascripts it: - shows only the header of the page (in this case)

Or:

Could you please help? How could I include the js to that project?

Upvotes: 2

Views: 2649

Answers (2)

atish shimpi
atish shimpi

Reputation: 5023

Javascript libraries in Spring MVC

put-list-attribute - The list attribute is first converted into a scripting variable; after that it is iterated using the <c:forEach> tag. The compound attributes are then rendered one after the other.

In following example I am importing jquery library inside spring tiles

tiles defination

<definition name="DefaultTemplate" template="/WEB-INF/views/jsp/layouts/layout.jsp">
    <put-list-attribute name="javascripts">
            <add-attribute value="/resources/js/custom/jquery-1.4.4.min.js" />
            <add-attribute value="/resources/js/jquery/jquery-ui-1.8.6.custom.min.js" />
    </put-list-attribute>
</definition>

layout jsp page

<c:forEach var="script" items="${javascripts}">
    <script src="<c:url value="${script}"/>"></script>
</c:forEach>

Upvotes: 2

kamil.rak
kamil.rak

Reputation: 1378

Ok, I have found the solution...

Creating a jsp I usually choose JSP/JSF file (xhtml, xml syntax). For some reason running the jquery in those pages does not work in my project. I have created a simple jsp file (xhtml) and the js scripts and all css run without problems.

If someone knows the reason, please share:)

Upvotes: 1

Related Questions