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



我正在尝试开发一个基于Spring MVC和Apache Tiles的Java应用程序。基本上一切都很好,直到我尝试在页面中添加一些javascript。然后不显示页面。也许你有任何想法,为什么它不起作用?

由于它在没有js的情况下运行良好,我认为这是可以的,如果我赢得了t copy the Java classes, but only the jspS和配置文件。。。

因此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将显示在网页上,但当我尝试包含javascript时:-仅显示页面的标题(在这种情况下)

或者:

  • 有一条错误消息"与元素类型"script"关联的属性"src"的值不能包含'<'字符。"?我该如何将js包含到该项目中?

Spring MVC中的Javascript库

put list attribute-列表属性首先转换为脚本变量;之后使用CCD_ 2标签进行迭代。然后将一个接一个地渲染复合属性。

在下面的例子中,我正在导入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>

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

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

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

最新更新