javascript - 脚本标签未在 JSP 页面中呈现(使用 Spring + Tiles + JSPX)

标签 javascript spring tiles jspx

各位

我遇到了一个奇怪的问题,包括 jsp 页面中的脚本标签。在我包含的三个脚本中,只有第一个脚本出现在最后一页。 这是我定义布局的方式

<?xml version="1.0" encoding="UTF-8"?>
<!--$Id$ -->
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
 <definition name="default" template="/WEB-INF/layouts/default.jspx">
  <put-attribute name="header" value="/WEB-INF/views/header.jspx" />
  <put-attribute name="submenu" value="/WEB-INF/views/submenu.jspx" />
  <put-attribute name="body" value="/WEB-INF/views/body.jspx" />
  <put-attribute name="footer" value="/WEB-INF/views/footer.jspx" />
 </definition>
 <definition name="registration" template="/WEB-INF/layouts/registration.jspx">
  <put-attribute name="header" value="/WEB-INF/views/header.jspx" />
  <put-attribute name="body" value="/WEB-INF/views/body.jspx" />
 </definition> 
</tiles-definitions>

View 定义如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
  "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
  "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
  <tiles-definitions>
   <definition extends="registration" name="register/default">
    <put-attribute name="body" value="/WEB-INF/views/register/register.jspx"/>
   </definition>     
  </tiles-definitions>

这是页面代码(registration.jspx)

<html xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:tiles="http://tiles.apache.org/tags-tiles" xmlns:c="http://java.sun.com/jsp/jstl/core" version="2.0">
<jsp:output doctype-root-element="HTML" doctype-system="about:legacy-compat" />
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:directive.page pageEncoding="UTF-8" />
<jsp:output omit-xml-declaration="true" />
<head>
<c:url var="rootUrl" value="/resources/" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<link rel="stylesheet" type="text/css" href="/resources/css/screen.css" />
<link rel="stylesheet" type="text/css" href="/resources/css/openid.css" />
<link rel="stylesheet" type="text/css" href="/resources/css/openid-shadow.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"/>
<script type="text/javascript" src="/resources/js/openid-jquery.js"/>
<script type="text/javascript" src="/resources/js/openid-en.js"/>
<script type="text/javascript">
//<![CDATA[
    $(document).ready(function() {
        openid.init('openid_identifier');
    });
//]]>   
</script>
<title>Mystery Shopping</title>
</head>
<body>
    <tiles:insertAttribute name="header" />
    <tiles:insertAttribute name="body" />
</body>
</html>

然后在名为 register.jspx 的 jspx 页面中覆盖 boby 属性,该页面包含带有 openid-selector 的表单。资源文件夹也已经映射

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-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.xsd">

 <!-- Scans within the base package of the application for @Components to configure as beans -->
 <context:component-scan base-package="vm.brands.controller" />

 <mvc:resources mapping="/resources/**" location="/resources/" />

 <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

</beans>

当我访问此注册页面的 url 时,我看到以下代码(通过 firebug 检查元素时)

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=8">
<link rel="stylesheet" type="text/css" href="/resources/css/screen.css">
<link rel="stylesheet" type="text/css" href="/resources/css/openid.css">
<link rel="stylesheet" type="text/css" href="/resources/css/openid-shadow.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js">
<title>Mystery Shopping</title>
</head>

当我查看页面源代码(未格式化)时,我确实看到了原本不可见的脚本标签;但是它们“不会”像源代码 View 中的其他标签一样格式化。

<html version="2.0"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/><meta content="IE=8" http-equiv="X-UA-Compatible"/><link href="/resources/css/screen.css" type="text/css" rel="stylesheet"/><link href="/resources/css/openid.css" type="text/css" rel="stylesheet"/><link href="/resources/css/openid-shadow.css" type="text/css" rel="stylesheet"/><script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"/><script src="/resources/js/openid-jquery.js" type="text/javascript"/><script src="/resources/js/openid-en.js" type="text/javascript"/><script type="text/javascript">
    $(document).ready(function() {
        openid.init('openid_identifier');
    });

我完全不知道这种奇怪行为的原因。

已解决:但弹出一个新问题

感谢拉尔夫的帮助;我尝试了你的建议

<spring:url value="/resources/js/openid-jquery.js" var="openid-jquery_url"/>
<spring:url value="/resources/js/openid-en.js" var="openid-en_url"/>
<script src="${openid-jquery_url}" type="text/javascript"><jsp:text> </jsp:text></script>
<script src="${openid-en_url}" type="text/javascript"><jsp:text> </jsp:text></script>

呈现 html(再次不知道为什么;我有点太分层了,无法搜索它;-))

<script src="0" type="text/javascript">
<script src="0" type="text/javascript">

但是让这个表格起作用了

<c:url var="rootUrl" value="/resources/" />
<script src="${rootUrl}js/openid-jquery.js" type="text/javascript"><jsp:text> </jsp:text></script>
<script src="${rootUrl}js/openid-en.js" type="text/javascript"><jsp:text> </jsp:text></script>

这给出了预期的输出

<script src="/resources/js/openid-jquery.js" type="text/javascript">
<script src="/resources/js/openid-en.js" type="text/javascript">

是 spring:url 标签有问题还是我遗漏了什么

最佳答案

以这种格式使用 Java Script 标签,因为如果 script 标签没有内容,某些浏览器会遇到问题:

<spring:url value="/resources/js/openid-jquery.js" var="openid-jquery_url" />
<script src="${openid-jquery_url}" type="text/javascript"><!-- required for FF3 and Opera --></script>

或更“安全”

<script src="${openid-jquery_url}" type="text/javascript"><!-- required for FF3 and Opera --><jsp:text> </jsp:text></script>

因为这会阻止 jspx 解析器最小化标记。

关于javascript - 脚本标签未在 JSP 页面中呈现(使用 Spring + Tiles + JSPX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9434755/

相关文章:

javascript - 关于更改 url 或加载 url 的 Jquery 问题

jquery - 平铺异步加载的图像 - 卡在 <img> 和背景 URL 之间

java - 如何找到Spring中使用注解执行的代码?

java - Spring Web App - 由 Java.lang.ClassNotFoundException 引起的 CannotLoadBeanClassException

java - 为什么 JMS MessageListener 中使用的实体管理器不参与 JTA 事务?

javascript - 如何用html/css制作智能磁贴?

android - 在安卓手机上显示巨大的平铺图像

javascript - 如何让 intro js 与 React 一起工作?

javascript - 仅使用 css 和 js 在网页上添加一个新部分

javascript - 如何在多个 html 元素上重复相同的 Javascript 代码