Spring MVC - Плитки, страница JSP включает файл js/jquery, не работает

Я пытаюсь разработать приложение Java, основанное на Spring MVC и Apache Tiles. В основном все работает нормально, пока я не пытаюсь добавить javascript на страницу. Чем страница не отображается. Может быть, у вас есть идеи, почему это не работает?

Поскольку он отлично работает без js, я думаю, что все в порядке, если я выигралt copy the Java classes, but only the jsps и файлы конфигурации...

Итак, Spring-сервлет:

<?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>

Веб.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>

И, наконец, файл 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>

Как вы можете видеть

<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> 

не комментируются. Если я прокомментирую это, img будут отображаться на веб-странице, но когда я пытаюсь включить javascripts, он: - показывает только заголовок страницы (в данном случае)

Or:

  • There is an error message "The value of attribute "src" associated with an element type "script" must not contain the '<' character." (when Using {src="

Не могли бы вы помочь? Как я могу включить js в этот проект?


person kamil.rak    schedule 06.03.2014    source источник


Ответы (2)


Библиотеки JavaScript в Spring MVC

put-list-attribute — атрибут списка сначала преобразуется в переменную сценария; после этого он повторяется с использованием тега <c:forEach>. Затем составные атрибуты отображаются один за другим.

В следующем примере я импортирую библиотеку jquery внутри пружинных плиток.

определение плитки

<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>

макет jsp-страницы

<c:forEach var="script" items="${javascripts}">
    <script src="<c:url value="${script}"/>"></script>
</c:forEach>
person atish shimpi    schedule 20.12.2015

Хорошо, я нашел решение...

При создании jsp я обычно выбираю файл JSP/JSF (синтаксис xhtml, xml). По какой-то причине запуск jquery на этих страницах не работает в моем проекте. Я создал простой файл jsp (xhtml), а сценарии js и все css работают без проблем.

Если кто-то знает причину, поделитесь, пожалуйста :)

person kamil.rak    schedule 07.03.2014