Spring MVC - Tiles,JSP页面包含js / jquery文件不起作用

时间:2014-03-06 11:30:45

标签: javascript jquery jsp spring-mvc tiles

我正在尝试开发基于Spring MVC和Apache Tiles的Java App。基本上一切都工作正常,直到我试图添加一些JavaScript到页面。 比页面不显示。也许你有任何想法,为什么它不起作用?

因为没有js它工作正常我觉得没问题,如果我赢了t copy the Java classes, but only the jsp s和配置文件......

所以Spring-servlet是:

<?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配置文件:

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

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>

最后是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时: - 仅显示页面标题(在本例中)

或者:

  • 有一条错误消息“与元素类型”script“关联的属性”src“的值不得包含'&lt;'字符。” (当使用{src =“时
你可以帮忙吗? 我如何将js包含在该项目中?

2 个答案:

答案 0 :(得分:2)

Spring MVC中的Javascript库

put-list-attribute - 首先将list属性转换为脚本变量;之后,使用<c:forEach>标记进行迭代。然后,复合属性将一个接一个地呈现。

在下面的示例中,我将在spring tiles中导入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>

答案 1 :(得分:1)

好的,我找到了解决方案......

创建jsp我通常选择JSP / JSF文件(xhtml,xml语法)。出于某种原因,在这些页面中运行jquery在我的项目中不起作用。我创建了一个简单的jsp文件(xhtml),js脚本和所有css运行没有问题。

如果有人知道原因,请分享:)