JSP页面中未呈现的脚本标记(使用Spring+Tiles+JSPX)

Script tags not rendered in JSP page (using Spring + Tiles + JSPX)

本文关键字:使用 Spring+Tiles+JSPX 脚本 JSP      更新时间:2023-09-26

所有人,

我正面临一个奇怪的问题,包括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>

视图定义如下:

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

boby属性随后在一个名为register.jspx的jspx页面中被重写,该页面包含带有openid选择器的表单。资源文件夹也已映射

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

当我查看页面源(未格式化)时,我确实看到了其他不可见的脚本标记;但是,它们的格式与源代码视图中显示的其他标记不同。

<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脚本标记,因为如果脚本标记没有内容,有些浏览器会遇到问题:

<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标记的一个问题是它们需要有一个结束标记,不能在末尾使用/>快捷键。

错误:<script type="text/javascript" src="script.js"/>

更正:<script type="text/javascript" src="script.js"></script>

确实需要以前的注释(type="text/javascript"jsp:text标记),但根据最初的问题,这与spring:url无关。

您正在var名称中使用破折号(-):

var="openid-jquery_url"

用下划线替换它,一切都会神奇地工作。。。

var="openid_jquery_url"

顺便说一句,c:url也是如此,但当你切换到它时,你只是使用了一个没有破折号的不同汽车名称。